-
Notifications
You must be signed in to change notification settings - Fork 9
fix(provider): length-bound and sanitise request inputs across endpoints #2709
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ed8c7cc
fix(provider): length-bound and sanitise request inputs across endpoints
goastler 8a99d5d
Merge remote-tracking branch 'origin/main' into fix/provider-input-sa…
goastler 7029f0a
fix(provider): bound nested request schemas and fix import order
goastler dd07549
fix(provider): length-bound fingerprintProof in SubmitPowCaptchaSolut…
goastler 509729e
fix(provider): widen headHash bound to TOKEN so frictionless challeng…
goastler 997a2db
fix(provider): use safeText/safeLine for freetext & header-derived fi…
goastler c515dfe
Merge branch 'main' into fix/provider-input-sanitisation
goastler 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| "@prosopo/types": patch | ||
| "@prosopo/provider": patch | ||
| --- | ||
|
|
||
| fix(provider): length-bound and sanitise request inputs across the provider API endpoints. | ||
|
|
||
| - Add shared zod helpers in `@prosopo/types` (`INPUT_LIMITS`, `boundedString`, `safeText`, `safeLine`): every request string field is now length-bounded, and human freetext additionally rejects control characters (null bytes etc.). Typing fields as strings already blocks Mongo operator injection; the control-character rejection covers the remaining log/header-injection vectors. | ||
| - Apply the helpers across the provider request schemas (image/pow/puzzle captcha challenge & solution bodies, frictionless challenge, server verify, DNS event ingestion, sitekey register/remove, detector-key and decision-machine admin bodies, and the spam-email check). Tokens, signatures, behavioural/simd readings and decision-machine source get generous caps; accounts/site-keys/hashes/ids get tight ones. | ||
|
|
||
| - Lower the provider API body-parser cap from 50 MB to 1 MB (`express.json` in `startProviderApi.ts`) as a coarse oversized-payload backstop before parsing. | ||
|
|
||
| Email and IP fields are treated as length-bounded strings (email keeps its existing format check where present). | ||
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
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 |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright 2021-2026 Prosopo (UK) Ltd. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| import { string } from "zod"; | ||
|
|
||
| /** | ||
| * Centralised input length limits for request payloads. Generous enough not to | ||
| * reject legitimate input, but bounded so an oversized field cannot bloat | ||
| * storage, logs, downstream API calls, or act as a cheap DoS vector. The | ||
| * express body-size cap (provider startProviderApi.ts) is the coarse backstop; | ||
| * these are the per-field limits. | ||
| */ | ||
| export const INPUT_LIMITS = { | ||
| /** Identifiers, keys, slugs (accounts, site keys, dataset ids, …). */ | ||
| ID: 256, | ||
| /** Names, labels, titles. */ | ||
| NAME: 256, | ||
| /** Email addresses (treated as opaque strings — no format validation). */ | ||
| EMAIL: 320, | ||
| /** URLs. */ | ||
| URL: 2048, | ||
| /** General short freetext. Default for `boundedString` / `safeText`. */ | ||
| TEXT: 16384, | ||
| /** Longer freetext: messages, descriptions, decision-machine source. */ | ||
| LONG_TEXT: 65536, | ||
| /** Tokens, signatures, base64 payloads, behavioural/simd readings. */ | ||
| TOKEN: 131072, | ||
| } as const; | ||
|
|
||
| // Anchored negated character classes: a string is valid only if it contains | ||
| // NONE of these. Implemented as a `.regex()` (rather than `.refine()`) so the | ||
| // result stays a `ZodString` and callers can still chain `.min()` / `.max()`. | ||
| // C0 control chars + DEL (U+007F) are rejected; tab/newline/carriage-return are | ||
| // allowed in safeText. C1 (U+0080–U+009F) is intentionally not rejected, to | ||
| // avoid false positives on legitimate international text. | ||
| // biome-ignore lint/suspicious/noControlCharactersInRegex: deliberately matching control chars in order to reject them | ||
| const NO_CONTROL_CHARS = /^[^\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]*$/; | ||
| // Single-line variant: additionally rejects tab/CR/LF. | ||
| // biome-ignore lint/suspicious/noControlCharactersInRegex: deliberately matching control chars in order to reject them | ||
| const NO_CONTROL_OR_NEWLINE = /^[^\u0000-\u001f\u007f]*$/; | ||
|
|
||
| /** | ||
| * A length-bounded string. Use for structured values (ids, keys, tokens) where | ||
| * the character set is already constrained by format. | ||
| */ | ||
| export const boundedString = (max: number = INPUT_LIMITS.TEXT) => | ||
| string().max(max); | ||
|
|
||
| /** | ||
| * Length-bounded freetext that rejects control characters (null bytes etc.). | ||
| * Use for human-entered, multi-line text (messages, descriptions). Typing the | ||
| * field as a string already blocks Mongo operator injection (an object such as | ||
| * `{$gt:…}` fails the string check); this additionally rejects the control | ||
| * characters used for log/terminal injection. | ||
| */ | ||
| export const safeText = (max: number = INPUT_LIMITS.TEXT) => | ||
| string() | ||
| .max(max) | ||
| .regex(NO_CONTROL_CHARS, "must not contain control characters"); | ||
|
|
||
| /** | ||
| * Single-line variant of {@link safeText}: also rejects line breaks. Use for | ||
| * short freetext that flows into headers/subjects (names, titles) to prevent | ||
| * header injection. | ||
| */ | ||
| export const safeLine = (max: number = INPUT_LIMITS.NAME) => | ||
| string() | ||
| .max(max) | ||
| .regex( | ||
| NO_CONTROL_OR_NEWLINE, | ||
| "must not contain control characters or line breaks", | ||
| ); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.