Files
floci/compatibility-tests/sdk-test-node/package.json
T
Ricardo Medina c426289879 fix(cognito): correct USER_SRP_AUTH PASSWORD_VERIFIER signature (#912)
* fix(cognito): correct USER_SRP_AUTH PASSWORD_VERIFIER signature

Server-side SRP-6a produced HMAC signatures that never matched what
AWS Amplify / SDKs compute, so USER_SRP_AUTH always failed with
NotAuthorizedException. Five independent bugs in the byte layout and
key-derivation chain:

1. SRP multiplier k: hashed N and g left-padded to N_BYTES (384 bytes).
   Cognito/Amplify hash the minimal two's-complement representation
   (385 bytes for N — sign byte prepended because MSB is set — and
   1 byte for g = 2). k participates in both server B = k*v + g^b and
   client S = (B - k*g^x)^(a+ux), so a divergent k breaks the protocol.
   Use N.toByteArray() and G.toByteArray() directly.

2. Session key derivation: previously ran an undocumented "Caldera
   interleaved hash" over S, then fed that into HKDF with a zero salt.
   Cognito applies HKDF-Expand directly over S with the scrambling
   parameter U as salt:
     PRK  = HMAC-SHA256(U_bytes, S_bytes)
     T(1) = HMAC-SHA256(PRK, "Caldera Derived Key" || 0x01)
     key  = T(1)[0..16]
   Inline that HKDF in computeSessionKey; drop interleaved hash + the
   auxiliary hkdf() helper.

3. HKDF output length: helper returned 32 bytes; Amplify uses the first
   16. Return Arrays.copyOf(t1, 16).

4. computeSignature double-HKDF: when sessionKey was already the final
   HMAC key, computeSignature ran HKDF on it again. Use it as-is.

5. computeU / computeX byte layout: U was hashed with A and B
   left-padded to N_BYTES; salt was decoded as raw hex bytes. Amplify
   feeds the minimal two's-complement representation (with sign byte
   for positive MSB-set values). Use BigInteger.toByteArray() for A/B
   and round-trip the hex salt through BigInteger so a sign byte is
   prepended when needed.

Verified by reproducing floci's exact server-side computation in
JavaScript using Amplify's byte conventions: the JS replay reproduces
floci's expected HMAC byte-for-byte (proving the server is internally
consistent) and the divergence from the client's claimed HMAC traces
entirely to k differing. After the fix, USER_SRP_AUTH end-to-end signin
from Amplify v6 (React Native) against floci succeeds.

Compat note: users whose verifier was stored under the previous
computeX with a salt whose first byte has the MSB set (~50% of
generate-salt outputs) will produce a different x and must reset their
password. New signups against this fix are interoperable with Amplify.

Also drops the now-unused padTo() helper.

* test(compatibility): add USER_SRP_AUTH round-trip tests for Node + Java SDKs

The existing CognitoSrpTest only validated that PASSWORD_VERIFIER was
returned by InitiateAuth; nothing exercised the full handshake. With the
server-side SRP math now spec-compliant, add end-to-end tests that prove
real AWS SDK clients authenticate against floci.

Java (sdk-test-java/CognitoSrpTest):
 - Add step 4: compute PASSWORD_CLAIM_SIGNATURE from the challenge using
   the AWS "Caldera" SRP variant, send RespondToAuthChallenge, assert
   AuthenticationResult contains valid JWT id/access/refresh tokens.
 - Helper math (computeX / computeU / computeK / calderaHkdf /
   hmacSha256 / formatCognitoTimestamp) inlined since the AWS Java SDK
   v2 does not expose a public SRP client.

Node (sdk-test-node/tests/cognito-srp.test.ts):
 - New round-trip test using amazon-cognito-identity-js 6.3.16 — the
   canonical JS SRP impl that @aws-amplify/auth wraps internally.
 - Pool is provisioned via AdminCreateUser + AdminSetUserPassword so the
   test doesn't depend on an email-delivered verification code.
 - Validates tokens come back as well-formed JWTs.

Both tests pass against floci with the fix from the previous commit and
fail (CodeMismatchException / NotAuthorizedException) without it.
2026-05-18 23:02:49 -05:00

42 lines
1.3 KiB
JSON

{
"name": "floci-compatibility-tests-typescript",
"version": "1.0.0",
"type": "module",
"scripts": {
"test": "vitest run",
"test:watch": "vitest"
},
"dependencies": {
"@aws-sdk/client-acm": "^3.500.0",
"@aws-sdk/client-apigatewaymanagementapi": "^3.500.0",
"@aws-sdk/client-apigatewayv2": "^3.500.0",
"@aws-sdk/client-cloudformation": "^3.500.0",
"@aws-sdk/client-cloudwatch": "^3.500.0",
"@aws-sdk/client-cognito-identity-provider": "^3.500.0",
"@aws-sdk/client-dynamodb": "^3.500.0",
"@aws-sdk/client-ecr": "^3.500.0",
"@aws-sdk/client-eventbridge": "^3.500.0",
"@aws-sdk/client-iam": "^3.500.0",
"@aws-sdk/client-kinesis": "^3.500.0",
"@aws-sdk/client-kms": "^3.500.0",
"@aws-sdk/client-lambda": "^3.500.0",
"@aws-sdk/client-pipes": "^3.500.0",
"@aws-sdk/client-s3": "^3.500.0",
"@aws-sdk/client-secrets-manager": "^3.500.0",
"@aws-sdk/client-sns": "^3.500.0",
"@aws-sdk/client-sqs": "^3.500.0",
"@aws-sdk/client-ssm": "^3.500.0",
"@aws-sdk/client-sts": "^3.500.0",
"selfsigned": "^5.5.0",
"ws": "^8.0.0"
},
"devDependencies": {
"@types/node": "^20.0.0",
"@types/ws": "^8.0.0",
"amazon-cognito-identity-js": "^6.3.16",
"esbuild": "^0.21.0",
"typescript": "^5.4.0",
"vitest": "^2.0.0"
}
}