-
-
Notifications
You must be signed in to change notification settings - Fork 296
fix(S3): enforce presigned POST policy conditions #1211
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
Open
TylerHillery
wants to merge
2
commits into
master
Choose a base branch
from
tyler/fix/presigned-s3-post-forms
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './policy' | ||
| export * from './signature-v4' |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import { ERRORS } from '@internal/errors' | ||
|
|
||
| /** | ||
| * A decoded AWS POST policy document: an expiration plus a list of conditions | ||
| * that the submitted upload form must satisfy. | ||
| */ | ||
| export interface Policy { | ||
| expiration: string | ||
| conditions: PolicyCondition[] | ||
| } | ||
|
|
||
| /** | ||
| * An AWS POST policy condition is either: | ||
| * - an exact-match object with a single key: `{ "bucket": "my-bucket" }` | ||
| * - a tuple: `["eq", "$key", "value"]`, `["starts-with", "$key", "prefix"]`, | ||
| * or `["content-length-range", min, max]` | ||
| */ | ||
| export type PolicyCondition = Record<string, string> | (string | number)[] | ||
|
|
||
| /** | ||
| * Submitted form fields that do NOT need to be covered by a policy condition, | ||
| * keyed by their lowercased name. This mirrors AWS POST semantics: every other | ||
| * field (including `key`, `bucket`, and every `x-amz-meta-*`) must be constrained | ||
| * by a condition, otherwise a holder of a signed policy could attach arbitrary | ||
| * uncovered fields. `file` is stripped before signature parsing; fields prefixed | ||
| * with `x-ignore-` are AWS's documented escape hatch and are also exempt. | ||
| */ | ||
| const EXEMPT_POLICY_FIELDS = new Set([ | ||
| 'policy', | ||
| 'x-amz-signature', | ||
| 'x-amz-algorithm', | ||
| 'x-amz-credential', | ||
| 'x-amz-date', | ||
| 'x-amz-security-token', | ||
| ]) | ||
|
|
||
| /** | ||
| * Validates a POST policy `expiration`, an absolute ISO8601 timestamp (e.g. | ||
| * `2025-01-01T00:00:00Z`). Compares it directly to the current time and never | ||
| * relies on any client-supplied date. The expiration is required: a policy | ||
| * without one is rejected as invalid. | ||
| */ | ||
| export function assertPolicyNotExpired(expiration: string | undefined): void { | ||
| if (!expiration) { | ||
| throw ERRORS.InvalidSignature('Missing policy expiration') | ||
| } | ||
|
|
||
| const expirationDate = new Date(expiration) | ||
| if (isNaN(expirationDate.getTime())) { | ||
| throw ERRORS.InvalidSignature('Invalid policy expiration') | ||
| } | ||
|
|
||
| if (expirationDate < new Date()) { | ||
| throw ERRORS.ExpiredSignature() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Evaluates the signed POST policy conditions against the values the client | ||
| * actually submitted, in a single pass. Throws if any condition is not | ||
| * satisfied, or if any submitted field is not covered by a condition (see | ||
| * {@link EXEMPT_POLICY_FIELDS}). | ||
| * | ||
| * The `bucket` value is taken from the request target (the URL), since AWS POST | ||
| * uploads carry the bucket in the path rather than as a form field. | ||
| * | ||
| * @param policy the decoded policy document | ||
| * @param submittedFields the submitted form fields, keyed by lowercased name | ||
| * @param opts.bucket the bucket resolved from the request path | ||
| */ | ||
| export function assertPolicyConditionsSatisfied( | ||
| policy: Policy, | ||
| submittedFields: Record<string, string>, | ||
| opts: { bucket?: string } | ||
| ): void { | ||
| const conditions = policy?.conditions | ||
| if (!Array.isArray(conditions)) { | ||
| throw ERRORS.InvalidSignature('Invalid policy conditions') | ||
| } | ||
|
|
||
| const fields: Record<string, string> = { ...submittedFields } | ||
| if (opts.bucket !== undefined) { | ||
| fields['bucket'] = opts.bucket | ||
| } | ||
|
|
||
| const coveredFields = new Set<string>() | ||
| for (const condition of conditions) { | ||
| const field = evaluatePolicyCondition(condition, fields) | ||
| if (field) { | ||
| coveredFields.add(field) | ||
| } | ||
| } | ||
|
|
||
| // Every submitted field must be constrained by a condition | ||
| for (const field of Object.keys(fields)) { | ||
| if (EXEMPT_POLICY_FIELDS.has(field) || field.startsWith('x-ignore-')) { | ||
| continue | ||
| } | ||
| if (!coveredFields.has(field)) { | ||
| throw ERRORS.AccessDenied( | ||
| `Policy condition failed: field "${field}" is not covered by the policy` | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function evaluatePolicyCondition( | ||
| condition: PolicyCondition, | ||
| fields: Record<string, string> | ||
| ): string | undefined { | ||
| // Exact-match object form: { "field": "value" } with exactly one key. | ||
| if (condition && typeof condition === 'object' && !Array.isArray(condition)) { | ||
| const keys = Object.keys(condition) | ||
| if (keys.length !== 1) { | ||
| throw ERRORS.InvalidSignature('Invalid policy condition') | ||
| } | ||
| const field = keys[0].toLowerCase() | ||
| assertFieldEquals(field, condition[keys[0]], fields) | ||
| return field | ||
| } | ||
|
|
||
| // Tuple form: ["eq" | "starts-with", "$field", value] or | ||
| // ["content-length-range", min, max]. | ||
| if (Array.isArray(condition)) { | ||
| const [operator, target, value] = condition | ||
|
|
||
| // TODO: content-length-range (min/max file size) is recognized but NOT | ||
| // enforce yet. enforcement is tracked as a follow-up). | ||
| if (operator === 'content-length-range') { | ||
| return undefined | ||
| } | ||
|
|
||
| if ((operator === 'eq' || operator === 'starts-with') && typeof target === 'string') { | ||
| if (!target.startsWith('$')) { | ||
| throw ERRORS.InvalidSignature('Invalid policy condition') | ||
| } | ||
| const field = target.slice(1).toLowerCase() | ||
| if (operator === 'eq') { | ||
| assertFieldEquals(field, value, fields) | ||
| } else { | ||
| assertFieldStartsWith(field, value, fields) | ||
| } | ||
| return field | ||
| } | ||
| } | ||
|
|
||
| throw ERRORS.InvalidSignature('Unsupported policy condition') | ||
| } | ||
|
|
||
| function assertFieldEquals( | ||
| field: string, | ||
| expected: string | number, | ||
| fields: Record<string, string> | ||
| ): void { | ||
| const actual = fields[field] | ||
| if (actual === undefined || actual !== String(expected)) { | ||
| throw ERRORS.AccessDenied(`Policy condition failed: "${field}" does not match`) | ||
| } | ||
| } | ||
|
|
||
| function assertFieldStartsWith( | ||
| field: string, | ||
| prefix: string | number, | ||
| fields: Record<string, string> | ||
| ): void { | ||
| const actual = fields[field] | ||
| if (actual === undefined || !actual.startsWith(String(prefix ?? ''))) { | ||
| throw ERRORS.AccessDenied(`Policy condition failed: "${field}" does not start with "${prefix}"`) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if we should move this check inside the signature-v4
parseMultipartSignatureso that we validate it before returning it