@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/base.js
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The base schema factories: plain strings, validated strings, optional
|
|
4
|
+
* fields, and the serialized wire format.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.zSensitiveString = zSensitiveString;
|
|
10
|
+
exports.zSensitive = zSensitive;
|
|
11
|
+
exports.zCredential = zCredential;
|
|
12
|
+
exports.zSensitiveWithValidation = zSensitiveWithValidation;
|
|
13
|
+
exports.zSensitiveOptional = zSensitiveOptional;
|
|
14
|
+
exports.zSensitiveDeserialize = zSensitiveDeserialize;
|
|
15
|
+
const zod_1 = require("zod");
|
|
16
|
+
const sensitive_string_core_1 = require("@platform-sec/sensitive-string-core");
|
|
17
|
+
const refine_js_1 = require("./refine.js");
|
|
18
|
+
/**
|
|
19
|
+
* Create a Zod schema that parses strings into SensitiveString instances.
|
|
20
|
+
*
|
|
21
|
+
* @typeParam C - The sensitivity class for the resulting SensitiveString
|
|
22
|
+
* @param klass - The sensitivity classification to apply
|
|
23
|
+
* @param label - Optional label for audit/compliance categorization
|
|
24
|
+
* @returns A Zod schema that transforms strings to SensitiveString<C>
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```ts
|
|
28
|
+
* const schema = z.object({
|
|
29
|
+
* apiKey: zSensitiveString('credential', 'api_key'),
|
|
30
|
+
* email: zSensitiveString('sensitive', 'email'),
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* const result = schema.parse({
|
|
34
|
+
* apiKey: 'sk-xxx',
|
|
35
|
+
* email: 'user@example.com',
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* console.log(result.apiKey); // SensitiveString<credential:api_key> { [REDACTED:credential] }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
function zSensitiveString(klass, label) {
|
|
42
|
+
return (0, refine_js_1.withSensitiveMethods)(zod_1.z.string().transform((value) => {
|
|
43
|
+
return new sensitive_string_core_1.SensitiveString(value, klass, label);
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Create a Zod schema for sensitive data (PII, business data, etc.).
|
|
48
|
+
* Shorthand for zSensitiveString('sensitive', label).
|
|
49
|
+
*
|
|
50
|
+
* @param label - Optional label for audit/compliance (e.g., 'email', 'phone', 'address')
|
|
51
|
+
* @returns A Zod schema that transforms strings to SensitiveString<'sensitive'>
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```ts
|
|
55
|
+
* const schema = z.object({
|
|
56
|
+
* email: zSensitive('email'),
|
|
57
|
+
* phone: zSensitive('phone'),
|
|
58
|
+
* name: zSensitive('name'),
|
|
59
|
+
* });
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
function zSensitive(label) {
|
|
63
|
+
return zSensitiveString('sensitive', label);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Create a Zod schema for credential data (passwords, API keys, tokens).
|
|
67
|
+
* Shorthand for zSensitiveString('credential', label).
|
|
68
|
+
* Values will use constant-time comparison to prevent timing attacks.
|
|
69
|
+
*
|
|
70
|
+
* @param label - Optional label for audit/compliance (e.g., 'api_key', 'password', 'session_token')
|
|
71
|
+
* @returns A Zod schema that transforms strings to SensitiveString<'credential'>
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const schema = z.object({
|
|
76
|
+
* password: zCredential('password'),
|
|
77
|
+
* apiKey: zCredential('api_key'),
|
|
78
|
+
* sessionToken: zCredential('session_token'),
|
|
79
|
+
* });
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
function zCredential(label) {
|
|
83
|
+
return zSensitiveString('credential', label);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Create a Zod schema with additional string validation before wrapping.
|
|
87
|
+
*
|
|
88
|
+
* The schema parameter is any string-to-string schema, not specifically a
|
|
89
|
+
* `z.ZodString`. Zod 4 promoted the string formats to top-level constructors
|
|
90
|
+
* with classes of their own, so `z.email()` is a `ZodEmail` and is not a
|
|
91
|
+
* `ZodString`; requiring the narrower type would push callers back onto the
|
|
92
|
+
* deprecated `z.string().email()` spelling to use this package.
|
|
93
|
+
*
|
|
94
|
+
* @typeParam C - The sensitivity class for the resulting SensitiveString
|
|
95
|
+
* @param klass - The sensitivity classification to apply
|
|
96
|
+
* @param schema - A Zod schema that takes and returns a string
|
|
97
|
+
* @param label - Optional label for audit/compliance
|
|
98
|
+
* @returns A Zod schema that validates then transforms to SensitiveString<C>
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* // API key must be at least 32 characters
|
|
103
|
+
* const apiKeySchema = zSensitiveWithValidation(
|
|
104
|
+
* 'credential',
|
|
105
|
+
* z.string().min(32, 'API key too short'),
|
|
106
|
+
* 'api_key'
|
|
107
|
+
* );
|
|
108
|
+
*
|
|
109
|
+
* // A v4 format constructor works directly.
|
|
110
|
+
* const emailSchema = zSensitiveWithValidation('sensitive', z.email(), 'email');
|
|
111
|
+
*
|
|
112
|
+
* // Password with complexity requirements
|
|
113
|
+
* const passwordSchema = zSensitiveWithValidation(
|
|
114
|
+
* 'credential',
|
|
115
|
+
* z.string()
|
|
116
|
+
* .min(8, 'Password must be at least 8 characters')
|
|
117
|
+
* .regex(/[A-Z]/, 'Password must contain uppercase'),
|
|
118
|
+
* 'password'
|
|
119
|
+
* );
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
function zSensitiveWithValidation(klass, schema, label) {
|
|
123
|
+
return (0, refine_js_1.withSensitiveMethods)(schema.transform((value) => {
|
|
124
|
+
return new sensitive_string_core_1.SensitiveString(value, klass, label);
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Create a Zod schema for optional sensitive strings.
|
|
129
|
+
* Returns null for undefined/null input, SensitiveString for valid strings.
|
|
130
|
+
*
|
|
131
|
+
* @typeParam C - The sensitivity class for the resulting SensitiveString
|
|
132
|
+
* @param klass - The sensitivity classification to apply
|
|
133
|
+
* @param label - Optional label for audit/compliance
|
|
134
|
+
* @returns A Zod schema that transforms optional strings
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* const schema = z.object({
|
|
139
|
+
* apiKey: zSensitiveOptional('credential', 'api_key'),
|
|
140
|
+
* });
|
|
141
|
+
*
|
|
142
|
+
* schema.parse({ apiKey: undefined }); // { apiKey: null }
|
|
143
|
+
* schema.parse({ apiKey: 'sk-xxx' }); // { apiKey: SensitiveString<credential> }
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
function zSensitiveOptional(klass, label) {
|
|
147
|
+
return zod_1.z
|
|
148
|
+
.string()
|
|
149
|
+
.nullable()
|
|
150
|
+
.optional()
|
|
151
|
+
.transform((value) => {
|
|
152
|
+
if (value == null || value === '') {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
return new sensitive_string_core_1.SensitiveString(value, klass, label);
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Create a Zod schema that validates against a serialized format
|
|
160
|
+
* and reconstructs the SensitiveString.
|
|
161
|
+
*
|
|
162
|
+
* Useful for validating incoming payloads that were serialized with serialize().
|
|
163
|
+
*
|
|
164
|
+
* Reconstruction is delegated to `SensitiveString.deserialize()`, so this path
|
|
165
|
+
* enforces exactly what the core wire format does: the sensitivity class must be
|
|
166
|
+
* registered, a deadline carried by the payload is carried into the value, a
|
|
167
|
+
* payload that marks the value revoked produces a revoked value, and environment
|
|
168
|
+
* restrictions and destination bindings arrive with it. Rebuilding the value
|
|
169
|
+
* here instead would silently drop all four, which fails open: an expiring
|
|
170
|
+
* token would arrive as one that never expires, a revoked one as live, one
|
|
171
|
+
* restricted to the EU as readable anywhere, and one bound to a host as
|
|
172
|
+
* sendable to any.
|
|
173
|
+
*
|
|
174
|
+
* A class mismatch, an unregistered class, and a malformed payload are all
|
|
175
|
+
* reported as Zod issues, so `safeParse` reports them rather than throwing.
|
|
176
|
+
*
|
|
177
|
+
* @typeParam C - The expected sensitivity class
|
|
178
|
+
* @param expectedKlass - Optional expected class (validates if provided)
|
|
179
|
+
* @returns A Zod schema that parses serialized forms to SensitiveString
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```ts
|
|
183
|
+
* const schema = z.object({
|
|
184
|
+
* token: zSensitiveDeserialize('credential'),
|
|
185
|
+
* });
|
|
186
|
+
*
|
|
187
|
+
* // Validates serialized structure and reconstructs
|
|
188
|
+
* const result = schema.parse({
|
|
189
|
+
* token: { __sensitive: true, klass: 'credential', value: 'secret-value', label: 'api_key' }
|
|
190
|
+
* });
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
function zSensitiveDeserialize(expectedKlass) {
|
|
194
|
+
return (0, refine_js_1.withSensitiveMethods)(zod_1.z
|
|
195
|
+
.object({
|
|
196
|
+
__sensitive: zod_1.z.literal(true),
|
|
197
|
+
klass: zod_1.z.string(),
|
|
198
|
+
value: zod_1.z.string(),
|
|
199
|
+
label: zod_1.z.string().optional(),
|
|
200
|
+
expiresAt: zod_1.z.string().optional(),
|
|
201
|
+
// Declared so the object schema does not strip them before
|
|
202
|
+
// `deserialize()` sees them. An unlisted key is dropped, and dropping
|
|
203
|
+
// these would turn a payload that says "do not use this" into one that
|
|
204
|
+
// says nothing, or a value restricted to the EU into one readable
|
|
205
|
+
// anywhere, or one bound to a single host into one that may go to any,
|
|
206
|
+
// which is the same fail-open shape as losing the deadline. Validating
|
|
207
|
+
// them is still core's job, so all four are accepted loosely here and
|
|
208
|
+
// rejected there.
|
|
209
|
+
revoked: zod_1.z.literal(true).optional(),
|
|
210
|
+
revocationReason: zod_1.z.string().optional(),
|
|
211
|
+
restrictions: zod_1.z.array(zod_1.z.unknown()).optional(),
|
|
212
|
+
destinations: zod_1.z.array(zod_1.z.unknown()).optional(),
|
|
213
|
+
})
|
|
214
|
+
.transform((serialized, ctx) => {
|
|
215
|
+
// Reported as issues rather than thrown. A throw from inside a
|
|
216
|
+
// transform escapes `safeParse`, so a caller who chose the
|
|
217
|
+
// non-throwing API would get an exception anyway, from the one schema
|
|
218
|
+
// whose whole job is handling a payload from another service.
|
|
219
|
+
if (expectedKlass && serialized.klass !== expectedKlass) {
|
|
220
|
+
ctx.addIssue({
|
|
221
|
+
code: 'custom',
|
|
222
|
+
message: `Expected sensitivity class '${expectedKlass}', got '${serialized.klass}'`,
|
|
223
|
+
});
|
|
224
|
+
return zod_1.z.NEVER;
|
|
225
|
+
}
|
|
226
|
+
try {
|
|
227
|
+
return sensitive_string_core_1.SensitiveString.deserialize(serialized);
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
ctx.addIssue({
|
|
231
|
+
code: 'custom',
|
|
232
|
+
message: error instanceof Error ? error.message : 'Invalid serialized sensitive value',
|
|
233
|
+
});
|
|
234
|
+
return zod_1.z.NEVER;
|
|
235
|
+
}
|
|
236
|
+
}));
|
|
237
|
+
}
|
|
238
|
+
//# sourceMappingURL=base.js.map
|
package/dist/base.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../src/base.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AA8BH,4CASC;AAkBD,gCAEC;AAmBD,kCAEC;AAuCD,4DAUC;AAqBD,gDAcC;AAqCD,sDA6DC;AApQD,6BAAwB;AACxB,+EAAwF;AACxF,2CAAmD;AAGnD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,SAAgB,gBAAgB,CAC9B,KAAQ,EACR,KAAc;IAEd,OAAO,IAAA,gCAAoB,EACzB,OAAC,CAAC,MAAM,EAAE,CAAC,SAAS,CAAC,CAAC,KAAK,EAAsB,EAAE;QACjD,OAAO,IAAI,uCAAe,CAAC,KAAK,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;IAClD,CAAC,CAAC,CACH,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,SAAgB,UAAU,CAAC,KAAc;IACvC,OAAO,gBAAgB,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;AAC9C,CAAC;AAED;;;;;;;;;;;;;;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+
* Arrays and records of sensitive values.
|
|
3
|
+
*
|
|
4
|
+
* Each helper wraps a per-element sensitive schema in a plain `z.array` or
|
|
5
|
+
* `z.record`, rather than transforming a whole collection at once. That keeps
|
|
6
|
+
* the collection combinators available (`.min()`, `.max()`, `.nonempty()`) and
|
|
7
|
+
* puts a failing element's index or key in the issue path, where a whole-array
|
|
8
|
+
* transform would report only against the array itself.
|
|
9
|
+
*
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
*/
|
|
12
|
+
import { z } from 'zod';
|
|
13
|
+
import type { SensitivityClass } from '@platform-sec/sensitive-string-core';
|
|
14
|
+
import type { SensitiveSchema } from './types.js';
|
|
15
|
+
/**
|
|
16
|
+
* A schema for an array of sensitive values.
|
|
17
|
+
*
|
|
18
|
+
* Exposed as its own name because the element schema is an intersection type
|
|
19
|
+
* and spelling it out at every call site is noise.
|
|
20
|
+
*/
|
|
21
|
+
export type SensitiveArraySchema<C extends SensitivityClass> = z.ZodArray<SensitiveSchema<C>>;
|
|
22
|
+
/**
|
|
23
|
+
* A schema for a record of sensitive values, keyed by plain strings.
|
|
24
|
+
*
|
|
25
|
+
* Keys stay plain. A key is a field name, not the secret, and redacting it
|
|
26
|
+
* would leave the record unusable for lookup.
|
|
27
|
+
*/
|
|
28
|
+
export type SensitiveRecordSchema<C extends SensitivityClass> = z.ZodRecord<z.ZodString, SensitiveSchema<C>>;
|
|
29
|
+
/**
|
|
30
|
+
* Create a schema for an array whose elements are wrapped individually.
|
|
31
|
+
*
|
|
32
|
+
* @typeParam C - The sensitivity class applied to every element
|
|
33
|
+
* @param klass - The sensitivity classification to apply
|
|
34
|
+
* @param label - Optional label shared by every element
|
|
35
|
+
* @returns A ZodArray of sensitive values
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const schema = zSensitiveStringArray('credential', 'api_key').min(1);
|
|
40
|
+
* const keys = schema.parse(['sk-a', 'sk-b']);
|
|
41
|
+
* // keys[0] is SensitiveString<'credential'>
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function zSensitiveStringArray<C extends SensitivityClass>(klass: C, label?: string): SensitiveArraySchema<C>;
|
|
45
|
+
/**
|
|
46
|
+
* Create a schema for an array of sensitive data (PII, business data).
|
|
47
|
+
* Shorthand for zSensitiveStringArray('sensitive', label).
|
|
48
|
+
*
|
|
49
|
+
* @param label - Optional label shared by every element
|
|
50
|
+
* @returns A ZodArray of SensitiveString<'sensitive'>
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* const schema = z.object({ recipients: zSensitiveArray('email') });
|
|
55
|
+
* schema.parse({ recipients: ['a@example.com', 'b@example.com'] });
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function zSensitiveArray(label?: string): SensitiveArraySchema<'sensitive'>;
|
|
59
|
+
/**
|
|
60
|
+
* Create a schema for an array of credentials.
|
|
61
|
+
* Shorthand for zSensitiveStringArray('credential', label).
|
|
62
|
+
*
|
|
63
|
+
* @param label - Optional label shared by every element
|
|
64
|
+
* @returns A ZodArray of SensitiveString<'credential'>
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* const schema = z.object({ keys: zCredentialArray('api_key') });
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function zCredentialArray(label?: string): SensitiveArraySchema<'credential'>;
|
|
72
|
+
/**
|
|
73
|
+
* Create a schema for a record whose values are wrapped individually.
|
|
74
|
+
*
|
|
75
|
+
* @typeParam C - The sensitivity class applied to every value
|
|
76
|
+
* @param klass - The sensitivity classification to apply
|
|
77
|
+
* @param label - Optional label shared by every value
|
|
78
|
+
* @returns A ZodRecord of sensitive values
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const schema = zSensitiveStringRecord('credential', 'secret');
|
|
83
|
+
* const secrets = schema.parse({ stripe: 'sk-a', twilio: 'sk-b' });
|
|
84
|
+
* // secrets.stripe is SensitiveString<'credential'>
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function zSensitiveStringRecord<C extends SensitivityClass>(klass: C, label?: string): SensitiveRecordSchema<C>;
|
|
88
|
+
/**
|
|
89
|
+
* Create a schema for a record of sensitive data.
|
|
90
|
+
* Shorthand for zSensitiveStringRecord('sensitive', label).
|
|
91
|
+
*
|
|
92
|
+
* @param label - Optional label shared by every value
|
|
93
|
+
* @returns A ZodRecord of SensitiveString<'sensitive'>
|
|
94
|
+
*/
|
|
95
|
+
export declare function zSensitiveRecord(label?: string): SensitiveRecordSchema<'sensitive'>;
|
|
96
|
+
/**
|
|
97
|
+
* Create a schema for a record of credentials.
|
|
98
|
+
* Shorthand for zSensitiveStringRecord('credential', label).
|
|
99
|
+
*
|
|
100
|
+
* @param label - Optional label shared by every value
|
|
101
|
+
* @returns A ZodRecord of SensitiveString<'credential'>
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* const schema = zCredentialRecord('secret');
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export declare function zCredentialRecord(label?: string): SensitiveRecordSchema<'credential'>;
|
|
109
|
+
//# sourceMappingURL=collections.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.d.ts","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AAE5E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAElD;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,CAAC,CAAC,SAAS,gBAAgB,IAAI,CAAC,CAAC,QAAQ,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;AAE9F;;;;;GAKG;AACH,MAAM,MAAM,qBAAqB,CAAC,CAAC,SAAS,gBAAgB,IAAI,CAAC,CAAC,SAAS,CACzE,CAAC,CAAC,SAAS,EACX,eAAe,CAAC,CAAC,CAAC,CACnB,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,gBAAgB,EAC9D,KAAK,EAAE,CAAC,EACR,KAAK,CAAC,EAAE,MAAM,GACb,oBAAoB,CAAC,CAAC,CAAC,CAEzB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,oBAAoB,CAAC,WAAW,CAAC,CAEjF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,oBAAoB,CAAC,YAAY,CAAC,CAEnF;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,sBAAsB,CAAC,CAAC,SAAS,gBAAgB,EAC/D,KAAK,EAAE,CAAC,EACR,KAAK,CAAC,EAAE,MAAM,GACb,qBAAqB,CAAC,CAAC,CAAC,CAE1B;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,qBAAqB,CAAC,WAAW,CAAC,CAEnF;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAErF"}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Arrays and records of sensitive values.
|
|
4
|
+
*
|
|
5
|
+
* Each helper wraps a per-element sensitive schema in a plain `z.array` or
|
|
6
|
+
* `z.record`, rather than transforming a whole collection at once. That keeps
|
|
7
|
+
* the collection combinators available (`.min()`, `.max()`, `.nonempty()`) and
|
|
8
|
+
* puts a failing element's index or key in the issue path, where a whole-array
|
|
9
|
+
* transform would report only against the array itself.
|
|
10
|
+
*
|
|
11
|
+
* @packageDocumentation
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.zSensitiveStringArray = zSensitiveStringArray;
|
|
15
|
+
exports.zSensitiveArray = zSensitiveArray;
|
|
16
|
+
exports.zCredentialArray = zCredentialArray;
|
|
17
|
+
exports.zSensitiveStringRecord = zSensitiveStringRecord;
|
|
18
|
+
exports.zSensitiveRecord = zSensitiveRecord;
|
|
19
|
+
exports.zCredentialRecord = zCredentialRecord;
|
|
20
|
+
const zod_1 = require("zod");
|
|
21
|
+
const base_js_1 = require("./base.js");
|
|
22
|
+
/**
|
|
23
|
+
* Create a schema for an array whose elements are wrapped individually.
|
|
24
|
+
*
|
|
25
|
+
* @typeParam C - The sensitivity class applied to every element
|
|
26
|
+
* @param klass - The sensitivity classification to apply
|
|
27
|
+
* @param label - Optional label shared by every element
|
|
28
|
+
* @returns A ZodArray of sensitive values
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const schema = zSensitiveStringArray('credential', 'api_key').min(1);
|
|
33
|
+
* const keys = schema.parse(['sk-a', 'sk-b']);
|
|
34
|
+
* // keys[0] is SensitiveString<'credential'>
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
function zSensitiveStringArray(klass, label) {
|
|
38
|
+
return zod_1.z.array((0, base_js_1.zSensitiveString)(klass, label));
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Create a schema for an array of sensitive data (PII, business data).
|
|
42
|
+
* Shorthand for zSensitiveStringArray('sensitive', label).
|
|
43
|
+
*
|
|
44
|
+
* @param label - Optional label shared by every element
|
|
45
|
+
* @returns A ZodArray of SensitiveString<'sensitive'>
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* const schema = z.object({ recipients: zSensitiveArray('email') });
|
|
50
|
+
* schema.parse({ recipients: ['a@example.com', 'b@example.com'] });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
function zSensitiveArray(label) {
|
|
54
|
+
return zSensitiveStringArray('sensitive', label);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Create a schema for an array of credentials.
|
|
58
|
+
* Shorthand for zSensitiveStringArray('credential', label).
|
|
59
|
+
*
|
|
60
|
+
* @param label - Optional label shared by every element
|
|
61
|
+
* @returns A ZodArray of SensitiveString<'credential'>
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* const schema = z.object({ keys: zCredentialArray('api_key') });
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
function zCredentialArray(label) {
|
|
69
|
+
return zSensitiveStringArray('credential', label);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Create a schema for a record whose values are wrapped individually.
|
|
73
|
+
*
|
|
74
|
+
* @typeParam C - The sensitivity class applied to every value
|
|
75
|
+
* @param klass - The sensitivity classification to apply
|
|
76
|
+
* @param label - Optional label shared by every value
|
|
77
|
+
* @returns A ZodRecord of sensitive values
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* const schema = zSensitiveStringRecord('credential', 'secret');
|
|
82
|
+
* const secrets = schema.parse({ stripe: 'sk-a', twilio: 'sk-b' });
|
|
83
|
+
* // secrets.stripe is SensitiveString<'credential'>
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
function zSensitiveStringRecord(klass, label) {
|
|
87
|
+
return zod_1.z.record(zod_1.z.string(), (0, base_js_1.zSensitiveString)(klass, label));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Create a schema for a record of sensitive data.
|
|
91
|
+
* Shorthand for zSensitiveStringRecord('sensitive', label).
|
|
92
|
+
*
|
|
93
|
+
* @param label - Optional label shared by every value
|
|
94
|
+
* @returns A ZodRecord of SensitiveString<'sensitive'>
|
|
95
|
+
*/
|
|
96
|
+
function zSensitiveRecord(label) {
|
|
97
|
+
return zSensitiveStringRecord('sensitive', label);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Create a schema for a record of credentials.
|
|
101
|
+
* Shorthand for zSensitiveStringRecord('credential', label).
|
|
102
|
+
*
|
|
103
|
+
* @param label - Optional label shared by every value
|
|
104
|
+
* @returns A ZodRecord of SensitiveString<'credential'>
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```ts
|
|
108
|
+
* const schema = zCredentialRecord('secret');
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
function zCredentialRecord(label) {
|
|
112
|
+
return zSensitiveStringRecord('credential', label);
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=collections.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collections.js","sourceRoot":"","sources":["../src/collections.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;AAyCH,sDAKC;AAeD,0CAEC;AAcD,4CAEC;AAiBD,wDAKC;AASD,4CAEC;AAcD,8CAEC;AA9HD,6BAAwB;AAExB,uCAA6C;AAsB7C;;;;;;;;;;;;;;GAcG;AACH,SAAgB,qBAAqB,CACnC,KAAQ,EACR,KAAc;IAEd,OAAO,OAAC,CAAC,KAAK,CAAC,IAAA,0BAAgB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;AACjD,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,eAAe,CAAC,KAAc;IAC5C,OAAO,qBAAqB,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;AACnD,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,gBAAgB,CAAC,KAAc;IAC7C,OAAO,qBAAqB,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,sBAAsB,CACpC,KAAQ,EACR,KAAc;IAEd,OAAO,OAAC,CAAC,MAAM,CAAC,OAAC,CAAC,MAAM,EAAE,EAAE,IAAA,0BAAgB,EAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;AAC9D,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,gBAAgB,CAAC,KAAc;IAC7C,OAAO,sBAAsB,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,iBAAiB,CAAC,KAAc;IAC9C,OAAO,sBAAsB,CAAC,YAAY,EAAE,KAAK,CAAC,CAAC;AACrD,CAAC"}
|
|
@@ -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,KAA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nC,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,uCAA6C;AA8D7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,SAAgB,qBAAqB,CAKnC,MAA2C;IAE3C,4EAA4E;IAC5E,wEAAwE;IACxE,qBAAqB;IACrB,MAAM,aAAa,GAAW,MAAM,CAAC,aAAa,CAAC;IACnD,MAAM,UAAU,GAAW,MAAM,CAAC,UAAU,CAAC;IAE7C,IAAI,aAAa,KAAK,UAAU,EAAE,CAAC;QACjC,sEAAsE;QACtE,uEAAuE;QACvE,uEAAuE;QACvE,gCAAgC;QAChC,MAAM,IAAI,SAAS,CACjB,4EAA4E,CAC7E,CAAC;IACJ,CAAC;IAED,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IAClD,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,MAAM,IAAI,SAAS,C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