Repository navigation
fix(parser): stop malformed base64 from escaping safeParse - #5666
Merged
svozza merged 1 commit intoSep 7, 2026
Merged
Conversation
`Base64Encoded` started its chain with `z.string()` and called `fromBase64` inside a transform. `fromBase64` throws a `TypeError` on invalid base64, and Zod 4 does not catch exceptions thrown inside transforms, so `safeParse` threw instead of returning a failed result. Through `parse()` this also made Zod fall back to async validation, whose rejected promise leaked as an unhandled rejection (`Runtime.UnhandledPromiseRejection` on Lambda). Guard the chain with `z.base64()` so invalid input becomes a normal Zod issue before the transform runs, matching `CloudWatchLogsSchema` and `KinesisFirehoseRecordSchema`. In `parse()`, attach a handler to the promise returned by an async validation before throwing so the rejection cannot leak, and drop the now-reachable `v8 ignore` on that branch. Closes aws-powertools#5651
svozza
approved these changes
Sep 7, 2026
Closed
1 of 2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Changes
Base64Encodedstarted its chain withz.string()and calledfromBase64inside the transform.fromBase64throws aTypeErroron invalid base64, and Zod 4 does not catch exceptions thrown inside transforms, sosafeParsethrew aTypeErrorinstead of returning{ success: false }. This affected the helper directly, the built-inKinesisDataStreamSchema/KinesisEnvelope,parse(event, undefined, KinesisDataStreamSchema, true), and the decorator / Middy middleware insafeParsemode.Through
parse()it was worse: Zod's~standard.validatecatches the synchronous throw and falls back tosafeParseAsync, returning a rejecting promise.parse()saw aPromise, threw the misleading "supports only synchronous validation" error, and never attached a handler to the rejected promise, so it surfaced as an unhandled rejection (Runtime.UnhandledPromiseRejectionon the Lambda runtime).Base64Encodedwithz.base64()instead ofz.string(), so invalid input becomes a normal Zod issue before the transform runs. This matches the existingCloudWatchLogsSchemaandKinesisFirehoseRecordSchema, which already guard withz.base64()and were not affected.parse(), attach a handler to the promise returned by an async validation before throwing, so a rejected promise from Zod's async fallback cannot leak. Dropped the/* v8 ignore */on that branch since it is reachable, and covered it with a test.The decorator and Middy
safeParsepaths route through the same schemas, so they are fixed at the source by the helper change.Added tests:
Base64Encoded(...).safeParse('not base64!!')returns a failed result instead of throwing.parse(event, undefined, KinesisDataStreamSchema, true)with a malformedkinesis.datareturns{ success: false }rather than throwing.parse()throw the sync-onlyParseErrorwithout leaking an unhandled rejection (the run fails if the handler is removed).Verified each test fails against the pre-fix code, and the full parser unit suite (289 tests) passes with 100% coverage on
parser.tsandhelpers/index.ts.Issue number: closes #5651
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.