[drizzle-zod] Add security features: mode, trim, and defaultTextMaxLength #5152
+269
−14
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
Adds opt-in security features to protect against common web application vulnerabilities:
mode: Control how unknown keys are handled ('strip'|'strict'|'passthrough')trim: Automatically trim whitespace from strings and enumsdefaultTextMaxLength: Limit unbounded text columns to prevent DoS attacksWhen using
createSchemaFactory(), secure defaults are applied automatically. Standalone functions remain backward compatible.Motivation
Mass Assignment Attacks
Without strict mode, attackers can inject extra fields like
isAdmin: true:Whitespace Bypass Attacks
Attackers can bypass validation with whitespace:
DoS via Large Payloads
Unbounded text columns can accept gigabytes of data:
Secure Defaults
createSchemaFactory()now applies secure defaults:mode: 'strict'- Reject unknown keystrim: true- Trim whitespacedefaultTextMaxLength: 65535- 64KB limitUsers can override any default:
Backward Compatibility
createInsertSchema(),createSelectSchema(),createUpdateSchema()are unchangedcreateSchemaFactory()applies secure defaultsChanges
src/schema.types.ts: AddSchemaMode, security options to factorysrc/schema.ts: AddapplySchemaMode(), secure defaultssrc/column.ts: Add trim/maxLength support withz.preprocess()tests/pg.test.ts: Add 10 comprehensive security testsTest plan