@platform-sec/sensitive-string-zod 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +312 -2
- package/dist/base.d.ts +173 -0
- package/dist/base.d.ts.map +1 -0
- package/dist/base.js +238 -0
- package/dist/base.js.map +1 -0
- package/dist/collections.d.ts +109 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/collections.js +114 -0
- package/dist/collections.js.map +1 -0
- package/dist/conditional.d.ts +101 -0
- package/dist/conditional.d.ts.map +1 -0
- package/dist/conditional.js +85 -0
- package/dist/conditional.js.map +1 -0
- package/dist/defaults.d.ts +48 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +60 -0
- package/dist/defaults.js.map +1 -0
- package/dist/error-map.d.ts +180 -0
- package/dist/error-map.d.ts.map +1 -0
- package/dist/error-map.js +311 -0
- package/dist/error-map.js.map +1 -0
- package/dist/index.d.ts +55 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +91 -0
- package/dist/index.js.map +1 -0
- package/dist/preprocessing.d.ts +106 -0
- package/dist/preprocessing.d.ts.map +1 -0
- package/dist/preprocessing.js +149 -0
- package/dist/preprocessing.js.map +1 -0
- package/dist/refine.d.ts +61 -0
- package/dist/refine.d.ts.map +1 -0
- package/dist/refine.js +114 -0
- package/dist/refine.js.map +1 -0
- package/dist/types.d.ts +75 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/package.json +46 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @platform-sec/sensitive-string-zod
|
|
3
|
+
*
|
|
4
|
+
* Zod integration for SensitiveString.
|
|
5
|
+
* Parse and validate input directly into sensitive types at boundary layers.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { z } from 'zod';
|
|
10
|
+
* import { zSensitive, zCredential } from '@platform-sec/sensitive-string-zod';
|
|
11
|
+
*
|
|
12
|
+
* const WebhookSchema = z.object({
|
|
13
|
+
* token: zCredential('session_token'),
|
|
14
|
+
* email: zSensitive('email'),
|
|
15
|
+
* note: z.string().max(1024).optional(),
|
|
16
|
+
* });
|
|
17
|
+
*
|
|
18
|
+
* // Type inference works correctly
|
|
19
|
+
* type Webhook = z.infer<typeof WebhookSchema>;
|
|
20
|
+
* // Webhook['token'] is SensitiveString<'credential'>
|
|
21
|
+
* // Webhook['email'] is SensitiveString<'sensitive'>
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @example Collections, normalization, and refinements
|
|
25
|
+
* ```ts
|
|
26
|
+
* const SignupSchema = z.object({
|
|
27
|
+
* email: zSensitivePreprocess({ trim: true, lowercase: true }, 'email', z.email()),
|
|
28
|
+
* password: zCredential('password').refineSensitive((raw) => raw.length >= 12, {
|
|
29
|
+
* message: 'Password must be at least 12 characters',
|
|
30
|
+
* }),
|
|
31
|
+
* backupCodes: zCredentialArray('backup_code').min(1),
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // Errors that never echo the input, in the message or in the issue objects.
|
|
35
|
+
* const result = sensitiveSafeParse(SignupSchema, req.body);
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @packageDocumentation
|
|
39
|
+
*/
|
|
40
|
+
export { zSensitiveString, zSensitive, zCredential, zSensitiveWithValidation, zSensitiveOptional, zSensitiveDeserialize, } from './base.js';
|
|
41
|
+
export { zSensitiveStringArray, zSensitiveArray, zCredentialArray, zSensitiveStringRecord, zSensitiveRecord, zCredentialRecord, } from './collections.js';
|
|
42
|
+
export type { SensitiveArraySchema, SensitiveRecordSchema } from './collections.js';
|
|
43
|
+
export { refineSensitive } from './refine.js';
|
|
44
|
+
export { zSensitiveStringPreprocess, zSensitivePreprocess, zCredentialPreprocess, } from './preprocessing.js';
|
|
45
|
+
export type { PreprocessOptions, PreprocessSpec } from './preprocessing.js';
|
|
46
|
+
export { zConditionalSensitive } from './conditional.js';
|
|
47
|
+
export type { ConditionalSensitivity, ConditionalConditions, ConditionalSensitiveConfig, ConditionalSensitiveInput, ConditionalSensitiveOutput, } from './conditional.js';
|
|
48
|
+
export { zSensitiveDefault } from './defaults.js';
|
|
49
|
+
export { sensitiveErrorMap, installSensitiveErrorMap, redactZodIssues, redactZodError, sensitiveParse, sensitiveSafeParse, sensitiveSafeParseAsync, REDACTED_ISSUE_VALUE, } from './error-map.js';
|
|
50
|
+
export type { SensitiveParseOptions } from './error-map.js';
|
|
51
|
+
export type { SensitiveSchema, SensitiveSchemaMethods, SensitiveCheck, RefineSensitiveParams, } from './types.js';
|
|
52
|
+
export { SensitiveString } from '@platform-sec/sensitive-string-core';
|
|
53
|
+
export type { SensitivityClass, SerializedSensitiveString, RevealContext, } from '@platform-sec/sensitive-string-core';
|
|
54
|
+
export { withRevealContext, getCurrentRevealContext, safeLog, redactInStructure, } from '@platform-sec/sensitive-string-core';
|
|
55
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAGH,OAAO,EACL,gBAAgB,EAChB,UAAU,EACV,WAAW,EACX,wBAAwB,EACxB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,WAAW,CAAC;AAGnB,OAAO,EACL,qBAAqB,EACrB,eAAe,EACf,gBAAgB,EAChB,sBAAsB,EACtB,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AAGpF,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAG9C,OAAO,EACL,0BAA0B,EAC1B,oBAAoB,EACpB,qBAAqB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAG5E,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AACzD,YAAY,EACV,sBAAsB,EACtB,qBAAqB,EACrB,0BAA0B,EAC1B,yBAAyB,EACzB,0BAA0B,GAC3B,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAGlD,OAAO,EACL,iBAAiB,EACjB,wBAAwB,EACxB,eAAe,EACf,cAAc,EACd,cAAc,EACd,kBAAkB,EAClB,uBAAuB,EACvB,oBAAoB,GACrB,MAAM,gBAAgB,CAAC;AACxB,YAAY,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAG5D,YAAY,EACV,eAAe,EACf,sBAAsB,EACtB,cAAc,EACd,qBAAqB,GACtB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AACtE,YAAY,EACV,gBAAgB,EAChB,yBAAyB,EACzB,aAAa,GACd,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EACL,iBAAiB,EACjB,uBAAuB,EACvB,OAAO,EACP,iBAAiB,GAClB,MAAM,qCAAqC,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @platform-sec/sensitive-string-zod
|
|
4
|
+
*
|
|
5
|
+
* Zod integration for SensitiveString.
|
|
6
|
+
* Parse and validate input directly into sensitive types at boundary layers.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import { z } from 'zod';
|
|
11
|
+
* import { zSensitive, zCredential } from '@platform-sec/sensitive-string-zod';
|
|
12
|
+
*
|
|
13
|
+
* const WebhookSchema = z.object({
|
|
14
|
+
* token: zCredential('session_token'),
|
|
15
|
+
* email: zSensitive('email'),
|
|
16
|
+
* note: z.string().max(1024).optional(),
|
|
17
|
+
* });
|
|
18
|
+
*
|
|
19
|
+
* // Type inference works correctly
|
|
20
|
+
* type Webhook = z.infer<typeof WebhookSchema>;
|
|
21
|
+
* // Webhook['token'] is SensitiveString<'credential'>
|
|
22
|
+
* // Webhook['email'] is SensitiveString<'sensitive'>
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @example Collections, normalization, and refinements
|
|
26
|
+
* ```ts
|
|
27
|
+
* const SignupSchema = z.object({
|
|
28
|
+
* email: zSensitivePreprocess({ trim: true, lowercase: true }, 'email', z.email()),
|
|
29
|
+
* password: zCredential('password').refineSensitive((raw) => raw.length >= 12, {
|
|
30
|
+
* message: 'Password must be at least 12 characters',
|
|
31
|
+
* }),
|
|
32
|
+
* backupCodes: zCredentialArray('backup_code').min(1),
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Errors that never echo the input, in the message or in the issue objects.
|
|
36
|
+
* const result = sensitiveSafeParse(SignupSchema, req.body);
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @packageDocumentation
|
|
40
|
+
*/
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.redactInStructure = exports.safeLog = exports.getCurrentRevealContext = exports.withRevealContext = exports.SensitiveString = exports.REDACTED_ISSUE_VALUE = exports.sensitiveSafeParseAsync = exports.sensitiveSafeParse = exports.sensitiveParse = exports.redactZodError = exports.redactZodIssues = exports.installSensitiveErrorMap = exports.sensitiveErrorMap = exports.zSensitiveDefault = exports.zConditionalSensitive = exports.zCredentialPreprocess = exports.zSensitivePreprocess = exports.zSensitiveStringPreprocess = exports.refineSensitive = exports.zCredentialRecord = exports.zSensitiveRecord = exports.zSensitiveStringRecord = exports.zCredentialArray = exports.zSensitiveArray = exports.zSensitiveStringArray = exports.zSensitiveDeserialize = exports.zSensitiveOptional = exports.zSensitiveWithValidation = exports.zCredential = exports.zSensitive = exports.zSensitiveString = void 0;
|
|
43
|
+
// Base schema factories
|
|
44
|
+
var base_js_1 = require("./base.js");
|
|
45
|
+
Object.defineProperty(exports, "zSensitiveString", { enumerable: true, get: function () { return base_js_1.zSensitiveString; } });
|
|
46
|
+
Object.defineProperty(exports, "zSensitive", { enumerable: true, get: function () { return base_js_1.zSensitive; } });
|
|
47
|
+
Object.defineProperty(exports, "zCredential", { enumerable: true, get: function () { return base_js_1.zCredential; } });
|
|
48
|
+
Object.defineProperty(exports, "zSensitiveWithValidation", { enumerable: true, get: function () { return base_js_1.zSensitiveWithValidation; } });
|
|
49
|
+
Object.defineProperty(exports, "zSensitiveOptional", { enumerable: true, get: function () { return base_js_1.zSensitiveOptional; } });
|
|
50
|
+
Object.defineProperty(exports, "zSensitiveDeserialize", { enumerable: true, get: function () { return base_js_1.zSensitiveDeserialize; } });
|
|
51
|
+
// Collections
|
|
52
|
+
var collections_js_1 = require("./collections.js");
|
|
53
|
+
Object.defineProperty(exports, "zSensitiveStringArray", { enumerable: true, get: function () { return collections_js_1.zSensitiveStringArray; } });
|
|
54
|
+
Object.defineProperty(exports, "zSensitiveArray", { enumerable: true, get: function () { return collections_js_1.zSensitiveArray; } });
|
|
55
|
+
Object.defineProperty(exports, "zCredentialArray", { enumerable: true, get: function () { return collections_js_1.zCredentialArray; } });
|
|
56
|
+
Object.defineProperty(exports, "zSensitiveStringRecord", { enumerable: true, get: function () { return collections_js_1.zSensitiveStringRecord; } });
|
|
57
|
+
Object.defineProperty(exports, "zSensitiveRecord", { enumerable: true, get: function () { return collections_js_1.zSensitiveRecord; } });
|
|
58
|
+
Object.defineProperty(exports, "zCredentialRecord", { enumerable: true, get: function () { return collections_js_1.zCredentialRecord; } });
|
|
59
|
+
// Refinements over the raw value
|
|
60
|
+
var refine_js_1 = require("./refine.js");
|
|
61
|
+
Object.defineProperty(exports, "refineSensitive", { enumerable: true, get: function () { return refine_js_1.refineSensitive; } });
|
|
62
|
+
// Normalization before wrapping
|
|
63
|
+
var preprocessing_js_1 = require("./preprocessing.js");
|
|
64
|
+
Object.defineProperty(exports, "zSensitiveStringPreprocess", { enumerable: true, get: function () { return preprocessing_js_1.zSensitiveStringPreprocess; } });
|
|
65
|
+
Object.defineProperty(exports, "zSensitivePreprocess", { enumerable: true, get: function () { return preprocessing_js_1.zSensitivePreprocess; } });
|
|
66
|
+
Object.defineProperty(exports, "zCredentialPreprocess", { enumerable: true, get: function () { return preprocessing_js_1.zCredentialPreprocess; } });
|
|
67
|
+
// Sensitivity decided by a sibling field
|
|
68
|
+
var conditional_js_1 = require("./conditional.js");
|
|
69
|
+
Object.defineProperty(exports, "zConditionalSensitive", { enumerable: true, get: function () { return conditional_js_1.zConditionalSensitive; } });
|
|
70
|
+
// Default values
|
|
71
|
+
var defaults_js_1 = require("./defaults.js");
|
|
72
|
+
Object.defineProperty(exports, "zSensitiveDefault", { enumerable: true, get: function () { return defaults_js_1.zSensitiveDefault; } });
|
|
73
|
+
// Error handling that does not echo the input
|
|
74
|
+
var error_map_js_1 = require("./error-map.js");
|
|
75
|
+
Object.defineProperty(exports, "sensitiveErrorMap", { enumerable: true, get: function () { return error_map_js_1.sensitiveErrorMap; } });
|
|
76
|
+
Object.defineProperty(exports, "installSensitiveErrorMap", { enumerable: true, get: function () { return error_map_js_1.installSensitiveErrorMap; } });
|
|
77
|
+
Object.defineProperty(exports, "redactZodIssues", { enumerable: true, get: function () { return error_map_js_1.redactZodIssues; } });
|
|
78
|
+
Object.defineProperty(exports, "redactZodError", { enumerable: true, get: function () { return error_map_js_1.redactZodError; } });
|
|
79
|
+
Object.defineProperty(exports, "sensitiveParse", { enumerable: true, get: function () { return error_map_js_1.sensitiveParse; } });
|
|
80
|
+
Object.defineProperty(exports, "sensitiveSafeParse", { enumerable: true, get: function () { return error_map_js_1.sensitiveSafeParse; } });
|
|
81
|
+
Object.defineProperty(exports, "sensitiveSafeParseAsync", { enumerable: true, get: function () { return error_map_js_1.sensitiveSafeParseAsync; } });
|
|
82
|
+
Object.defineProperty(exports, "REDACTED_ISSUE_VALUE", { enumerable: true, get: function () { return error_map_js_1.REDACTED_ISSUE_VALUE; } });
|
|
83
|
+
// Re-export core types and utilities for convenience
|
|
84
|
+
var sensitive_string_core_1 = require("@platform-sec/sensitive-string-core");
|
|
85
|
+
Object.defineProperty(exports, "SensitiveString", { enumerable: true, get: function () { return sensitive_string_core_1.SensitiveString; } });
|
|
86
|
+
var sensitive_string_core_2 = require("@platform-sec/sensitive-string-core");
|
|
87
|
+
Object.defineProperty(exports, "withRevealContext", { enumerable: true, get: function () { return sensitive_string_core_2.withRevealContext; } });
|
|
88
|
+
Object.defineProperty(exports, "getCurrentRevealContext", { enumerable: true, get: function () { return sensitive_string_core_2.getCurrentRevealContext; } });
|
|
89
|
+
Object.defineProperty(exports, "safeLog", { enumerable: true, get: function () { return sensitive_string_core_2.safeLog; } });
|
|
90
|
+
Object.defineProperty(exports, "redactInStructure", { enumerable: true, get: function () { return sensitive_string_core_2.redactInStructure; } });
|
|
91
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;;;AAEH,wBAAwB;AACxB,qCAOmB;AANjB,2GAAA,gBAAgB,OAAA;AAChB,qGAAA,UAAU,OAAA;AACV,sGAAA,WAAW,OAAA;AACX,mHAAA,wBAAwB,OAAA;AACxB,6GAAA,kBAAkB,OAAA;AAClB,gHAAA,qBAAqB,OAAA;AAGvB,cAAc;AACd,mDAO0B;AANxB,uHAAA,qBAAqB,OAAA;AACrB,iHAAA,eAAe,OAAA;AACf,kHAAA,gBAAgB,OAAA;AAChB,wHAAA,sBAAsB,OAAA;AACtB,kHAAA,gBAAgB,OAAA;AAChB,mHAAA,iBAAiB,OAAA;AAInB,iCAAiC;AACjC,yCAA8C;AAArC,4GAAA,eAAe,OAAA;AAExB,gCAAgC;AAChC,uDAI4B;AAH1B,8HAAA,0BAA0B,OAAA;AAC1B,wHAAA,oBAAoB,OAAA;AACpB,yHAAA,qBAAqB,OAAA;AAIvB,yCAAyC;AACzC,mDAAyD;AAAhD,uHAAA,qBAAqB,OAAA;AAS9B,iBAAiB;AACjB,6CAAkD;AAAzC,gHAAA,iBAAiB,OAAA;AAE1B,8CAA8C;AAC9C,+CASwB;AARtB,iHAAA,iBAAiB,OAAA;AACjB,wHAAA,wBAAwB,OAAA;AACxB,+GAAA,eAAe,OAAA;AACf,8GAAA,cAAc,OAAA;AACd,8GAAA,cAAc,OAAA;AACd,kHAAA,kBAAkB,OAAA;AAClB,uHAAA,uBAAuB,OAAA;AACvB,oHAAA,oBAAoB,OAAA;AAYtB,qDAAqD;AACrD,6EAAsE;AAA7D,wHAAA,eAAe,OAAA;AAMxB,6EAK6C;AAJ3C,0HAAA,iBAAiB,OAAA;AACjB,gIAAA,uBAAuB,OAAA;AACvB,gHAAA,OAAO,OAAA;AACP,0HAAA,iBAAiB,OAAA"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalizing input before it is wrapped.
|
|
3
|
+
*
|
|
4
|
+
* Normalization has to happen before wrapping: once a value is a
|
|
5
|
+
* SensitiveString, trimming it means revealing it. Doing it here keeps the
|
|
6
|
+
* whole normalize-validate-wrap sequence on the raw string, inside the parse,
|
|
7
|
+
* with no reveal at all.
|
|
8
|
+
*
|
|
9
|
+
* Order is normalize, then validate, then wrap. That matches Zod's own
|
|
10
|
+
* `preprocess`, and it is the order callers expect: `' A@B.COM '` should pass
|
|
11
|
+
* an email check.
|
|
12
|
+
*
|
|
13
|
+
* @packageDocumentation
|
|
14
|
+
*/
|
|
15
|
+
import { z } from 'zod';
|
|
16
|
+
import { SensitivityClass } from '@platform-sec/sensitive-string-core';
|
|
17
|
+
import type { SensitiveSchema } from './types.js';
|
|
18
|
+
/**
|
|
19
|
+
* Declarative normalization steps, applied in the order listed here.
|
|
20
|
+
*/
|
|
21
|
+
export interface PreprocessOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Unicode normalization form, applied first so that later steps and any
|
|
24
|
+
* validation see one canonical encoding of the same text.
|
|
25
|
+
*/
|
|
26
|
+
normalize?: 'NFC' | 'NFD' | 'NFKC' | 'NFKD';
|
|
27
|
+
/** Collapse each run of whitespace to a single space. */
|
|
28
|
+
collapseWhitespace?: boolean;
|
|
29
|
+
/** Remove every whitespace character. Useful for keys pasted with line breaks. */
|
|
30
|
+
stripWhitespace?: boolean;
|
|
31
|
+
/** Remove leading and trailing whitespace. */
|
|
32
|
+
trim?: boolean;
|
|
33
|
+
/** Lowercase the value. Mutually exclusive with `uppercase`. */
|
|
34
|
+
lowercase?: boolean;
|
|
35
|
+
/** Uppercase the value. Mutually exclusive with `lowercase`. */
|
|
36
|
+
uppercase?: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* What a caller may pass as the normalization to apply: the declarative option
|
|
40
|
+
* set, a single function, or a list of functions run left to right.
|
|
41
|
+
*/
|
|
42
|
+
export type PreprocessSpec = PreprocessOptions | ((value: string) => string) | ReadonlyArray<(value: string) => string>;
|
|
43
|
+
/**
|
|
44
|
+
* Create a schema that normalizes the input, validates it, then wraps it.
|
|
45
|
+
*
|
|
46
|
+
* A non-string input is passed through untouched, so the inner string schema
|
|
47
|
+
* reports the usual `invalid_type` issue rather than the preprocessor throwing
|
|
48
|
+
* on it.
|
|
49
|
+
*
|
|
50
|
+
* The inner schema is any string-to-string schema, not specifically a
|
|
51
|
+
* `z.ZodString`, so that Zod 4's format constructors work: `z.email()` is a
|
|
52
|
+
* `ZodEmail` and is not a `ZodString`.
|
|
53
|
+
*
|
|
54
|
+
* @typeParam C - The sensitivity class for the resulting SensitiveString
|
|
55
|
+
* @param klass - The sensitivity classification to apply
|
|
56
|
+
* @param spec - Normalization options, a function, or a list of functions
|
|
57
|
+
* @param label - Optional label for audit/compliance
|
|
58
|
+
* @param schema - Optional string-to-string schema, validated after normalization
|
|
59
|
+
* @returns A schema that transforms normalized strings to SensitiveString<C>
|
|
60
|
+
* @throws TypeError If the spec is malformed.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const email = zSensitiveStringPreprocess(
|
|
65
|
+
* 'sensitive',
|
|
66
|
+
* { trim: true, lowercase: true },
|
|
67
|
+
* 'email',
|
|
68
|
+
* z.email()
|
|
69
|
+
* );
|
|
70
|
+
*
|
|
71
|
+
* email.parse(' Alice@Example.COM ').reveal(); // 'alice@example.com'
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export declare function zSensitiveStringPreprocess<C extends SensitivityClass>(klass: C, spec: PreprocessSpec, label?: string, schema?: z.ZodType<string, string>): SensitiveSchema<C, string>;
|
|
75
|
+
/**
|
|
76
|
+
* Normalize, then wrap as sensitive data.
|
|
77
|
+
* Shorthand for zSensitiveStringPreprocess('sensitive', spec, label, schema).
|
|
78
|
+
*
|
|
79
|
+
* @param spec - Normalization options, a function, or a list of functions
|
|
80
|
+
* @param label - Optional label for audit/compliance
|
|
81
|
+
* @param schema - Optional string-to-string schema, validated after normalization
|
|
82
|
+
* @returns A schema producing SensitiveString<'sensitive'>
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* const schema = zSensitivePreprocess({ trim: true, lowercase: true }, 'email');
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare function zSensitivePreprocess(spec: PreprocessSpec, label?: string, schema?: z.ZodType<string, string>): SensitiveSchema<'sensitive', string>;
|
|
90
|
+
/**
|
|
91
|
+
* Normalize, then wrap as a credential.
|
|
92
|
+
* Shorthand for zSensitiveStringPreprocess('credential', spec, label, schema).
|
|
93
|
+
*
|
|
94
|
+
* @param spec - Normalization options, a function, or a list of functions
|
|
95
|
+
* @param label - Optional label for audit/compliance
|
|
96
|
+
* @param schema - Optional string-to-string schema, validated after normalization
|
|
97
|
+
* @returns A schema producing SensitiveString<'credential'>
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* // An API key pasted from a console arrives with line breaks in it.
|
|
102
|
+
* const apiKey = zCredentialPreprocess({ stripWhitespace: true }, 'api_key');
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export declare function zCredentialPreprocess(spec: PreprocessSpec, label?: string, schema?: z.ZodType<string, string>): SensitiveSchema<'credential', string>;
|
|
106
|
+
//# sourceMappingURL=preprocessing.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preprocessing.d.ts","sourceRoot":"","sources":["../src/preprocessing.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAmB,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAExF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;;OAGG;IACH,SAAS,CAAC,EAAE,KAAK,GAAG,KAAK,GAAG,MAAM,GAAG,MAAM,CAAC;IAC5C,yDAAyD;IACzD,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,kFAAkF;IAClF,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,8CAA8C;IAC9C,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,gEAAgE;IAChE,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,gEAAgE;IAChE,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,MAAM,cAAc,GACxB,iBAAiB,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,GAAG,aAAa,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;AAmD7F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,SAAS,gBAAgB,EACnE,KAAK,EAAE,CAAC,EACR,IAAI,EAAE,cAAc,EACpB,KAAK,CAAC,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,GACjC,eAAe,CAAC,CAAC,EAAE,MAAM,CAAC,CAmB5B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,cAAc,EACpB,KAAK,CAAC,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,GACjC,eAAe,CAAC,WAAW,EAAE,MAAM,CAAC,CAEtC;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,qBAAqB,CACnC,IAAI,EAAE,cAAc,EACpB,KAAK,CAAC,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,GACjC,eAAe,CAAC,YAAY,EAAE,MAAM,CAAC,CAEvC"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Normalizing input before it is wrapped.
|
|
4
|
+
*
|
|
5
|
+
* Normalization has to happen before wrapping: once a value is a
|
|
6
|
+
* SensitiveString, trimming it means revealing it. Doing it here keeps the
|
|
7
|
+
* whole normalize-validate-wrap sequence on the raw string, inside the parse,
|
|
8
|
+
* with no reveal at all.
|
|
9
|
+
*
|
|
10
|
+
* Order is normalize, then validate, then wrap. That matches Zod's own
|
|
11
|
+
* `preprocess`, and it is the order callers expect: `' A@B.COM '` should pass
|
|
12
|
+
* an email check.
|
|
13
|
+
*
|
|
14
|
+
* @packageDocumentation
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.zSensitiveStringPreprocess = zSensitiveStringPreprocess;
|
|
18
|
+
exports.zSensitivePreprocess = zSensitivePreprocess;
|
|
19
|
+
exports.zCredentialPreprocess = zCredentialPreprocess;
|
|
20
|
+
const zod_1 = require("zod");
|
|
21
|
+
const sensitive_string_core_1 = require("@platform-sec/sensitive-string-core");
|
|
22
|
+
const refine_js_1 = require("./refine.js");
|
|
23
|
+
/**
|
|
24
|
+
* Turn a spec into a single function over the raw string.
|
|
25
|
+
*
|
|
26
|
+
* @throws TypeError If `lowercase` and `uppercase` are both set, or a step is not a function.
|
|
27
|
+
*/
|
|
28
|
+
function compileSpec(spec) {
|
|
29
|
+
if (typeof spec === 'function') {
|
|
30
|
+
return spec;
|
|
31
|
+
}
|
|
32
|
+
if (Array.isArray(spec)) {
|
|
33
|
+
const steps = spec;
|
|
34
|
+
for (const step of steps) {
|
|
35
|
+
if (typeof step !== 'function') {
|
|
36
|
+
throw new TypeError('Preprocess steps must be functions');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return (value) => steps.reduce((acc, step) => step(acc), value);
|
|
40
|
+
}
|
|
41
|
+
const options = spec;
|
|
42
|
+
if (options.lowercase === true && options.uppercase === true) {
|
|
43
|
+
throw new TypeError('Preprocess options cannot set both lowercase and uppercase');
|
|
44
|
+
}
|
|
45
|
+
return (value) => {
|
|
46
|
+
let result = value;
|
|
47
|
+
if (options.normalize !== undefined) {
|
|
48
|
+
result = result.normalize(options.normalize);
|
|
49
|
+
}
|
|
50
|
+
if (options.collapseWhitespace === true) {
|
|
51
|
+
result = result.replace(/\s+/gu, ' ');
|
|
52
|
+
}
|
|
53
|
+
if (options.stripWhitespace === true) {
|
|
54
|
+
result = result.replace(/\s+/gu, '');
|
|
55
|
+
}
|
|
56
|
+
if (options.trim === true) {
|
|
57
|
+
result = result.trim();
|
|
58
|
+
}
|
|
59
|
+
if (options.lowercase === true) {
|
|
60
|
+
result = result.toLowerCase();
|
|
61
|
+
}
|
|
62
|
+
if (options.uppercase === true) {
|
|
63
|
+
result = result.toUpperCase();
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Create a schema that normalizes the input, validates it, then wraps it.
|
|
70
|
+
*
|
|
71
|
+
* A non-string input is passed through untouched, so the inner string schema
|
|
72
|
+
* reports the usual `invalid_type` issue rather than the preprocessor throwing
|
|
73
|
+
* on it.
|
|
74
|
+
*
|
|
75
|
+
* The inner schema is any string-to-string schema, not specifically a
|
|
76
|
+
* `z.ZodString`, so that Zod 4's format constructors work: `z.email()` is a
|
|
77
|
+
* `ZodEmail` and is not a `ZodString`.
|
|
78
|
+
*
|
|
79
|
+
* @typeParam C - The sensitivity class for the resulting SensitiveString
|
|
80
|
+
* @param klass - The sensitivity classification to apply
|
|
81
|
+
* @param spec - Normalization options, a function, or a list of functions
|
|
82
|
+
* @param label - Optional label for audit/compliance
|
|
83
|
+
* @param schema - Optional string-to-string schema, validated after normalization
|
|
84
|
+
* @returns A schema that transforms normalized strings to SensitiveString<C>
|
|
85
|
+
* @throws TypeError If the spec is malformed.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* const email = zSensitiveStringPreprocess(
|
|
90
|
+
* 'sensitive',
|
|
91
|
+
* { trim: true, lowercase: true },
|
|
92
|
+
* 'email',
|
|
93
|
+
* z.email()
|
|
94
|
+
* );
|
|
95
|
+
*
|
|
96
|
+
* email.parse(' Alice@Example.COM ').reveal(); // 'alice@example.com'
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
function zSensitiveStringPreprocess(klass, spec, label, schema) {
|
|
100
|
+
const normalize = compileSpec(spec);
|
|
101
|
+
const built = zod_1.z
|
|
102
|
+
.preprocess((value) => (typeof value === 'string' ? normalize(value) : value), schema ?? zod_1.z.string())
|
|
103
|
+
.transform((value) => new sensitive_string_core_1.SensitiveString(value, klass, label));
|
|
104
|
+
// `z.preprocess` declares its input as a required `unknown`, which is wider
|
|
105
|
+
// than the string this schema actually asks for and would let a caller pass
|
|
106
|
+
// anything without a compile error. The cast narrows it to that string.
|
|
107
|
+
//
|
|
108
|
+
// Under v3 the same cast fixed a different problem: `undefined` extends
|
|
109
|
+
// `unknown`, so the declared input marked the field optional wherever this
|
|
110
|
+
// schema sat in an object. v4 closed that, and the runtime case pinning it
|
|
111
|
+
// still passes.
|
|
112
|
+
return (0, refine_js_1.withSensitiveMethods)(built);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Normalize, then wrap as sensitive data.
|
|
116
|
+
* Shorthand for zSensitiveStringPreprocess('sensitive', spec, label, schema).
|
|
117
|
+
*
|
|
118
|
+
* @param spec - Normalization options, a function, or a list of functions
|
|
119
|
+
* @param label - Optional label for audit/compliance
|
|
120
|
+
* @param schema - Optional string-to-string schema, validated after normalization
|
|
121
|
+
* @returns A schema producing SensitiveString<'sensitive'>
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* const schema = zSensitivePreprocess({ trim: true, lowercase: true }, 'email');
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
function zSensitivePreprocess(spec, label, schema) {
|
|
129
|
+
return zSensitiveStringPreprocess('sensitive', spec, label, schema);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Normalize, then wrap as a credential.
|
|
133
|
+
* Shorthand for zSensitiveStringPreprocess('credential', spec, label, schema).
|
|
134
|
+
*
|
|
135
|
+
* @param spec - Normalization options, a function, or a list of functions
|
|
136
|
+
* @param label - Optional label for audit/compliance
|
|
137
|
+
* @param schema - Optional string-to-string schema, validated after normalization
|
|
138
|
+
* @returns A schema producing SensitiveString<'credential'>
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* // An API key pasted from a console arrives with line breaks in it.
|
|
143
|
+
* const apiKey = zCredentialPreprocess({ stripWhitespace: true }, 'api_key');
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
function zCredentialPreprocess(spec, label, schema) {
|
|
147
|
+
return zSensitiveStringPreprocess('credential', spec, label, schema);
|
|
148
|
+
}
|
|
149
|
+
//# sourceMappingURL=preprocessing.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preprocessing.js","sourceRoot":"","sources":["../src/preprocessing.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG;;AAmHH,gEAwBC;AAgBD,oDAMC;AAiBD,sDAMC;AAtLD,6BAAwB;AACxB,+EAAwF;AACxF,2CAAmD;AA+BnD;;;;GAIG;AACH,SAAS,WAAW,CAAC,IAAoB;IACvC,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE,CAAC;QAC/B,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,MAAM,KAAK,GAAG,IAAgD,CAAC;QAC/D,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,IAAI,OAAO,IAAI,KAAK,UAAU,EAAE,CAAC;gBAC/B,MAAM,IAAI,SAAS,CAAC,oCAAoC,CAAC,CAAC;YAC5D,CAAC;QACH,CAAC;QACD,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;IAClE,CAAC;IAED,MAAM,OAAO,GAAG,IAAyB,CAAC;IAC1C,IAAI,OAAO,CAAC,SAAS,KAAK,IAAI,IAAI,OAAO,CAAC,SAAS,KAAK,IAAI,EAAE,CAAC;QAC7D,MAAM,IAAI,SAAS,CAAC,4DAA4D,CAAC,CAAC;IACpF,CAAC;IAED,OAAO,CAAC,KAAK,EAAE,EAAE;QACf,IAAI,MAAM,GAAG,KAAK,CAAC;QACnB,IAAI,OAAO,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;YACpC,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAC/C,CAAC;QACD,IAAI,OAAO,CAAC,kBAAkB,KAAK,IAAI,EAAE,CAAC;YACxC,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;QACxC,CAAC;QACD,IAAI,OAAO,CAAC,eAAe,KAAK,IAAI,EAAE,CAAC;YACrC,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;QACvC,CAAC;QACD,IAAI,OAAO,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;YAC1B,MAAM,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;QACzB,CAAC;QACD,IAAI,OAAO,CAAC,SAAS,KAAK,IAAI,EAAE,CAAC;YAC/B,MAAM,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC;QAChC,CAAC;QACD,IAAI,OAAO,CAAC,SAAS,KAAK,IAAI,EAAE,CAAC;YAC/B,MAAM,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC;QAChC,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,SAAgB,0BAA0B,CACxC,KAAQ,EACR,IAAoB,EACpB,KAAc,EACd,MAAkC;IAElC,MAAM,SAAS,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;IAEpC,MAAM,KAAK,GAAG,OAAC;SACZ,UAAU,CACT,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,EACjE,MAAM,IAAI,OAAC,CAAC,MAAM,EAAE,CACrB;SACA,SAAS,CAAC,CAAC,KAAK,EAAsB,EAAE,CAAC,IAAI,uCAAe,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;IAEtF,4EAA4E;IAC5E,4EAA4E;IAC5E,wEAAwE;IACxE,EAAE;IACF,wEAAwE;IACxE,2EAA2E;IAC3E,2EAA2E;IAC3E,gBAAgB;IAChB,OAAO,IAAA,gCAAoB,EAAC,KAAyD,CAAC,CAAC;AACzF,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,SAAgB,oBAAoB,CAClC,IAAoB,EACpB,KAAc,EACd,MAAkC;IAElC,OAAO,0BAA0B,CAAC,WAAW,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AACtE,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,qBAAqB,CACnC,IAAoB,EACpB,KAAc,EACd,MAAkC;IAElC,OAAO,0BAA0B,CAAC,YAAY,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AACvE,CAAC"}
|
package/dist/refine.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Refinements that need the raw value.
|
|
3
|
+
*
|
|
4
|
+
* A refinement that checks a password's shape, or an API key's prefix, has to
|
|
5
|
+
* see the string. Running it before wrapping would hide that read from the
|
|
6
|
+
* audit trail, so these helpers run after wrapping and read through
|
|
7
|
+
* `reveal()`: the same authorization and the same audit hook as any other
|
|
8
|
+
* reveal in the codebase.
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
import { z } from 'zod';
|
|
13
|
+
import type { SensitiveString, SensitivityClass } from '@platform-sec/sensitive-string-core';
|
|
14
|
+
import type { RefineSensitiveParams, SensitiveCheck, SensitiveSchema } from './types.js';
|
|
15
|
+
/**
|
|
16
|
+
* Refine a schema that produces a SensitiveString, using the raw value.
|
|
17
|
+
*
|
|
18
|
+
* The predicate receives the revealed string. That read goes through
|
|
19
|
+
* `reveal()`, so an ambient `RevealContext` authorizes it and its
|
|
20
|
+
* `auditReveal` hook sees it with `operation: 'reveal'`. Pass
|
|
21
|
+
* `params.context` to audit the refinement under a context of its own.
|
|
22
|
+
*
|
|
23
|
+
* @typeParam C - The sensitivity class the schema produces
|
|
24
|
+
* @typeParam Input - The schema's input type
|
|
25
|
+
* @param schema - A schema whose output is a SensitiveString
|
|
26
|
+
* @param check - Predicate over the raw value; `false` produces an issue
|
|
27
|
+
* @param params - Message, path, and reveal context for the check
|
|
28
|
+
* @returns A schema producing the same value, refined
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const password = refineSensitive(
|
|
33
|
+
* zCredential('password'),
|
|
34
|
+
* (raw) => /[A-Z]/.test(raw),
|
|
35
|
+
* { message: 'Password must contain an uppercase letter' }
|
|
36
|
+
* );
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* An async check requires `parseAsync`. Zod throws `$ZodAsyncError` rather
|
|
41
|
+
* than skipping it if it is reached from a synchronous `parse()`.
|
|
42
|
+
*
|
|
43
|
+
* A reveal the policy refuses, or one denied by a lapsed deadline, throws out
|
|
44
|
+
* of the parse rather than becoming an issue, and that is deliberate. An issue
|
|
45
|
+
* says the input was wrong. A refused reveal says this process may not read a
|
|
46
|
+
* value it holds, which is a configuration or lifetime problem; reporting it as
|
|
47
|
+
* a validation failure would tell a client its payload was bad and hide the
|
|
48
|
+
* real cause. This is the opposite call from `zSensitiveDeserialize`, whose
|
|
49
|
+
* failures really are the payload's fault and so are reported as issues.
|
|
50
|
+
*/
|
|
51
|
+
export declare function refineSensitive<C extends SensitivityClass, Input>(schema: z.ZodType<SensitiveString<C>, Input>, check: SensitiveCheck, params?: RefineSensitiveParams): SensitiveSchema<C, Input>;
|
|
52
|
+
/**
|
|
53
|
+
* Attach `refineSensitive` to a schema this package builds.
|
|
54
|
+
*
|
|
55
|
+
* The method is non-enumerable so that it does not show up when a schema is
|
|
56
|
+
* spread, inspected, or serialized by tooling that walks own properties.
|
|
57
|
+
*
|
|
58
|
+
* @internal
|
|
59
|
+
*/
|
|
60
|
+
export declare function withSensitiveMethods<C extends SensitivityClass, Input>(schema: z.ZodType<SensitiveString<C>, Input>): SensitiveSchema<C, Input>;
|
|
61
|
+
//# sourceMappingURL=refine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refine.d.ts","sourceRoot":"","sources":["../src/refine.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAC7F,OAAO,KAAK,EACV,qBAAqB,EACrB,cAAc,EACd,eAAe,EAEhB,MAAM,YAAY,CAAC;AA0CpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,wBAAgB,eAAe,CAAC,CAAC,SAAS,gBAAgB,EAAE,KAAK,EAC/D,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAC5C,KAAK,EAAE,cAAc,EACrB,MAAM,CAAC,EAAE,qBAAqB,GAC7B,eAAe,CAAC,CAAC,EAAE,KAAK,CAAC,CAoB3B;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,SAAS,gBAAgB,EAAE,KAAK,EACpE,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,GAC3C,eAAe,CAAC,CAAC,EAAE,KAAK,CAAC,CAY3B"}
|
package/dist/refine.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Refinements that need the raw value.
|
|
4
|
+
*
|
|
5
|
+
* A refinement that checks a password's shape, or an API key's prefix, has to
|
|
6
|
+
* see the string. Running it before wrapping would hide that read from the
|
|
7
|
+
* audit trail, so these helpers run after wrapping and read through
|
|
8
|
+
* `reveal()`: the same authorization and the same audit hook as any other
|
|
9
|
+
* reveal in the codebase.
|
|
10
|
+
*
|
|
11
|
+
* @packageDocumentation
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.refineSensitive = refineSensitive;
|
|
15
|
+
exports.withSensitiveMethods = withSensitiveMethods;
|
|
16
|
+
/**
|
|
17
|
+
* The message used when a check fails and the caller supplied none.
|
|
18
|
+
*
|
|
19
|
+
* Deliberately says nothing about the value, not even its length.
|
|
20
|
+
*/
|
|
21
|
+
const DEFAULT_REFINE_MESSAGE = 'Invalid value';
|
|
22
|
+
/**
|
|
23
|
+
* Build the issue for a failed check.
|
|
24
|
+
*
|
|
25
|
+
* `params.message` is the only caller-controlled text that reaches the issue.
|
|
26
|
+
* Nothing derived from the raw value is added here, and a caller who
|
|
27
|
+
* interpolates the value into their own message is the one leaking it.
|
|
28
|
+
*/
|
|
29
|
+
function refinementIssue(params) {
|
|
30
|
+
const issue = {
|
|
31
|
+
code: 'custom',
|
|
32
|
+
message: params?.message ?? DEFAULT_REFINE_MESSAGE,
|
|
33
|
+
};
|
|
34
|
+
if (params?.path !== undefined) {
|
|
35
|
+
issue.path = params.path;
|
|
36
|
+
}
|
|
37
|
+
if (params?.fatal !== undefined) {
|
|
38
|
+
issue.fatal = params.fatal;
|
|
39
|
+
}
|
|
40
|
+
return issue;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Refine a schema that produces a SensitiveString, using the raw value.
|
|
44
|
+
*
|
|
45
|
+
* The predicate receives the revealed string. That read goes through
|
|
46
|
+
* `reveal()`, so an ambient `RevealContext` authorizes it and its
|
|
47
|
+
* `auditReveal` hook sees it with `operation: 'reveal'`. Pass
|
|
48
|
+
* `params.context` to audit the refinement under a context of its own.
|
|
49
|
+
*
|
|
50
|
+
* @typeParam C - The sensitivity class the schema produces
|
|
51
|
+
* @typeParam Input - The schema's input type
|
|
52
|
+
* @param schema - A schema whose output is a SensitiveString
|
|
53
|
+
* @param check - Predicate over the raw value; `false` produces an issue
|
|
54
|
+
* @param params - Message, path, and reveal context for the check
|
|
55
|
+
* @returns A schema producing the same value, refined
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```ts
|
|
59
|
+
* const password = refineSensitive(
|
|
60
|
+
* zCredential('password'),
|
|
61
|
+
* (raw) => /[A-Z]/.test(raw),
|
|
62
|
+
* { message: 'Password must contain an uppercase letter' }
|
|
63
|
+
* );
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @remarks
|
|
67
|
+
* An async check requires `parseAsync`. Zod throws `$ZodAsyncError` rather
|
|
68
|
+
* than skipping it if it is reached from a synchronous `parse()`.
|
|
69
|
+
*
|
|
70
|
+
* A reveal the policy refuses, or one denied by a lapsed deadline, throws out
|
|
71
|
+
* of the parse rather than becoming an issue, and that is deliberate. An issue
|
|
72
|
+
* says the input was wrong. A refused reveal says this process may not read a
|
|
73
|
+
* value it holds, which is a configuration or lifetime problem; reporting it as
|
|
74
|
+
* a validation failure would tell a client its payload was bad and hide the
|
|
75
|
+
* real cause. This is the opposite call from `zSensitiveDeserialize`, whose
|
|
76
|
+
* failures really are the payload's fault and so are reported as issues.
|
|
77
|
+
*/
|
|
78
|
+
function refineSensitive(schema, check, params) {
|
|
79
|
+
const refined = schema.superRefine((value, ctx) => {
|
|
80
|
+
const rawValue = value.reveal(params?.context);
|
|
81
|
+
const outcome = check(rawValue);
|
|
82
|
+
if (outcome instanceof Promise) {
|
|
83
|
+
return outcome.then((passed) => {
|
|
84
|
+
if (!passed) {
|
|
85
|
+
ctx.addIssue(refinementIssue(params));
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (!outcome) {
|
|
90
|
+
ctx.addIssue(refinementIssue(params));
|
|
91
|
+
}
|
|
92
|
+
return;
|
|
93
|
+
});
|
|
94
|
+
return withSensitiveMethods(refined);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Attach `refineSensitive` to a schema this package builds.
|
|
98
|
+
*
|
|
99
|
+
* The method is non-enumerable so that it does not show up when a schema is
|
|
100
|
+
* spread, inspected, or serialized by tooling that walks own properties.
|
|
101
|
+
*
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
function withSensitiveMethods(schema) {
|
|
105
|
+
const refine = (check, params) => refineSensitive(schema, check, params);
|
|
106
|
+
Object.defineProperty(schema, 'refineSensitive', {
|
|
107
|
+
value: refine,
|
|
108
|
+
enumerable: false,
|
|
109
|
+
writable: false,
|
|
110
|
+
configurable: true,
|
|
111
|
+
});
|
|
112
|
+
return schema;
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=refine.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refine.js","sourceRoot":"","sources":["../src/refine.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;AAuFH,0CAwBC;AAUD,oDAcC;AA5HD;;;;GAIG;AACH,MAAM,sBAAsB,GAAG,eAAe,CAAC;AAE/C;;;;;;GAMG;AACH,SAAS,eAAe,CAAC,MAAyC;IAMhE,MAAM,KAAK,GAKP;QACF,IAAI,EAAE,QAAQ;QACd,OAAO,EAAE,MAAM,EAAE,OAAO,IAAI,sBAAsB;KACnD,CAAC;IAEF,IAAI,MAAM,EAAE,IAAI,KAAK,SAAS,EAAE,CAAC;QAC/B,KAAK,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;IAC3B,CAAC;IACD,IAAI,MAAM,EAAE,KAAK,KAAK,SAAS,EAAE,CAAC;QAChC,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;IAC7B,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,SAAgB,eAAe,CAC7B,MAA4C,EAC5C,KAAqB,EACrB,MAA8B;IAE9B,MAAM,OAAO,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,KAAK,EAAE,GAAG,EAAE,EAAE;QAChD,MAAM,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QAC/C,MAAM,OAAO,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC;QAEhC,IAAI,OAAO,YAAY,OAAO,EAAE,CAAC;YAC/B,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE;gBAC7B,IAAI,CAAC,MAAM,EAAE,CAAC;oBACZ,GAAG,CAAC,QAAQ,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC;gBACxC,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;QAED,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,GAAG,CAAC,QAAQ,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC,CAAC;QACxC,CAAC;QACD,OAAO;IACT,CAAC,CAAC,CAAC;IAEH,OAAO,oBAAoB,CAAC,OAAO,CAAC,CAAC;AACvC,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,oBAAoB,CAClC,MAA4C;IAE5C,MAAM,MAAM,GAAwD,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,CACpF,eAAe,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;IAEzC,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,iBAAiB,EAAE;QAC/C,KAAK,EAAE,MAAM;QACb,UAAU,EAAE,KAAK;QACjB,QAAQ,EAAE,KAAK;QACf,YAAY,EAAE,IAAI;KACnB,CAAC,CAAC;IAEH,OAAO,MAAmC,CAAC;AAC7C,CAAC"}
|