@platform-sec/sensitive-string-zod 0.2.0 → 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/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 +11 -11
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sensitivity that depends on a sibling field.
|
|
3
|
+
*
|
|
4
|
+
* Some payloads carry a value whose classification is decided by another field
|
|
5
|
+
* in the same object: a settings row that is a secret only when its `type` says
|
|
6
|
+
* so. Wrapping that after the parse means the raw string exists, unwrapped, for
|
|
7
|
+
* as long as it takes to look at the discriminator. This builds the decision
|
|
8
|
+
* into the schema instead.
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
import { z } from 'zod';
|
|
13
|
+
import type { SensitiveString, SensitivityClass } from '@platform-sec/sensitive-string-core';
|
|
14
|
+
/**
|
|
15
|
+
* How to classify the value for one discriminator case. `null` leaves the
|
|
16
|
+
* value a plain string.
|
|
17
|
+
*/
|
|
18
|
+
export interface ConditionalSensitivity {
|
|
19
|
+
/** The sensitivity classification to apply for this case. */
|
|
20
|
+
klass: SensitivityClass;
|
|
21
|
+
/** Optional label for audit/compliance. */
|
|
22
|
+
label?: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The map from discriminator value to classification.
|
|
26
|
+
*/
|
|
27
|
+
export type ConditionalConditions = Readonly<Record<string, ConditionalSensitivity | null>>;
|
|
28
|
+
/**
|
|
29
|
+
* The value type produced for one case: wrapped when the case names a class,
|
|
30
|
+
* a plain string when it is `null`.
|
|
31
|
+
*/
|
|
32
|
+
type ConditionalValue<T> = T extends {
|
|
33
|
+
klass: infer C extends SensitivityClass;
|
|
34
|
+
} ? SensitiveString<C> : string;
|
|
35
|
+
/**
|
|
36
|
+
* The discriminated union produced by {@link zConditionalSensitive}.
|
|
37
|
+
*/
|
|
38
|
+
export type ConditionalSensitiveOutput<D extends string, V extends string, M extends ConditionalConditions> = {
|
|
39
|
+
[K in keyof M & string]: {
|
|
40
|
+
[P in D]: K;
|
|
41
|
+
} & {
|
|
42
|
+
[P in V]: ConditionalValue<M[K]>;
|
|
43
|
+
};
|
|
44
|
+
}[keyof M & string];
|
|
45
|
+
/**
|
|
46
|
+
* The accepted input: the discriminator plus a string value.
|
|
47
|
+
*/
|
|
48
|
+
export type ConditionalSensitiveInput<D extends string, V extends string, M extends ConditionalConditions> = {
|
|
49
|
+
[P in D]: keyof M & string;
|
|
50
|
+
} & {
|
|
51
|
+
[P in V]: string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Configuration for {@link zConditionalSensitive}.
|
|
55
|
+
*/
|
|
56
|
+
export interface ConditionalSensitiveConfig<D extends string, V extends string, M extends ConditionalConditions> {
|
|
57
|
+
/** The field naming the case. */
|
|
58
|
+
discriminator: D;
|
|
59
|
+
/** The field holding the value to classify. */
|
|
60
|
+
valueField: V;
|
|
61
|
+
/** One entry per accepted discriminator value. */
|
|
62
|
+
conditions: M;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Build a discriminated union that classifies one field according to another.
|
|
66
|
+
*
|
|
67
|
+
* The result is a plain `z.discriminatedUnion`, so an unknown discriminator is
|
|
68
|
+
* rejected before the value is looked at, and the value never exists unwrapped
|
|
69
|
+
* on a case that classifies it.
|
|
70
|
+
*
|
|
71
|
+
* The generated objects hold exactly the two fields. A payload with more
|
|
72
|
+
* fields than that is better written as a `z.discriminatedUnion` over
|
|
73
|
+
* hand-written branches; `zSensitive` and `zCredential` work as object fields
|
|
74
|
+
* there with nothing extra.
|
|
75
|
+
*
|
|
76
|
+
* @typeParam D - The discriminator field name
|
|
77
|
+
* @typeParam V - The value field name
|
|
78
|
+
* @typeParam M - The conditions map
|
|
79
|
+
* @param config - Discriminator, value field, and one entry per case
|
|
80
|
+
* @returns A discriminated union schema
|
|
81
|
+
* @throws TypeError If `conditions` is empty, or the two field names are the same.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* const Setting = zConditionalSensitive({
|
|
86
|
+
* discriminator: 'type',
|
|
87
|
+
* valueField: 'value',
|
|
88
|
+
* conditions: {
|
|
89
|
+
* secret: { klass: 'credential', label: 'secret_data' },
|
|
90
|
+
* private: { klass: 'sensitive', label: 'private_data' },
|
|
91
|
+
* public: null,
|
|
92
|
+
* },
|
|
93
|
+
* });
|
|
94
|
+
*
|
|
95
|
+
* Setting.parse({ type: 'secret', value: 'sk-live-x' }); // value is SensitiveString
|
|
96
|
+
* Setting.parse({ type: 'public', value: 'motd' }); // value is a string
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
export declare function zConditionalSensitive<D extends string, V extends string, M extends ConditionalConditions>(config: ConditionalSensitiveConfig<D, V, M>): z.ZodType<ConditionalSensitiveOutput<D, V, M>, ConditionalSensitiveInput<D, V, M>>;
|
|
100
|
+
export {};
|
|
101
|
+
//# sourceMappingURL=conditional.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conditional.d.ts","sourceRoot":"","sources":["../src/conditional.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAG7F;;;GAGG;AACH,MAAM,WAAW,sBAAsB;IACrC,6DAA6D;IAC7D,KAAK,EAAE,gBAAgB,CAAC;IACxB,2CAA2C;IAC3C,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,sBAAsB,GAAG,IAAI,CAAC,CAAC,CAAC;AAE5F;;;GAGG;AACH,KAAK,gBAAgB,CAAC,CAAC,IAAI,CAAC,SAAS;IAAE,KAAK,EAAE,MAAM,CAAC,SAAS,gBAAgB,CAAA;CAAE,GAC5E,eAAe,CAAC,CAAC,CAAC,GAClB,MAAM,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,0BAA0B,CACpC,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,qBAAqB,IAC7B;KACD,CAAC,IAAI,MAAM,CAAC,GAAG,MAAM,GAAG;SAAG,CAAC,IAAI,CAAC,GAAG,CAAC;KAAE,GAAG;SAAG,CAAC,IAAI,CAAC,GAAG,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KAAE;CAChF,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC;AAEpB;;GAEG;AACH,MAAM,MAAM,yBAAyB,CACnC,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,qBAAqB,IAC7B;KAAG,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,GAAG,MAAM;CAAE,GAAG;KAAG,CAAC,IAAI,CAAC,GAAG,MAAM;CAAE,CAAC;AAE1D;;GAEG;AACH,MAAM,WAAW,0BAA0B,CACzC,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,qBAAqB;IAE/B,iCAAiC;IACjC,aAAa,EAAE,CAAC,CAAC;IACjB,+CAA+C;IAC/C,UAAU,EAAE,CAAC,CAAC;IACd,kDAAkD;IAClD,UAAU,EAAE,CAAC,CAAC;CACf;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,qBAAqB,CACnC,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,qBAAqB,EAE/B,MAAM,EAAE,0BAA0B,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,GAC1C,CAAC,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,yBAAyB,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAgDpF"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Sensitivity that depends on a sibling field.
|
|
4
|
+
*
|
|
5
|
+
* Some payloads carry a value whose classification is decided by another field
|
|
6
|
+
* in the same object: a settings row that is a secret only when its `type` says
|
|
7
|
+
* so. Wrapping that after the parse means the raw string exists, unwrapped, for
|
|
8
|
+
* as long as it takes to look at the discriminator. This builds the decision
|
|
9
|
+
* into the schema instead.
|
|
10
|
+
*
|
|
11
|
+
* @packageDocumentation
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.zConditionalSensitive = zConditionalSensitive;
|
|
15
|
+
const zod_1 = require("zod");
|
|
16
|
+
const base_js_1 = require("./base.js");
|
|
17
|
+
/**
|
|
18
|
+
* Build a discriminated union that classifies one field according to another.
|
|
19
|
+
*
|
|
20
|
+
* The result is a plain `z.discriminatedUnion`, so an unknown discriminator is
|
|
21
|
+
* rejected before the value is looked at, and the value never exists unwrapped
|
|
22
|
+
* on a case that classifies it.
|
|
23
|
+
*
|
|
24
|
+
* The generated objects hold exactly the two fields. A payload with more
|
|
25
|
+
* fields than that is better written as a `z.discriminatedUnion` over
|
|
26
|
+
* hand-written branches; `zSensitive` and `zCredential` work as object fields
|
|
27
|
+
* there with nothing extra.
|
|
28
|
+
*
|
|
29
|
+
* @typeParam D - The discriminator field name
|
|
30
|
+
* @typeParam V - The value field name
|
|
31
|
+
* @typeParam M - The conditions map
|
|
32
|
+
* @param config - Discriminator, value field, and one entry per case
|
|
33
|
+
* @returns A discriminated union schema
|
|
34
|
+
* @throws TypeError If `conditions` is empty, or the two field names are the same.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const Setting = zConditionalSensitive({
|
|
39
|
+
* discriminator: 'type',
|
|
40
|
+
* valueField: 'value',
|
|
41
|
+
* conditions: {
|
|
42
|
+
* secret: { klass: 'credential', label: 'secret_data' },
|
|
43
|
+
* private: { klass: 'sensitive', label: 'private_data' },
|
|
44
|
+
* public: null,
|
|
45
|
+
* },
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* Setting.parse({ type: 'secret', value: 'sk-live-x' }); // value is SensitiveString
|
|
49
|
+
* Setting.parse({ type: 'public', value: 'motd' }); // value is a string
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function zConditionalSensitive(config) {
|
|
53
|
+
// Compared as plain strings: inside the function `D` and `V` are unresolved
|
|
54
|
+
// type parameters, and tsc reads a direct comparison of two of those as
|
|
55
|
+
// having no overlap.
|
|
56
|
+
const discriminator = config.discriminator;
|
|
57
|
+
const valueField = config.valueField;
|
|
58
|
+
if (discriminator === valueField) {
|
|
59
|
+
// The two names become keys of the same object literal, so the second
|
|
60
|
+
// would overwrite the first and the value field would vanish without a
|
|
61
|
+
// word. A schema that silently stops classifying anything is the worst
|
|
62
|
+
// failure mode this helper has.
|
|
63
|
+
throw new TypeError('zConditionalSensitive requires distinct discriminator and valueField names');
|
|
64
|
+
}
|
|
65
|
+
const entries = Object.entries(config.conditions);
|
|
66
|
+
if (entries.length === 0) {
|
|
67
|
+
throw new TypeError('zConditionalSensitive requires at least one condition');
|
|
68
|
+
}
|
|
69
|
+
const options = entries.map(([discriminatorValue, sensitivity]) => zod_1.z.object({
|
|
70
|
+
[config.discriminator]: zod_1.z.literal(discriminatorValue),
|
|
71
|
+
[config.valueField]: sensitivity == null ? zod_1.z.string() : (0, base_js_1.zSensitiveString)(sensitivity.klass, sensitivity.label),
|
|
72
|
+
}));
|
|
73
|
+
// The branches are built from a runtime map, so their literal types are not
|
|
74
|
+
// recoverable by inference. The declared return type states the union those
|
|
75
|
+
// branches produce, and the two casts are the seam between the two.
|
|
76
|
+
//
|
|
77
|
+
// The first is on `options`, because a runtime-built array does not satisfy
|
|
78
|
+
// the non-empty tuple `z.discriminatedUnion` asks for. Erasing the branch
|
|
79
|
+
// types to `$ZodTypeDiscriminable` is what makes the second necessary: v4
|
|
80
|
+
// infers the union's output from its option tuple, so an erased tuple infers
|
|
81
|
+
// `unknown` and the result has to be re-stated.
|
|
82
|
+
const union = zod_1.z.discriminatedUnion(config.discriminator, options);
|
|
83
|
+
return union;
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=conditional.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conditional.js","sourceRoot":"","sources":["../src/conditional.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;AAqGH,sDAsDC;AAzJD,6BAAwB;AAExB,uC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,CACxF,CAAC;IAEF,OAAO,KAGN,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default values for sensitive fields.
|
|
3
|
+
*
|
|
4
|
+
* The default is produced as a raw string and wrapped by the schema, so a
|
|
5
|
+
* caller never has to build a `SensitiveString` to describe a fallback, and a
|
|
6
|
+
* generated default is classified the same way a parsed one is.
|
|
7
|
+
*
|
|
8
|
+
* @packageDocumentation
|
|
9
|
+
*/
|
|
10
|
+
import { SensitivityClass } from '@platform-sec/sensitive-string-core';
|
|
11
|
+
import type { SensitiveSchema } from './types.js';
|
|
12
|
+
/**
|
|
13
|
+
* Create a schema that falls back to a default when the field is absent.
|
|
14
|
+
*
|
|
15
|
+
* A function default is called lazily, once per parse that needs it, so a
|
|
16
|
+
* generated token is not shared between parses and a default read from the
|
|
17
|
+
* environment is read at parse time rather than at schema construction.
|
|
18
|
+
*
|
|
19
|
+
* `undefined` triggers the default; `null` and `''` do not. They are values a
|
|
20
|
+
* client sent, and treating them as absent would silently substitute a secret
|
|
21
|
+
* for input that should have been rejected.
|
|
22
|
+
*
|
|
23
|
+
* The fallback is held in the transform's closure rather than in a
|
|
24
|
+
* `.default()`, and that placement is the security-relevant part.
|
|
25
|
+
* `z.toJSONSchema(schema, { io: 'input' })` reads a schema definition's
|
|
26
|
+
* `default` and emits it verbatim, calling a function default to do so; every
|
|
27
|
+
* OpenAPI generator in the ecosystem takes that path. The documented example
|
|
28
|
+
* below reads `process.env.API_KEY`, so under a `.default()` the live key
|
|
29
|
+
* would be embedded in a document whose whole purpose is publication. A
|
|
30
|
+
* closure is not part of the definition and has nothing to read.
|
|
31
|
+
*
|
|
32
|
+
* @typeParam C - The sensitivity class for the resulting SensitiveString
|
|
33
|
+
* @param klass - The sensitivity classification to apply
|
|
34
|
+
* @param label - Optional label for audit/compliance
|
|
35
|
+
* @param defaultValue - The fallback string, or a function producing one
|
|
36
|
+
* @returns A schema producing SensitiveString<C>, with the field optional
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* const Config = z.object({
|
|
41
|
+
* apiKey: zSensitiveDefault('credential', 'api_key', () => process.env.API_KEY ?? ''),
|
|
42
|
+
* });
|
|
43
|
+
*
|
|
44
|
+
* Config.parse({}).apiKey; // SensitiveString<'credential'>
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
export declare function zSensitiveDefault<C extends SensitivityClass>(klass: C, label: string | undefined, defaultValue: string | (() => string)): SensitiveSchema<C, string | undefined>;
|
|
48
|
+
//# sourceMappingURL=defaults.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAGH,OAAO,EAAmB,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAExF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,gBAAgB,EAC1D,KAAK,EAAE,CAAC,EACR,KAAK,EAAE,MAAM,GAAG,SAAS,EACzB,YAAY,EAAE,MAAM,GAAG,CAAC,MAAM,MAAM,CAAC,GACpC,eAAe,CAAC,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC,CAUxC"}
|
package/dist/defaults.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Default values for sensitive fields.
|
|
4
|
+
*
|
|
5
|
+
* The default is produced as a raw string and wrapped by the schema, so a
|
|
6
|
+
* caller never has to build a `SensitiveString` to describe a fallback, and a
|
|
7
|
+
* generated default is classified the same way a parsed one is.
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.zSensitiveDefault = zSensitiveDefault;
|
|
13
|
+
const zod_1 = require("zod");
|
|
14
|
+
const sensitive_string_core_1 = require("@platform-sec/sensitive-string-core");
|
|
15
|
+
const refine_js_1 = require("./refine.js");
|
|
16
|
+
/**
|
|
17
|
+
* Create a schema that falls back to a default when the field is absent.
|
|
18
|
+
*
|
|
19
|
+
* A function default is called lazily, once per parse that needs it, so a
|
|
20
|
+
* generated token is not shared between parses and a default read from the
|
|
21
|
+
* environment is read at parse time rather than at schema construction.
|
|
22
|
+
*
|
|
23
|
+
* `undefined` triggers the default; `null` and `''` do not. They are values a
|
|
24
|
+
* client sent, and treating them as absent would silently substitute a secret
|
|
25
|
+
* for input that should have been rejected.
|
|
26
|
+
*
|
|
27
|
+
* The fallback is held in the transform's closure rather than in a
|
|
28
|
+
* `.default()`, and that placement is the security-relevant part.
|
|
29
|
+
* `z.toJSONSchema(schema, { io: 'input' })` reads a schema definition's
|
|
30
|
+
* `default` and emits it verbatim, calling a function default to do so; every
|
|
31
|
+
* OpenAPI generator in the ecosystem takes that path. The documented example
|
|
32
|
+
* below reads `process.env.API_KEY`, so under a `.default()` the live key
|
|
33
|
+
* would be embedded in a document whose whole purpose is publication. A
|
|
34
|
+
* closure is not part of the definition and has nothing to read.
|
|
35
|
+
*
|
|
36
|
+
* @typeParam C - The sensitivity class for the resulting SensitiveString
|
|
37
|
+
* @param klass - The sensitivity classification to apply
|
|
38
|
+
* @param label - Optional label for audit/compliance
|
|
39
|
+
* @param defaultValue - The fallback string, or a function producing one
|
|
40
|
+
* @returns A schema producing SensitiveString<C>, with the field optional
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const Config = z.object({
|
|
45
|
+
* apiKey: zSensitiveDefault('credential', 'api_key', () => process.env.API_KEY ?? ''),
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* Config.parse({}).apiKey; // SensitiveString<'credential'>
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
function zSensitiveDefault(klass, label, defaultValue) {
|
|
52
|
+
return (0, refine_js_1.withSensitiveMethods)(zod_1.z
|
|
53
|
+
.string()
|
|
54
|
+
.optional()
|
|
55
|
+
.transform((value) => {
|
|
56
|
+
const raw = value ?? (typeof defaultValue === 'function' ? defaultValue() : defaultValue);
|
|
57
|
+
return new sensitive_string_core_1.SensitiveString(raw, klass, label);
|
|
58
|
+
}));
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=defaults.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaults.js","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;AA0CH,8CAcC;AAtDD,6BAAwB;AACxB,+EAAwF;AACxF,2CAAmD;AAGnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,SAAgB,iBAAiB,CAC/B,KAAQ,EACR,KAAyB,EACzB,YAAqC;IAErC,OAAO,IAAA,gCAAoB,EACzB,OAAC;SACE,MAAM,EAAE;SACR,QAAQ,EAAE;SACV,SAAS,CAAC,CAAC,KAAK,EAAsB,EAAE;QACvC,MAAM,GAAG,GAAG,KAAK,IAAI,CAAC,OAAO,YAAY,KAAK,UAAU,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC;QAC1F,OAAO,IAAI,uCAAe,CAAC,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IAChD,CAAC,CAAC,CACL,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Keeping the input out of validation errors.
|
|
3
|
+
*
|
|
4
|
+
* Most of Zod's built-in issues report the rule, not the string that failed it,
|
|
5
|
+
* and Zod 4 closed two leaks v3 had: `invalid_enum_value` and `invalid_literal`
|
|
6
|
+
* are both `invalid_value` now, which carries the *allowed* set rather than
|
|
7
|
+
* what arrived. What is left, measured against 4.5.4 with a canary:
|
|
8
|
+
*
|
|
9
|
+
* - `unrecognized_keys` puts unknown key names in `keys` and in the message.
|
|
10
|
+
* - `custom` carries whatever a `superRefine` caller attached to `params`.
|
|
11
|
+
* - `invalid_element` carries the offending map key in `key`.
|
|
12
|
+
* - every issue can carry `input`, the value itself. Zod drops it during
|
|
13
|
+
* finalization by default, but `{ reportInput: true }` in the parse options
|
|
14
|
+
* keeps it, and the raw issue an error function sees always has it.
|
|
15
|
+
* - `invalid_union`, `invalid_key`, and `invalid_element` nest issue lists, so
|
|
16
|
+
* any of the above can arrive one or more levels down.
|
|
17
|
+
*
|
|
18
|
+
* That splits the fix in two. An error map can only rewrite the message, so it
|
|
19
|
+
* handles what a caller sees in `error.message`. The `keys`, `params`, `key`,
|
|
20
|
+
* and `input` fields survive an error map untouched and reach anything that
|
|
21
|
+
* serializes the error, which is where structured logging picks them up. Use
|
|
22
|
+
* {@link sensitiveSafeParse} to get both, or {@link redactZodError} on an error
|
|
23
|
+
* you already hold.
|
|
24
|
+
*
|
|
25
|
+
* What is deliberately *not* covered is `issue.path`. A container keyed by a
|
|
26
|
+
* sensitive value puts that value in the path of every issue below it. Redacting
|
|
27
|
+
* it would collapse every field of a multi-issue error into one bucket and make
|
|
28
|
+
* `z.treeifyError` useless, so the answer is not to key a container by a secret:
|
|
29
|
+
* put it in the value position, which is what `zSensitiveRecord` does.
|
|
30
|
+
*
|
|
31
|
+
* One thing this module does not attempt at all: the guarantee is about the
|
|
32
|
+
* *input*, not about values a schema author embedded in their own schema.
|
|
33
|
+
* `invalid_value.values` echoes what `z.literal()` and `z.enum()` were given,
|
|
34
|
+
* and `invalid_format` echoes the argument to `startsWith`, `endsWith`, and
|
|
35
|
+
* `includes`. Those read as safe in the table above, and they are, with respect
|
|
36
|
+
* to the input. `z.literal(secret)` still puts the secret in every issue it
|
|
37
|
+
* produces, and there is nothing here that could tell it apart from a legitimate
|
|
38
|
+
* allowed value. Do not build a schema out of a secret.
|
|
39
|
+
*
|
|
40
|
+
* @packageDocumentation
|
|
41
|
+
*/
|
|
42
|
+
import { z } from 'zod';
|
|
43
|
+
/**
|
|
44
|
+
* Stand-in written over any value stripped from an issue.
|
|
45
|
+
*
|
|
46
|
+
* Matches the core library's redaction marker so that one grep over a log
|
|
47
|
+
* finds every kind of redaction the library performs.
|
|
48
|
+
*/
|
|
49
|
+
export declare const REDACTED_ISSUE_VALUE = "[REDACTED]";
|
|
50
|
+
/**
|
|
51
|
+
* The parse options accepted by the helpers in this module.
|
|
52
|
+
*
|
|
53
|
+
* Zod 4 renamed the per-call error map from `errorMap` to `error` and does not
|
|
54
|
+
* reject unknown keys, so a v3 spelling would be accepted at runtime and
|
|
55
|
+
* ignored. This alias is the v4 type, which rejects `errorMap` at compile time.
|
|
56
|
+
*/
|
|
57
|
+
export type SensitiveParseOptions = z.core.ParseContext<z.core.$ZodIssue>;
|
|
58
|
+
/**
|
|
59
|
+
* A Zod error map that never echoes the input into a message.
|
|
60
|
+
*
|
|
61
|
+
* Install it globally with {@link installSensitiveErrorMap}, or pass it per
|
|
62
|
+
* call as `schema.safeParse(data, { error: sensitiveErrorMap })`.
|
|
63
|
+
*
|
|
64
|
+
* This rewrites messages only. The issue objects still carry `keys`, `params`,
|
|
65
|
+
* and `key`; see {@link redactZodError}.
|
|
66
|
+
*
|
|
67
|
+
* Returning `undefined` is how a v4 error map declines: there is no second
|
|
68
|
+
* `ctx` argument carrying a default message, and returning nothing hands the
|
|
69
|
+
* issue back to Zod's own message resolution.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* const schema = z.object({ a: z.string() }).strict();
|
|
74
|
+
* const result = schema.safeParse({ a: 'x', [secret]: 1 }, { error: sensitiveErrorMap });
|
|
75
|
+
* // "Unrecognized key(s) in object: 1 not allowed" - and nothing about the key
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare const sensitiveErrorMap: z.core.$ZodErrorMap;
|
|
79
|
+
/**
|
|
80
|
+
* Install {@link sensitiveErrorMap} as Zod's global error map.
|
|
81
|
+
*
|
|
82
|
+
* Global on purpose: the leaking issue codes come from schemas this package
|
|
83
|
+
* knows nothing about, most often a strict object sitting beside a sensitive
|
|
84
|
+
* field. Scoping the map to sensitive schemas would miss exactly the cases it
|
|
85
|
+
* exists for.
|
|
86
|
+
*
|
|
87
|
+
* It is not installed on import. A library that reaches into a process's
|
|
88
|
+
* global Zod configuration behind the application's back is worse than one
|
|
89
|
+
* that asks for a line of setup.
|
|
90
|
+
*
|
|
91
|
+
* `z.config()` reads the configuration back, so a caller who needs to undo this
|
|
92
|
+
* can capture `z.config().customError` first and restore it afterwards.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* // Once, during startup.
|
|
97
|
+
* installSensitiveErrorMap();
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export declare function installSensitiveErrorMap(): void;
|
|
101
|
+
/**
|
|
102
|
+
* Copy a list of issues with every input value stripped out.
|
|
103
|
+
*
|
|
104
|
+
* The originals are not modified: a caller may still be holding the unredacted
|
|
105
|
+
* error, and silently mutating it would change what an already-written handler
|
|
106
|
+
* reports.
|
|
107
|
+
*
|
|
108
|
+
* @param issues - The issues to copy
|
|
109
|
+
* @returns Issues safe to log or serialize
|
|
110
|
+
*/
|
|
111
|
+
export declare function redactZodIssues(issues: readonly z.core.$ZodIssue[]): z.core.$ZodIssue[];
|
|
112
|
+
/**
|
|
113
|
+
* Copy a ZodError with every input value stripped out, including from the
|
|
114
|
+
* issues nested inside a union, key, or element issue.
|
|
115
|
+
*
|
|
116
|
+
* @param error - The error to copy
|
|
117
|
+
* @returns A new ZodError safe to log or serialize
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* try {
|
|
122
|
+
* schema.parse(payload);
|
|
123
|
+
* } catch (err) {
|
|
124
|
+
* if (err instanceof z.ZodError) {
|
|
125
|
+
* logger.warn('validation failed', { issues: redactZodError(err).issues });
|
|
126
|
+
* }
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
export declare function redactZodError<T>(error: z.ZodError<T>): z.ZodError<T>;
|
|
131
|
+
/**
|
|
132
|
+
* `safeParse` with both halves of the fix applied: the error map rewrites the
|
|
133
|
+
* messages, and the returned issues are redacted.
|
|
134
|
+
*
|
|
135
|
+
* @typeParam Output - What the schema produces
|
|
136
|
+
* @typeParam Input - What the schema accepts
|
|
137
|
+
* @param schema - The schema to parse with
|
|
138
|
+
* @param data - The input
|
|
139
|
+
* @param params - Additional Zod parse options; a supplied `error` map wins
|
|
140
|
+
* @returns Zod's usual safe-parse result, with a redacted error on failure
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* const result = sensitiveSafeParse(LoginSchema, req.body);
|
|
145
|
+
* if (!result.success) {
|
|
146
|
+
* res.status(400).json({ issues: result.error.issues }); // no input echoed
|
|
147
|
+
* }
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export declare function sensitiveSafeParse<Output, Input>(schema: z.ZodType<Output, Input>, data: unknown, params?: SensitiveParseOptions): z.ZodSafeParseResult<Output>;
|
|
151
|
+
/**
|
|
152
|
+
* The async counterpart of {@link sensitiveSafeParse}, for schemas carrying an
|
|
153
|
+
* async refinement.
|
|
154
|
+
*
|
|
155
|
+
* @typeParam Output - What the schema produces
|
|
156
|
+
* @typeParam Input - What the schema accepts
|
|
157
|
+
* @param schema - The schema to parse with
|
|
158
|
+
* @param data - The input
|
|
159
|
+
* @param params - Additional Zod parse options; a supplied `error` map wins
|
|
160
|
+
* @returns Zod's usual safe-parse result, with a redacted error on failure
|
|
161
|
+
*/
|
|
162
|
+
export declare function sensitiveSafeParseAsync<Output, Input>(schema: z.ZodType<Output, Input>, data: unknown, params?: SensitiveParseOptions): Promise<z.ZodSafeParseResult<Output>>;
|
|
163
|
+
/**
|
|
164
|
+
* `parse` that throws a redacted ZodError.
|
|
165
|
+
*
|
|
166
|
+
* Anything else raised during parsing, such as an error thrown from inside a
|
|
167
|
+
* `transform`, propagates untouched. This helper cannot know what such an
|
|
168
|
+
* error's message contains, and pretending to have sanitized it would be worse
|
|
169
|
+
* than passing it through.
|
|
170
|
+
*
|
|
171
|
+
* @typeParam Output - What the schema produces
|
|
172
|
+
* @typeParam Input - What the schema accepts
|
|
173
|
+
* @param schema - The schema to parse with
|
|
174
|
+
* @param data - The input
|
|
175
|
+
* @param params - Additional Zod parse options; a supplied `error` map wins
|
|
176
|
+
* @returns The parsed value
|
|
177
|
+
* @throws z.ZodError Redacted, if validation fails.
|
|
178
|
+
*/
|
|
179
|
+
export declare function sensitiveParse<Output, Input>(schema: z.ZodType<Output, Input>, data: unknown, params?: SensitiveParseOptions): Output;
|
|
180
|
+
//# sourceMappingURL=error-map.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-map.d.ts","sourceRoot":"","sources":["../src/error-map.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,eAAe,CAAC;AAEjD;;;;;;GAMG;AACH,MAAM,MAAM,qBAAqB,GAAG,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;AAE1E;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,iBAAiB,EAAE,CAAC,CAAC,IAAI,CAAC,YAYtC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,wBAAwB,IAAI,IAAI,CAE/C;AA8GD;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,IAA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