This is an automated email from the ASF dual-hosted git repository.
lresende pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-toree.git
The following commit(s) were added to refs/heads/master by this push:
new 215dbcf8 Fix memory leak in HmacAlgorithm and make it case-insensitive
(#250)
215dbcf8 is described below
commit 215dbcf8b9284b6d95564b95f76324ccce92f744
Author: Luciano Resende <[email protected]>
AuthorDate: Sun Mar 8 22:54:56 2026 -0700
Fix memory leak in HmacAlgorithm and make it case-insensitive (#250)
- Fixes the AssertionError: Duplicate id
- Fixes a memory leak in HmacAlgorithm by modifying Hmac.scala to move
predefined
algorithms before the apply method, making apply thread-safe, and
ensuring it reuses existing values case-insensitively.
- Adds test cases to HmacSpec.scala to verify case-insensitivity works
correctly
---
.../org/apache/toree/communication/security/Hmac.scala | 6 ++++--
.../apache/toree/communication/security/HmacSpec.scala | 15 +++++++++++++++
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git
a/communication/src/main/scala/org/apache/toree/communication/security/Hmac.scala
b/communication/src/main/scala/org/apache/toree/communication/security/Hmac.scala
index 06582b32..71514c0b 100644
---
a/communication/src/main/scala/org/apache/toree/communication/security/Hmac.scala
+++
b/communication/src/main/scala/org/apache/toree/communication/security/Hmac.scala
@@ -25,11 +25,13 @@ import
org.apache.toree.communication.security.HmacAlgorithm.HmacAlgorithm
object HmacAlgorithm extends Enumeration {
type HmacAlgorithm = Value
- def apply(key: String) = Value(key)
-
val MD5 = Value("HmacMD5")
val SHA1 = Value("HmacSHA1")
val SHA256 = Value("HmacSHA256")
+
+ def apply(name: String): HmacAlgorithm = synchronized {
+ values.find(_.toString.equalsIgnoreCase(name)).getOrElse(Value(name))
+ }
}
object Hmac {
diff --git
a/communication/src/test/scala/org/apache/toree/communication/security/HmacSpec.scala
b/communication/src/test/scala/org/apache/toree/communication/security/HmacSpec.scala
index 24d71a80..2f459f8f 100644
---
a/communication/src/test/scala/org/apache/toree/communication/security/HmacSpec.scala
+++
b/communication/src/test/scala/org/apache/toree/communication/security/HmacSpec.scala
@@ -106,6 +106,21 @@ class HmacSpec extends AnyFunSpec with Matchers {
// the Scala enumeration value
resultTypeName should be ("scala.Enumeration$Val")
}
+
+ it("should reuse existing values for predefined algorithms
(case-insensitive)") {
+ HmacAlgorithm("HmacSHA256") should be (HmacAlgorithm.SHA256)
+ HmacAlgorithm("hmacsha256") should be (HmacAlgorithm.SHA256)
+ HmacAlgorithm("HmacMD5") should be (HmacAlgorithm.MD5)
+ HmacAlgorithm("hmacmd5") should be (HmacAlgorithm.MD5)
+ HmacAlgorithm("HmacSHA1") should be (HmacAlgorithm.SHA1)
+ HmacAlgorithm("hmacsha1") should be (HmacAlgorithm.SHA1)
+ }
+
+ it("should reuse existing values for custom algorithms") {
+ val custom = HmacAlgorithm("CustomAlgorithm")
+ HmacAlgorithm("CustomAlgorithm") should be (custom)
+ HmacAlgorithm("customalgorithm") should be (custom)
+ }
}
}
}