@rehearsal-db/core 0.1.0-beta.14 → 0.1.0-beta.16
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/CHANGELOG.md +51 -1
- package/README.md +1 -0
- package/dist/package.json +101 -0
- package/dist/scripts/install/create_root_config.d.mts +3 -0
- package/dist/scripts/install/create_root_config.mjs +13 -0
- package/dist/scripts/runtime/manage_database.d.mts +6 -0
- package/{scripts → dist/scripts}/runtime/manage_database.mjs +6 -8
- package/dist/src/application/session.d.mts +96 -0
- package/dist/src/application/session.mjs +380 -0
- package/dist/src/baseline/artifact.d.mts +119 -0
- package/dist/src/baseline/artifact.mjs +522 -0
- package/dist/src/baseline/builder.d.mts +19 -0
- package/dist/src/baseline/builder.mjs +142 -0
- package/dist/src/baseline/input_discovery.d.mts +13 -0
- package/dist/src/baseline/input_discovery.mjs +149 -0
- package/dist/src/baseline/policy_review.d.mts +56 -0
- package/dist/src/baseline/policy_review.mjs +73 -0
- package/dist/src/baseline/preparation.d.mts +63 -0
- package/dist/src/baseline/preparation.mjs +295 -0
- package/dist/src/baseline/privacy_contract.d.mts +147 -0
- package/dist/src/baseline/privacy_contract.mjs +2 -0
- package/dist/src/baseline/privacy_dependency_graph.d.mts +7 -0
- package/dist/src/baseline/privacy_dependency_graph.mjs +110 -0
- package/dist/src/baseline/privacy_engine.d.mts +5 -0
- package/dist/src/baseline/privacy_engine.mjs +4 -0
- package/dist/src/baseline/privacy_key.d.mts +7 -0
- package/dist/src/baseline/privacy_key.mjs +42 -0
- package/dist/src/baseline/privacy_pipeline.d.mts +7 -0
- package/dist/src/baseline/privacy_pipeline.mjs +127 -0
- package/dist/src/baseline/privacy_policy.d.mts +6 -0
- package/dist/src/baseline/privacy_policy.mjs +237 -0
- package/dist/src/baseline/privacy_recipe_validation.d.mts +5 -0
- package/dist/src/baseline/privacy_recipe_validation.mjs +289 -0
- package/dist/src/baseline/privacy_rule_registry.d.mts +18 -0
- package/dist/src/baseline/privacy_rule_registry.mjs +143 -0
- package/dist/src/baseline/privacy_structured_json.d.mts +66 -0
- package/dist/src/baseline/privacy_structured_json.mjs +464 -0
- package/dist/src/baseline/privacy_transforms.d.mts +18 -0
- package/dist/src/baseline/privacy_transforms.mjs +142 -0
- package/dist/src/baseline/privacy_validation_shared.d.mts +31 -0
- package/dist/src/baseline/privacy_validation_shared.mjs +127 -0
- package/dist/src/baseline/sanitization_policy.d.mts +69 -0
- package/dist/src/baseline/sanitization_policy.mjs +227 -0
- package/dist/src/baseline/schema_snapshot.d.mts +19 -0
- package/dist/src/baseline/schema_snapshot.mjs +79 -0
- package/dist/src/cli/application_command_handlers.d.mts +8 -0
- package/dist/src/cli/application_command_handlers.mjs +52 -0
- package/dist/src/cli/arguments.d.mts +38 -0
- package/dist/src/cli/arguments.mjs +128 -0
- package/dist/src/cli/basic_command_handlers.d.mts +4 -0
- package/dist/src/cli/basic_command_handlers.mjs +56 -0
- package/dist/src/cli/cli_commands.d.mts +14 -0
- package/dist/src/cli/cli_commands.mjs +32 -0
- package/dist/src/cli/command_contract.d.mts +57 -0
- package/dist/src/cli/command_contract.mjs +39 -0
- package/dist/src/cli/command_output.d.mts +12 -0
- package/dist/src/cli/command_output.mjs +20 -0
- package/dist/src/cli/command_registry.d.mts +7 -0
- package/dist/src/cli/command_registry.mjs +14 -0
- package/dist/src/cli/guided.d.mts +53 -0
- package/dist/src/cli/guided.mjs +719 -0
- package/dist/src/cli/identity_command_handlers.d.mts +8 -0
- package/dist/src/cli/identity_command_handlers.mjs +43 -0
- package/dist/src/cli/init_command.d.mts +21 -0
- package/dist/src/cli/init_command.mjs +90 -0
- package/dist/src/cli/inspection_command_handlers.d.mts +3 -0
- package/dist/src/cli/inspection_command_handlers.mjs +79 -0
- package/dist/src/cli/project_command_handlers.d.mts +11 -0
- package/dist/src/cli/project_command_handlers.mjs +92 -0
- package/dist/src/cli/project_commands.d.mts +159 -0
- package/dist/src/cli/project_commands.mjs +183 -0
- package/dist/src/cli/rehearsal.d.mts +7 -0
- package/dist/src/cli/rehearsal.mjs +190 -0
- package/dist/src/cli/renderers.d.mts +299 -0
- package/dist/src/cli/renderers.mjs +554 -0
- package/dist/src/cli/runtime_action_handlers.d.mts +12 -0
- package/dist/src/cli/runtime_action_handlers.mjs +201 -0
- package/dist/src/cli/runtime_commands.d.mts +269 -0
- package/dist/src/cli/runtime_commands.mjs +601 -0
- package/dist/src/cli/source_command_handlers.d.mts +8 -0
- package/dist/src/cli/source_command_handlers.mjs +62 -0
- package/dist/src/cli/source_commands.d.mts +436 -0
- package/dist/src/cli/source_commands.mjs +301 -0
- package/dist/src/cli/terminal.d.mts +59 -0
- package/dist/src/cli/terminal.mjs +230 -0
- package/dist/src/identity/claim.d.mts +5 -0
- package/dist/src/identity/claim.mjs +4 -0
- package/dist/src/identity/claim_contract.d.mts +135 -0
- package/dist/src/identity/claim_contract.mjs +6 -0
- package/dist/src/identity/claim_executor.d.mts +10 -0
- package/dist/src/identity/claim_executor.mjs +358 -0
- package/dist/src/identity/claim_identity_lookup.d.mts +17 -0
- package/dist/src/identity/claim_identity_lookup.mjs +125 -0
- package/dist/src/identity/claim_plan.d.mts +8 -0
- package/dist/src/identity/claim_plan.mjs +61 -0
- package/dist/src/identity/claim_policy.d.mts +3 -0
- package/dist/src/identity/claim_policy.mjs +377 -0
- package/dist/src/identity/claim_sql.d.mts +10 -0
- package/dist/src/identity/claim_sql.mjs +46 -0
- package/dist/src/identity/claim_verification.d.mts +13 -0
- package/dist/src/identity/claim_verification.mjs +35 -0
- package/dist/src/identity/provider_configuration.d.mts +37 -0
- package/dist/src/identity/provider_configuration.mjs +132 -0
- package/dist/src/identity/storage.d.mts +21 -0
- package/dist/src/identity/storage.mjs +122 -0
- package/dist/src/index.d.mts +4 -0
- package/dist/src/index.mjs +3 -0
- package/dist/src/project/configuration.d.mts +13 -0
- package/dist/src/project/configuration.mjs +9 -0
- package/dist/src/project/configuration_contract.d.mts +125 -0
- package/dist/src/project/configuration_contract.mjs +27 -0
- package/dist/src/project/configuration_discovery.d.mts +7 -0
- package/dist/src/project/configuration_discovery.mjs +37 -0
- package/dist/src/project/configuration_normalization.d.mts +107 -0
- package/dist/src/project/configuration_normalization.mjs +302 -0
- package/dist/src/project/configuration_rendering.d.mts +14 -0
- package/dist/src/project/configuration_rendering.mjs +224 -0
- package/dist/src/project/configuration_resolution.d.mts +125 -0
- package/dist/src/project/configuration_resolution.mjs +81 -0
- package/dist/src/project/configuration_schema.d.mts +22 -0
- package/dist/src/project/configuration_schema.mjs +160 -0
- package/dist/src/project/configuration_validation.d.mts +22 -0
- package/dist/src/project/configuration_validation.mjs +136 -0
- package/dist/src/project/install_scaffold.d.mts +42 -0
- package/dist/src/project/install_scaffold.mjs +263 -0
- package/dist/src/project/project_detection.d.mts +14 -0
- package/dist/src/project/project_detection.mjs +47 -0
- package/dist/src/project/setup.d.mts +87 -0
- package/dist/src/project/setup.mjs +414 -0
- package/dist/src/project/support_report.d.mts +88 -0
- package/dist/src/project/support_report.mjs +83 -0
- package/dist/src/runtime/cleanup.d.mts +85 -0
- package/dist/src/runtime/cleanup.mjs +311 -0
- package/dist/src/runtime/lifecycle_engine.d.mts +63 -0
- package/dist/src/runtime/lifecycle_engine.mjs +114 -0
- package/dist/src/runtime/migration_history.d.mts +51 -0
- package/dist/src/runtime/migration_history.mjs +170 -0
- package/dist/src/runtime/plan.d.mts +134 -0
- package/dist/src/runtime/plan.mjs +637 -0
- package/dist/src/runtime/policy.d.mts +66 -0
- package/dist/src/runtime/policy.mjs +369 -0
- package/dist/src/runtime/restore.d.mts +22 -0
- package/dist/src/runtime/restore.mjs +276 -0
- package/dist/src/runtime/service_environment.d.mts +11 -0
- package/dist/src/runtime/service_environment.mjs +58 -0
- package/dist/src/runtime/topology.d.mts +51 -0
- package/dist/src/runtime/topology.mjs +128 -0
- package/dist/src/shared/diagnostics.d.mts +97 -0
- package/dist/src/shared/diagnostics.mjs +170 -0
- package/dist/src/shared/human_output.d.mts +2 -0
- package/{src → dist/src}/shared/human_output.mjs +1 -3
- package/dist/src/shared/node_runtime.d.mts +14 -0
- package/dist/src/shared/node_runtime.mjs +25 -0
- package/dist/src/shared/operation_guard.d.mts +22 -0
- package/dist/src/shared/operation_guard.mjs +150 -0
- package/dist/src/shared/process_environment.d.mts +15 -0
- package/dist/src/shared/process_environment.mjs +50 -0
- package/dist/src/source/access.d.mts +165 -0
- package/dist/src/source/access.mjs +602 -0
- package/dist/src/source/asset_transfer.d.mts +27 -0
- package/dist/src/source/asset_transfer.mjs +202 -0
- package/dist/src/source/baseline.d.mts +66 -0
- package/dist/src/source/baseline.mjs +372 -0
- package/dist/src/source/external_reader.d.mts +42 -0
- package/dist/src/source/external_reader.mjs +278 -0
- package/dist/src/source/postgresql_access.d.mts +86 -0
- package/dist/src/source/postgresql_access.mjs +454 -0
- package/dist/src/source/privacy_paths.d.mts +10 -0
- package/dist/src/source/privacy_paths.mjs +27 -0
- package/dist/src/targets/driver.d.mts +2 -0
- package/dist/src/targets/driver.mjs +13 -0
- package/dist/src/targets/postgresql.d.mts +5 -0
- package/dist/src/targets/postgresql.mjs +620 -0
- package/dist/src/targets/supabase.d.mts +8 -0
- package/dist/src/targets/supabase.mjs +627 -0
- package/dist/src/targets/supabase_environment.d.mts +49 -0
- package/dist/src/targets/supabase_environment.mjs +162 -0
- package/dist/src/targets/target.d.mts +33 -0
- package/dist/src/targets/target.mjs +39 -0
- package/docs/README.md +1 -0
- package/docs/architecture.md +133 -10
- package/docs/configuration.md +9 -3
- package/docs/index.md +1 -0
- package/docs/releasing.md +8 -3
- package/docs/typescript.md +134 -0
- package/package.json +36 -29
- package/types/configuration.d.mts +5 -0
- package/scripts/install/create_root_config.mjs +0 -19
- package/src/README.md +0 -17
- package/src/application/session.mjs +0 -435
- package/src/baseline/artifact.mjs +0 -684
- package/src/baseline/builder.mjs +0 -164
- package/src/baseline/input_discovery.mjs +0 -155
- package/src/baseline/policy_review.mjs +0 -92
- package/src/baseline/preparation.mjs +0 -323
- package/src/baseline/privacy_engine.mjs +0 -1399
- package/src/baseline/privacy_structured_json.mjs +0 -554
- package/src/baseline/sanitization_policy.mjs +0 -299
- package/src/baseline/schema_snapshot.mjs +0 -113
- package/src/cli/arguments.mjs +0 -126
- package/src/cli/guided.mjs +0 -814
- package/src/cli/init_command.mjs +0 -94
- package/src/cli/rehearsal.mjs +0 -972
- package/src/cli/renderers.mjs +0 -638
- package/src/cli/runtime_commands.mjs +0 -707
- package/src/cli/source_commands.mjs +0 -355
- package/src/cli/terminal.mjs +0 -275
- package/src/identity/claim.mjs +0 -1165
- package/src/identity/provider_configuration.mjs +0 -177
- package/src/identity/storage.mjs +0 -165
- package/src/project/configuration.d.mts +0 -164
- package/src/project/configuration.mjs +0 -1337
- package/src/project/install_scaffold.mjs +0 -325
- package/src/project/setup.mjs +0 -474
- package/src/project/support_report.mjs +0 -105
- package/src/runtime/cleanup.mjs +0 -381
- package/src/runtime/migration_history.mjs +0 -220
- package/src/runtime/plan.mjs +0 -722
- package/src/runtime/policy.mjs +0 -438
- package/src/runtime/restore.mjs +0 -337
- package/src/runtime/service_environment.mjs +0 -82
- package/src/runtime/topology.mjs +0 -177
- package/src/shared/diagnostics.mjs +0 -207
- package/src/shared/node_runtime.mjs +0 -31
- package/src/shared/operation_guard.mjs +0 -162
- package/src/shared/process_environment.mjs +0 -68
- package/src/source/access.mjs +0 -798
- package/src/source/asset_transfer.mjs +0 -259
- package/src/source/baseline.mjs +0 -482
- package/src/source/external_reader.mjs +0 -368
- package/src/source/postgresql_access.mjs +0 -638
- package/src/source/privacy_paths.mjs +0 -38
- package/src/targets/postgresql.mjs +0 -808
- package/src/targets/supabase.mjs +0 -840
- package/src/targets/supabase_environment.mjs +0 -229
- package/src/targets/target.mjs +0 -66
|
@@ -1,554 +0,0 @@
|
|
|
1
|
-
/** Validate and execute the bounded structured-JSON privacy recipes. */
|
|
2
|
-
|
|
3
|
-
const JSON_UNION_VARIANTS = new Set([
|
|
4
|
-
"string",
|
|
5
|
-
"number",
|
|
6
|
-
"boolean",
|
|
7
|
-
"array",
|
|
8
|
-
"object",
|
|
9
|
-
]);
|
|
10
|
-
const DICTIONARY_KEY_FORMATS = new Set([
|
|
11
|
-
"integer",
|
|
12
|
-
"positive-integer",
|
|
13
|
-
"uuid",
|
|
14
|
-
"identifier",
|
|
15
|
-
"text",
|
|
16
|
-
]);
|
|
17
|
-
const IDENTIFIER = /^[a-z][a-z0-9_]{0,62}$/u;
|
|
18
|
-
const UUID =
|
|
19
|
-
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/iu;
|
|
20
|
-
const DEFAULT_MAXIMUM_BYTES = 65_536;
|
|
21
|
-
const DEFAULT_MAXIMUM_DEPTH = 8;
|
|
22
|
-
|
|
23
|
-
export const STRUCTURED_JSON_KINDS = new Set([
|
|
24
|
-
"json-object",
|
|
25
|
-
"json-array",
|
|
26
|
-
"json-union",
|
|
27
|
-
"json-dictionary",
|
|
28
|
-
]);
|
|
29
|
-
|
|
30
|
-
const isObject = (value) =>
|
|
31
|
-
value !== null &&
|
|
32
|
-
typeof value === "object" &&
|
|
33
|
-
!Array.isArray(value) &&
|
|
34
|
-
Object.getPrototypeOf(value) === Object.prototype;
|
|
35
|
-
|
|
36
|
-
const knownKeys = (value, allowed, label) => {
|
|
37
|
-
for (const key of Object.keys(value)) {
|
|
38
|
-
if (!allowed.includes(key)) throw new Error(`${label}.${key} is unknown.`);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
const positiveInteger = (value, label, maximum) => {
|
|
43
|
-
if (!Number.isSafeInteger(value) || value < 1 || value > maximum) {
|
|
44
|
-
throw new Error(`${label} must be an integer from 1 through ${maximum}.`);
|
|
45
|
-
}
|
|
46
|
-
return value;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const safeInteger = (value, label) => {
|
|
50
|
-
if (!Number.isSafeInteger(value)) {
|
|
51
|
-
throw new Error(`${label} must be a safe integer.`);
|
|
52
|
-
}
|
|
53
|
-
return value;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const boundsFor = (recipe, label, maximumPolicyDepth) => ({
|
|
57
|
-
maximumBytes: positiveInteger(
|
|
58
|
-
recipe.maximumBytes ?? DEFAULT_MAXIMUM_BYTES,
|
|
59
|
-
`${label}.recipe.maximumBytes`,
|
|
60
|
-
16_777_216,
|
|
61
|
-
),
|
|
62
|
-
maximumDepth: positiveInteger(
|
|
63
|
-
recipe.maximumDepth ?? DEFAULT_MAXIMUM_DEPTH,
|
|
64
|
-
`${label}.recipe.maximumDepth`,
|
|
65
|
-
maximumPolicyDepth,
|
|
66
|
-
),
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
const classifiesUnknownJsonValue = (declaration) => {
|
|
70
|
-
if (["REPLACE", "EXCLUDE", "PSEUDONYMIZE"].includes(declaration.action)) {
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
if (declaration.action !== "DERIVE") return false;
|
|
74
|
-
if (declaration.recipe.kind === "approved-owner") {
|
|
75
|
-
return (
|
|
76
|
-
classifiesUnknownJsonValue(declaration.recipe.approved) &&
|
|
77
|
-
classifiesUnknownJsonValue(declaration.recipe.otherwise)
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
if (declaration.recipe.kind === "json-union") return true;
|
|
81
|
-
if (declaration.recipe.kind === "json-array") {
|
|
82
|
-
return classifiesUnknownJsonValue(declaration.recipe.items);
|
|
83
|
-
}
|
|
84
|
-
if (declaration.recipe.kind === "json-object") {
|
|
85
|
-
return Object.values(declaration.recipe.fields).every(
|
|
86
|
-
classifiesUnknownJsonValue,
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
if (declaration.recipe.kind === "json-dictionary") {
|
|
90
|
-
return classifiesUnknownJsonValue(declaration.recipe.values);
|
|
91
|
-
}
|
|
92
|
-
return [
|
|
93
|
-
"date-shift",
|
|
94
|
-
"enum",
|
|
95
|
-
"validated-string",
|
|
96
|
-
"path-map",
|
|
97
|
-
"binding-substitute",
|
|
98
|
-
].includes(declaration.recipe.kind);
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
const classifiesContainer = (declaration, expectedType) => {
|
|
102
|
-
if (["REPLACE", "EXCLUDE", "PSEUDONYMIZE"].includes(declaration.action)) {
|
|
103
|
-
return true;
|
|
104
|
-
}
|
|
105
|
-
if (declaration.action !== "DERIVE") return false;
|
|
106
|
-
if (declaration.recipe.kind === "approved-owner") {
|
|
107
|
-
return (
|
|
108
|
-
classifiesContainer(declaration.recipe.approved, expectedType) &&
|
|
109
|
-
classifiesContainer(declaration.recipe.otherwise, expectedType)
|
|
110
|
-
);
|
|
111
|
-
}
|
|
112
|
-
if (declaration.recipe.kind === "json-union") return true;
|
|
113
|
-
return expectedType === "array"
|
|
114
|
-
? declaration.recipe.kind === "json-array" &&
|
|
115
|
-
classifiesUnknownJsonValue(declaration.recipe.items)
|
|
116
|
-
: declaration.recipe.kind === "json-object"
|
|
117
|
-
? Object.values(declaration.recipe.fields).every(
|
|
118
|
-
classifiesUnknownJsonValue,
|
|
119
|
-
)
|
|
120
|
-
: declaration.recipe.kind === "json-dictionary" &&
|
|
121
|
-
classifiesUnknownJsonValue(declaration.recipe.values);
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
export const validateStructuredJsonRecipe = ({
|
|
125
|
-
recipe,
|
|
126
|
-
label,
|
|
127
|
-
depth,
|
|
128
|
-
maximumPolicyDepth,
|
|
129
|
-
validateDeclaration,
|
|
130
|
-
validatePseudonymRecipe,
|
|
131
|
-
}) => {
|
|
132
|
-
const bounds = boundsFor(recipe, label, maximumPolicyDepth);
|
|
133
|
-
if (recipe.allowNull !== undefined && typeof recipe.allowNull !== "boolean") {
|
|
134
|
-
throw new Error(`${label}.recipe.allowNull must be true or false.`);
|
|
135
|
-
}
|
|
136
|
-
if (recipe.kind === "json-array") {
|
|
137
|
-
knownKeys(
|
|
138
|
-
recipe,
|
|
139
|
-
[
|
|
140
|
-
"kind",
|
|
141
|
-
"items",
|
|
142
|
-
"allowNull",
|
|
143
|
-
"maximumBytes",
|
|
144
|
-
"maximumDepth",
|
|
145
|
-
"maximumItems",
|
|
146
|
-
],
|
|
147
|
-
`${label}.recipe`,
|
|
148
|
-
);
|
|
149
|
-
return Object.freeze({
|
|
150
|
-
kind: "json-array",
|
|
151
|
-
allowNull: recipe.allowNull === true,
|
|
152
|
-
...bounds,
|
|
153
|
-
maximumItems: positiveInteger(
|
|
154
|
-
recipe.maximumItems,
|
|
155
|
-
`${label}.recipe.maximumItems`,
|
|
156
|
-
10_000,
|
|
157
|
-
),
|
|
158
|
-
items: validateDeclaration(recipe.items, `${label}.recipe.items`, {
|
|
159
|
-
depth,
|
|
160
|
-
}),
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
if (recipe.kind === "json-union") {
|
|
164
|
-
knownKeys(
|
|
165
|
-
recipe,
|
|
166
|
-
[
|
|
167
|
-
"kind",
|
|
168
|
-
"variants",
|
|
169
|
-
"allowNull",
|
|
170
|
-
"maximumBytes",
|
|
171
|
-
"maximumDepth",
|
|
172
|
-
"maximumItems",
|
|
173
|
-
],
|
|
174
|
-
`${label}.recipe`,
|
|
175
|
-
);
|
|
176
|
-
if (
|
|
177
|
-
!isObject(recipe.variants) ||
|
|
178
|
-
Object.keys(recipe.variants).length === 0
|
|
179
|
-
) {
|
|
180
|
-
throw new Error(
|
|
181
|
-
`${label}.recipe.variants must classify JSON value types.`,
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
const variants = Object.fromEntries(
|
|
185
|
-
Object.entries(recipe.variants).map(([type, declaration]) => {
|
|
186
|
-
if (!JSON_UNION_VARIANTS.has(type)) {
|
|
187
|
-
throw new Error(`${label}.recipe.variants.${type} is unsupported.`);
|
|
188
|
-
}
|
|
189
|
-
const normalized = validateDeclaration(
|
|
190
|
-
declaration,
|
|
191
|
-
`${label}.recipe.variants.${type}`,
|
|
192
|
-
{ depth },
|
|
193
|
-
);
|
|
194
|
-
if (
|
|
195
|
-
["array", "object"].includes(type) &&
|
|
196
|
-
!classifiesContainer(normalized, type)
|
|
197
|
-
) {
|
|
198
|
-
throw new Error(
|
|
199
|
-
`${label}.recipe.variants.${type} must classify container contents instead of retaining the whole value.`,
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
return [type, normalized];
|
|
203
|
-
}),
|
|
204
|
-
);
|
|
205
|
-
return Object.freeze({
|
|
206
|
-
kind: "json-union",
|
|
207
|
-
allowNull: recipe.allowNull === true,
|
|
208
|
-
...bounds,
|
|
209
|
-
maximumItems: positiveInteger(
|
|
210
|
-
recipe.maximumItems,
|
|
211
|
-
`${label}.recipe.maximumItems`,
|
|
212
|
-
10_000,
|
|
213
|
-
),
|
|
214
|
-
variants: Object.freeze(variants),
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
if (recipe.kind === "json-dictionary") {
|
|
218
|
-
knownKeys(
|
|
219
|
-
recipe,
|
|
220
|
-
[
|
|
221
|
-
"kind",
|
|
222
|
-
"keys",
|
|
223
|
-
"values",
|
|
224
|
-
"allowNull",
|
|
225
|
-
"maximumBytes",
|
|
226
|
-
"maximumDepth",
|
|
227
|
-
"maximumItems",
|
|
228
|
-
],
|
|
229
|
-
`${label}.recipe`,
|
|
230
|
-
);
|
|
231
|
-
if (!isObject(recipe.keys)) {
|
|
232
|
-
throw new Error(`${label}.recipe.keys must be an object.`);
|
|
233
|
-
}
|
|
234
|
-
knownKeys(
|
|
235
|
-
recipe.keys,
|
|
236
|
-
["format", "action", "minimum", "maximum", "maximumLength", "recipe"],
|
|
237
|
-
`${label}.recipe.keys`,
|
|
238
|
-
);
|
|
239
|
-
if (!DICTIONARY_KEY_FORMATS.has(recipe.keys.format)) {
|
|
240
|
-
throw new Error(`${label}.recipe.keys.format is unsupported.`);
|
|
241
|
-
}
|
|
242
|
-
if (!["KEEP", "PSEUDONYMIZE"].includes(recipe.keys.action)) {
|
|
243
|
-
throw new Error(
|
|
244
|
-
`${label}.recipe.keys.action must be KEEP or PSEUDONYMIZE.`,
|
|
245
|
-
);
|
|
246
|
-
}
|
|
247
|
-
const maximumLength =
|
|
248
|
-
recipe.keys.format === "text"
|
|
249
|
-
? positiveInteger(
|
|
250
|
-
recipe.keys.maximumLength,
|
|
251
|
-
`${label}.recipe.keys.maximumLength`,
|
|
252
|
-
1_024,
|
|
253
|
-
)
|
|
254
|
-
: undefined;
|
|
255
|
-
const integerRange =
|
|
256
|
-
recipe.keys.format === "integer"
|
|
257
|
-
? {
|
|
258
|
-
minimum: safeInteger(
|
|
259
|
-
recipe.keys.minimum,
|
|
260
|
-
`${label}.recipe.keys.minimum`,
|
|
261
|
-
),
|
|
262
|
-
maximum: safeInteger(
|
|
263
|
-
recipe.keys.maximum,
|
|
264
|
-
`${label}.recipe.keys.maximum`,
|
|
265
|
-
),
|
|
266
|
-
}
|
|
267
|
-
: null;
|
|
268
|
-
if (integerRange !== null && integerRange.minimum > integerRange.maximum) {
|
|
269
|
-
throw new Error(`${label}.recipe.keys.minimum must not exceed maximum.`);
|
|
270
|
-
}
|
|
271
|
-
if (
|
|
272
|
-
recipe.keys.format !== "integer" &&
|
|
273
|
-
(recipe.keys.minimum !== undefined || recipe.keys.maximum !== undefined)
|
|
274
|
-
) {
|
|
275
|
-
throw new Error(
|
|
276
|
-
`${label}.recipe.keys.minimum and maximum are supported only for integer keys.`,
|
|
277
|
-
);
|
|
278
|
-
}
|
|
279
|
-
if (
|
|
280
|
-
recipe.keys.format !== "text" &&
|
|
281
|
-
recipe.keys.maximumLength !== undefined
|
|
282
|
-
) {
|
|
283
|
-
throw new Error(
|
|
284
|
-
`${label}.recipe.keys.maximumLength is supported only for text keys.`,
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
const keyRecipe =
|
|
288
|
-
recipe.keys.action === "PSEUDONYMIZE"
|
|
289
|
-
? validatePseudonymRecipe(
|
|
290
|
-
recipe.keys.recipe,
|
|
291
|
-
`${label}.recipe.keys`,
|
|
292
|
-
depth + 1,
|
|
293
|
-
)
|
|
294
|
-
: null;
|
|
295
|
-
if (recipe.keys.action === "KEEP" && recipe.keys.recipe !== undefined) {
|
|
296
|
-
throw new Error(
|
|
297
|
-
`${label}.recipe.keys.recipe is not allowed when keys are kept.`,
|
|
298
|
-
);
|
|
299
|
-
}
|
|
300
|
-
const values = validateDeclaration(
|
|
301
|
-
recipe.values,
|
|
302
|
-
`${label}.recipe.values`,
|
|
303
|
-
{ depth },
|
|
304
|
-
);
|
|
305
|
-
if (!classifiesUnknownJsonValue(values)) {
|
|
306
|
-
throw new Error(
|
|
307
|
-
`${label}.recipe.values must classify value types instead of retaining unknown values.`,
|
|
308
|
-
);
|
|
309
|
-
}
|
|
310
|
-
return Object.freeze({
|
|
311
|
-
kind: "json-dictionary",
|
|
312
|
-
allowNull: recipe.allowNull === true,
|
|
313
|
-
...bounds,
|
|
314
|
-
maximumItems: positiveInteger(
|
|
315
|
-
recipe.maximumItems,
|
|
316
|
-
`${label}.recipe.maximumItems`,
|
|
317
|
-
10_000,
|
|
318
|
-
),
|
|
319
|
-
keys: Object.freeze({
|
|
320
|
-
format: recipe.keys.format,
|
|
321
|
-
action: recipe.keys.action,
|
|
322
|
-
...(integerRange === null ? {} : integerRange),
|
|
323
|
-
...(maximumLength === undefined ? {} : { maximumLength }),
|
|
324
|
-
...(keyRecipe === null ? {} : { recipe: keyRecipe }),
|
|
325
|
-
}),
|
|
326
|
-
values,
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
if (!isObject(recipe.fields) || Object.keys(recipe.fields).length === 0) {
|
|
330
|
-
throw new Error(`${label}.recipe.fields must classify JSON object keys.`);
|
|
331
|
-
}
|
|
332
|
-
knownKeys(
|
|
333
|
-
recipe,
|
|
334
|
-
["kind", "fields", "allowNull", "maximumBytes", "maximumDepth"],
|
|
335
|
-
`${label}.recipe`,
|
|
336
|
-
);
|
|
337
|
-
const fields = Object.fromEntries(
|
|
338
|
-
Object.entries(recipe.fields).map(([key, declaration]) => {
|
|
339
|
-
if (!key) {
|
|
340
|
-
throw new Error(`${label}.recipe.fields contains an invalid key.`);
|
|
341
|
-
}
|
|
342
|
-
return [
|
|
343
|
-
key,
|
|
344
|
-
validateDeclaration(declaration, `${label}.recipe.fields.${key}`, {
|
|
345
|
-
allowRequired: true,
|
|
346
|
-
depth,
|
|
347
|
-
}),
|
|
348
|
-
];
|
|
349
|
-
}),
|
|
350
|
-
);
|
|
351
|
-
return Object.freeze({
|
|
352
|
-
kind: "json-object",
|
|
353
|
-
allowNull: recipe.allowNull === true,
|
|
354
|
-
...bounds,
|
|
355
|
-
fields: Object.freeze(fields),
|
|
356
|
-
});
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
export const structuredJsonDeclarations = (recipe) => {
|
|
360
|
-
if (recipe?.kind === "json-object") return Object.values(recipe.fields);
|
|
361
|
-
if (recipe?.kind === "json-array") return [recipe.items];
|
|
362
|
-
if (recipe?.kind === "json-union") return Object.values(recipe.variants);
|
|
363
|
-
if (recipe?.kind === "json-dictionary") return [recipe.values];
|
|
364
|
-
return [];
|
|
365
|
-
};
|
|
366
|
-
|
|
367
|
-
const assertJsonBounds = (value, recipe, path) => {
|
|
368
|
-
let bytes = 0;
|
|
369
|
-
const jsonStringBytes = (string) => {
|
|
370
|
-
let size = 2;
|
|
371
|
-
for (let index = 0; index < string.length; index += 1) {
|
|
372
|
-
const code = string.charCodeAt(index);
|
|
373
|
-
if (code === 34 || code === 92) size += 2;
|
|
374
|
-
else if (code < 32) size += [8, 9, 10, 12, 13].includes(code) ? 2 : 6;
|
|
375
|
-
else if (code >= 0xd800 && code <= 0xdbff) {
|
|
376
|
-
const next = string.charCodeAt(index + 1);
|
|
377
|
-
if (next >= 0xdc00 && next <= 0xdfff) {
|
|
378
|
-
size += 4;
|
|
379
|
-
index += 1;
|
|
380
|
-
} else size += 6;
|
|
381
|
-
} else if (code >= 0xdc00 && code <= 0xdfff) size += 6;
|
|
382
|
-
else if (code <= 0x7f) size += 1;
|
|
383
|
-
else if (code <= 0x7ff) size += 2;
|
|
384
|
-
else size += 3;
|
|
385
|
-
}
|
|
386
|
-
return size;
|
|
387
|
-
};
|
|
388
|
-
const visit = (current, depth) => {
|
|
389
|
-
if (depth > recipe.maximumDepth) {
|
|
390
|
-
throw new Error(`${path} exceeds maximumDepth ${recipe.maximumDepth}.`);
|
|
391
|
-
}
|
|
392
|
-
if (typeof current === "string") bytes += jsonStringBytes(current);
|
|
393
|
-
else if (current === null) bytes += 4;
|
|
394
|
-
else if (typeof current === "number" && !Number.isFinite(current)) {
|
|
395
|
-
throw new Error(`${path} contains an unsupported JSON value.`);
|
|
396
|
-
} else if (["number", "boolean"].includes(typeof current)) {
|
|
397
|
-
bytes += String(current).length;
|
|
398
|
-
} else if (Array.isArray(current)) {
|
|
399
|
-
if (
|
|
400
|
-
recipe.kind === "json-array" &&
|
|
401
|
-
current.length > recipe.maximumItems
|
|
402
|
-
) {
|
|
403
|
-
throw new Error(`${path} exceeds maximumItems ${recipe.maximumItems}.`);
|
|
404
|
-
}
|
|
405
|
-
bytes += 2 + Math.max(0, current.length - 1);
|
|
406
|
-
for (const item of current) visit(item, depth + 1);
|
|
407
|
-
} else if (isObject(current)) {
|
|
408
|
-
bytes += 2 + Math.max(0, Object.keys(current).length - 1);
|
|
409
|
-
for (const [key, nested] of Object.entries(current)) {
|
|
410
|
-
bytes += jsonStringBytes(key) + 1;
|
|
411
|
-
visit(nested, depth + 1);
|
|
412
|
-
}
|
|
413
|
-
} else throw new Error(`${path} contains an unsupported JSON value.`);
|
|
414
|
-
if (bytes > recipe.maximumBytes) {
|
|
415
|
-
throw new Error(`${path} exceeds maximumBytes ${recipe.maximumBytes}.`);
|
|
416
|
-
}
|
|
417
|
-
};
|
|
418
|
-
visit(value, 1);
|
|
419
|
-
};
|
|
420
|
-
|
|
421
|
-
const jsonValueType = (value) => {
|
|
422
|
-
if (Array.isArray(value)) return "array";
|
|
423
|
-
if (isObject(value)) return "object";
|
|
424
|
-
if (["string", "number", "boolean"].includes(typeof value)) {
|
|
425
|
-
return typeof value;
|
|
426
|
-
}
|
|
427
|
-
return null;
|
|
428
|
-
};
|
|
429
|
-
|
|
430
|
-
const validateDictionaryKey = (value, rule, path) => {
|
|
431
|
-
if (rule.format === "integer") {
|
|
432
|
-
if (!/^(?:0|-?[1-9][0-9]{0,15})$/u.test(value)) {
|
|
433
|
-
throw new Error(`${path} contains an invalid integer key.`);
|
|
434
|
-
}
|
|
435
|
-
const integer = Number(value);
|
|
436
|
-
if (
|
|
437
|
-
!Number.isSafeInteger(integer) ||
|
|
438
|
-
String(integer) !== value ||
|
|
439
|
-
integer < rule.minimum ||
|
|
440
|
-
integer > rule.maximum
|
|
441
|
-
) {
|
|
442
|
-
throw new Error(
|
|
443
|
-
`${path} contains an integer key outside the reviewed range ${rule.minimum} through ${rule.maximum}.`,
|
|
444
|
-
);
|
|
445
|
-
}
|
|
446
|
-
return integer;
|
|
447
|
-
} else if (rule.format === "positive-integer") {
|
|
448
|
-
if (!/^[1-9][0-9]{0,18}$/u.test(value)) {
|
|
449
|
-
throw new Error(`${path} contains an invalid positive-integer key.`);
|
|
450
|
-
}
|
|
451
|
-
} else if (rule.format === "uuid") {
|
|
452
|
-
if (!UUID.test(value)) {
|
|
453
|
-
throw new Error(`${path} contains an invalid uuid key.`);
|
|
454
|
-
}
|
|
455
|
-
} else if (rule.format === "identifier") {
|
|
456
|
-
if (!IDENTIFIER.test(value)) {
|
|
457
|
-
throw new Error(`${path} contains an invalid identifier key.`);
|
|
458
|
-
}
|
|
459
|
-
} else if (
|
|
460
|
-
!value ||
|
|
461
|
-
value.length > rule.maximumLength ||
|
|
462
|
-
/[\u0000-\u001f\u007f]/u.test(value)
|
|
463
|
-
) {
|
|
464
|
-
throw new Error(`${path} contains an invalid bounded text key.`);
|
|
465
|
-
}
|
|
466
|
-
return value;
|
|
467
|
-
};
|
|
468
|
-
|
|
469
|
-
export const executeStructuredJsonDeclaration = ({
|
|
470
|
-
declaration,
|
|
471
|
-
value,
|
|
472
|
-
key,
|
|
473
|
-
path,
|
|
474
|
-
execute,
|
|
475
|
-
pseudonymize,
|
|
476
|
-
}) => {
|
|
477
|
-
const recipe = declaration.recipe;
|
|
478
|
-
if (value === null && recipe.allowNull) return null;
|
|
479
|
-
assertJsonBounds(value, recipe, path);
|
|
480
|
-
if (recipe.kind === "json-union") {
|
|
481
|
-
const type = jsonValueType(value);
|
|
482
|
-
if (type === null) {
|
|
483
|
-
throw new Error(`${path} contains an unsupported JSON value.`);
|
|
484
|
-
}
|
|
485
|
-
if (
|
|
486
|
-
["array", "object"].includes(type) &&
|
|
487
|
-
Object.keys(value).length > recipe.maximumItems
|
|
488
|
-
) {
|
|
489
|
-
throw new Error(`${path} exceeds maximumItems ${recipe.maximumItems}.`);
|
|
490
|
-
}
|
|
491
|
-
const variant = recipe.variants[type];
|
|
492
|
-
if (!variant) {
|
|
493
|
-
throw new Error(`${path} has an unreviewed JSON ${type} variant.`);
|
|
494
|
-
}
|
|
495
|
-
return execute(variant, value, path);
|
|
496
|
-
}
|
|
497
|
-
if (recipe.kind === "json-array") {
|
|
498
|
-
if (!Array.isArray(value)) throw new Error(`${path} must be a JSON array.`);
|
|
499
|
-
return value.flatMap((item, index) => {
|
|
500
|
-
const transformed = execute(recipe.items, item, `${path}[${index}]`);
|
|
501
|
-
return transformed === undefined ? [] : [transformed];
|
|
502
|
-
});
|
|
503
|
-
}
|
|
504
|
-
if (recipe.kind === "json-dictionary") {
|
|
505
|
-
if (!isObject(value)) {
|
|
506
|
-
throw new Error(`${path} must be a JSON dictionary object.`);
|
|
507
|
-
}
|
|
508
|
-
const entries = Object.entries(value);
|
|
509
|
-
if (entries.length > recipe.maximumItems) {
|
|
510
|
-
throw new Error(`${path} exceeds maximumItems ${recipe.maximumItems}.`);
|
|
511
|
-
}
|
|
512
|
-
const transformed = [];
|
|
513
|
-
const transformedKeys = new Set();
|
|
514
|
-
for (const [sourceKey, nestedValue] of entries) {
|
|
515
|
-
const canonicalSourceKey = validateDictionaryKey(
|
|
516
|
-
sourceKey,
|
|
517
|
-
recipe.keys,
|
|
518
|
-
path,
|
|
519
|
-
);
|
|
520
|
-
const destinationKey =
|
|
521
|
-
recipe.keys.action === "KEEP"
|
|
522
|
-
? sourceKey
|
|
523
|
-
: String(pseudonymize(key, recipe.keys.recipe, canonicalSourceKey));
|
|
524
|
-
if (transformedKeys.has(destinationKey)) {
|
|
525
|
-
throw new Error(`${path} produced a duplicate transformed JSON key.`);
|
|
526
|
-
}
|
|
527
|
-
transformedKeys.add(destinationKey);
|
|
528
|
-
const nested = execute(
|
|
529
|
-
recipe.values,
|
|
530
|
-
nestedValue,
|
|
531
|
-
`${path}.${sourceKey}`,
|
|
532
|
-
);
|
|
533
|
-
if (nested !== undefined) transformed.push([destinationKey, nested]);
|
|
534
|
-
}
|
|
535
|
-
return Object.fromEntries(transformed);
|
|
536
|
-
}
|
|
537
|
-
if (!isObject(value)) throw new Error(`${path} must be a JSON object.`);
|
|
538
|
-
const actual = Object.keys(value).sort();
|
|
539
|
-
const expected = Object.keys(recipe.fields);
|
|
540
|
-
if (actual.some((field) => !expected.includes(field))) {
|
|
541
|
-
throw new Error(`${path} contains an unclassified JSON key.`);
|
|
542
|
-
}
|
|
543
|
-
const missing = expected.filter(
|
|
544
|
-
(field) => recipe.fields[field].required && !Object.hasOwn(value, field),
|
|
545
|
-
);
|
|
546
|
-
if (missing.length) throw new Error(`${path} is missing required JSON keys.`);
|
|
547
|
-
return Object.fromEntries(
|
|
548
|
-
Object.entries(recipe.fields).flatMap(([field, nested]) => {
|
|
549
|
-
if (!Object.hasOwn(value, field)) return [];
|
|
550
|
-
const transformed = execute(nested, value[field], `${path}.${field}`);
|
|
551
|
-
return transformed === undefined ? [] : [[field, transformed]];
|
|
552
|
-
}),
|
|
553
|
-
);
|
|
554
|
-
};
|