Files
floci/docs
Alessandro Lattao 3041d6bd90 fix(rds,elasticache): decode IAM auth token parameters once (#5039)
* fix(rds,elasticache): decode IAM auth token parameters once

SigV4RequestValidator, which verifies the IAM auth tokens the RDS, Redshift,
ElastiCache and MemoryDB proxies receive, URL-decoded every query parameter
as it looked it up, then decoded X-Amz-Credential and X-Amz-Security-Token a
second time. Session tokens are drawn from an alphabet that includes '+',
'/' and '='. A presigner writes '+' as %2B; the first decode restored it and
the second turned it into a space, so the session no longer matched and the
token was refused with "password authentication failed". Every token signed
with a Lambda function's execution-role credentials failed this way, from
the AWS CLI, botocore and the Go SDK's feature/rds/auth alike, while
AssumeRole credentials passed only because their tokens are alphanumeric.
The second decode also let a token carrying %252B stand in for a session
whose token holds a literal '+'.

SigV4 defines the canonical query string as each parameter name and value
URI-encoded (only A-Z a-z 0-9 - . _ ~ left as they are, uppercase hex, a
space as %20, '/' encoded), sorted by encoded name and then by value. The
validator now decodes each parameter exactly once, keeping a literal '+' as
a plus as the API Gateway and EKS verifiers already do, looks the session
up with that value, and rebuilds the canonical query string from the
decoded parameters rather than hashing the bytes on the wire, so the token
is checked against the string its signer hashed whichever valid
percent-encoding it travelled in. Using the raw pairs as before would have
fixed the session lookup alone, but would still reject a token whose
credential travels with literal slashes or lowercase hex.

The test token helper encoded values with URLEncoder, whose form encoding
writes a space as '+' and leaves '*' unescaped, which no SDK presigner does;
it now applies SigV4's UriEncode. S3 presigned URLs do not go through this
validator and read X-Amz-Security-Token once already: a URL presigned with
a Lambda session's credentials is served, and one whose token reads a space
for the '+' is refused.

* fix(rds): authorize the key the token's signature was verified with

The signature check reads a token's parameters with their names
percent-decoded, and takes the first X-Amz-Credential it finds. The
rds-db:connect check read the credential again from the raw query, matching
the name byte for byte. A token that spelled its signer's credential as
X-Amz%2DCredential and added a plain X-Amz-Credential of a key IAM does not
know was verified with the signer's key and authorized on the unknown one,
which IAM enforcement waves through: the connection was allowed whatever
the signer's policy said.

SigV4RequestValidator.queryParameter reads a parameter exactly as the
signature check does, and RdsSigV4Validator takes the credential scope from
there, so the key authorized is the key that signed.
validateAuthorizesTheKeyTheSignatureWasVerifiedWith signs such a token as
SigV4 signs a query and expects it refused; it was accepted before.

RdsIamAuthLambdaSessionIntegrationTest now creates its database inside the
try whose finally deletes it, so a create that fails an assertion leaves no
container behind.
2026-10-08 22:23:11 -05:00
..
2026-03-15 17:43:56 -05:00