Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated constantTimeEquals methods from CSRFTokenSigner #12519

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -54,20 +54,6 @@ trait CSRFTokenSigner {
* Compare two signed tokens
*/
def compareSignedTokens(tokenA: String, tokenB: String): Boolean

/**
* Constant time equals method.
*
* Given a length that both Strings are equal to, this method will always
* run in constant time. This prevents timing attacks.
*
* @deprecated Please use `java.security.MessageDigest.isEqual(a.getBytes("utf-8"), b.getBytes("utf-8"))` over this method.
*/
@deprecated(
"Please use java.security.MessageDigest.isEqual(a.getBytes(\"utf-8\"), b.getBytes(\"utf-8\")) over this method.",
"2.6.0"
)
def constantTimeEquals(a: String, b: String): Boolean
}

/**
Expand All @@ -92,7 +78,7 @@ class DefaultCSRFTokenSigner @Inject() (signer: CookieSigner, clock: Clock) exte
*/
def signToken(token: String): String = {
val nonce = clock.millis()
val joined = s"${nonce}-${token}"
val joined = s"$nonce-$token"
signer.sign(joined) + "-" + joined
}

Expand Down Expand Up @@ -133,25 +119,11 @@ class DefaultCSRFTokenSigner @Inject() (signer: CookieSigner, clock: Clock) exte
} yield isEqual(rawA, rawB)).getOrElse(false)
}

override def constantTimeEquals(a: String, b: String): Boolean = isEqual(a, b)

private def isEqual(a: String, b: String): Boolean = {
MessageDigest.isEqual(a.getBytes(StandardCharsets.UTF_8), b.getBytes(StandardCharsets.UTF_8))
}
}

@deprecated("CSRFTokenSigner's singleton object can be replaced by MessageDigest.isEqual", "2.6.0")
object CSRFTokenSigner {

/**
* @deprecated Please use [[java.security.MessageDigest.isEqual]] over this method.
*/
@deprecated("Consider java.security.MessageDigest.isEqual", "2.6.0")
def constantTimeEquals(a: String, b: String): Boolean = {
MessageDigest.isEqual(a.getBytes(StandardCharsets.UTF_8), b.getBytes(StandardCharsets.UTF_8))
}
}

@Singleton
class CSRFTokenSignerProvider @Inject() (signer: CookieSigner) extends Provider[CSRFTokenSigner] {
lazy val get: CSRFTokenSigner = new DefaultCSRFTokenSigner(signer, Clock.systemUTC())
Expand Down