@10x-media/fields 0.1.0-beta.0
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 +12 -0
- package/LICENSE +21 -0
- package/README.md +62 -0
- package/dist/exports/client.d.ts +7 -0
- package/dist/exports/client.js +8 -0
- package/dist/exports/color-utils.d.ts +9 -0
- package/dist/exports/color-utils.js +7 -0
- package/dist/exports/color.d.ts +6 -0
- package/dist/exports/color.js +5 -0
- package/dist/exports/encrypted.d.ts +12 -0
- package/dist/exports/encrypted.js +10 -0
- package/dist/exports/i18n.d.ts +3 -0
- package/dist/exports/i18n.js +3 -0
- package/dist/exports/icon-adapters/lucide.d.ts +5 -0
- package/dist/exports/icon-adapters/lucide.js +4 -0
- package/dist/exports/icon-adapters/radix.d.ts +5 -0
- package/dist/exports/icon-adapters/radix.js +4 -0
- package/dist/exports/icon-adapters/tabler.d.ts +5 -0
- package/dist/exports/icon-adapters/tabler.js +4 -0
- package/dist/exports/icon-codegen.d.ts +4 -0
- package/dist/exports/icon-codegen.js +2 -0
- package/dist/exports/icon-react.d.ts +4 -0
- package/dist/exports/icon-react.js +3 -0
- package/dist/exports/icon.d.ts +8 -0
- package/dist/exports/icon.js +7 -0
- package/dist/exports/rsc.d.ts +10 -0
- package/dist/exports/rsc.js +9 -0
- package/dist/exports/types.d.ts +4 -0
- package/dist/exports/types.js +1 -0
- package/dist/fields/color/client/ColorField.d.ts +13 -0
- package/dist/fields/color/client/ColorField.js +321 -0
- package/dist/fields/color/client/ColorField.js.map +1 -0
- package/dist/fields/color/client/ColorPickerPanel.d.ts +29 -0
- package/dist/fields/color/client/ColorPickerPanel.js +233 -0
- package/dist/fields/color/client/ColorPickerPanel.js.map +1 -0
- package/dist/fields/color/client/colorField.css +411 -0
- package/dist/fields/color/client/icons.js +38 -0
- package/dist/fields/color/client/icons.js.map +1 -0
- package/dist/fields/color/colorField.d.ts +20 -0
- package/dist/fields/color/colorField.js +102 -0
- package/dist/fields/color/colorField.js.map +1 -0
- package/dist/fields/color/commitValue.js +29 -0
- package/dist/fields/color/commitValue.js.map +1 -0
- package/dist/fields/color/engine/contrast.d.ts +10 -0
- package/dist/fields/color/engine/contrast.js +25 -0
- package/dist/fields/color/engine/contrast.js.map +1 -0
- package/dist/fields/color/engine/convert.d.ts +28 -0
- package/dist/fields/color/engine/convert.js +232 -0
- package/dist/fields/color/engine/convert.js.map +1 -0
- package/dist/fields/color/engine/format.d.ts +15 -0
- package/dist/fields/color/engine/format.js +53 -0
- package/dist/fields/color/engine/format.js.map +1 -0
- package/dist/fields/color/engine/index.d.ts +8 -0
- package/dist/fields/color/engine/namedColors.d.ts +9 -0
- package/dist/fields/color/engine/namedColors.js +159 -0
- package/dist/fields/color/engine/namedColors.js.map +1 -0
- package/dist/fields/color/engine/parse.d.ts +12 -0
- package/dist/fields/color/engine/parse.js +151 -0
- package/dist/fields/color/engine/parse.js.map +1 -0
- package/dist/fields/color/engine/salvage.d.ts +13 -0
- package/dist/fields/color/engine/salvage.js +75 -0
- package/dist/fields/color/engine/salvage.js.map +1 -0
- package/dist/fields/color/engine/types.d.ts +34 -0
- package/dist/fields/color/options.d.ts +55 -0
- package/dist/fields/color/options.js +8 -0
- package/dist/fields/color/options.js.map +1 -0
- package/dist/fields/color/presetChip.js +27 -0
- package/dist/fields/color/presetChip.js.map +1 -0
- package/dist/fields/color/presetsFromDoc.d.ts +27 -0
- package/dist/fields/color/presetsFromDoc.js +74 -0
- package/dist/fields/color/presetsFromDoc.js.map +1 -0
- package/dist/fields/color/resolveFormat.js +16 -0
- package/dist/fields/color/resolveFormat.js.map +1 -0
- package/dist/fields/color/resolvePresets.d.ts +29 -0
- package/dist/fields/color/resolvePresets.js +39 -0
- package/dist/fields/color/resolvePresets.js.map +1 -0
- package/dist/fields/color/server/ColorCell.d.ts +6 -0
- package/dist/fields/color/server/ColorCell.js +34 -0
- package/dist/fields/color/server/ColorCell.js.map +1 -0
- package/dist/fields/color/server/ColorFieldServer.d.ts +18 -0
- package/dist/fields/color/server/ColorFieldServer.js +69 -0
- package/dist/fields/color/server/ColorFieldServer.js.map +1 -0
- package/dist/fields/color/server/colorCell.css +32 -0
- package/dist/fields/encrypted/boot.d.ts +14 -0
- package/dist/fields/encrypted/boot.js +22 -0
- package/dist/fields/encrypted/boot.js.map +1 -0
- package/dist/fields/encrypted/components/EncryptedInput.js +89 -0
- package/dist/fields/encrypted/components/EncryptedInput.js.map +1 -0
- package/dist/fields/encrypted/components/EncryptedTextarea.js +86 -0
- package/dist/fields/encrypted/components/EncryptedTextarea.js.map +1 -0
- package/dist/fields/encrypted/components/EyeToggle.js +51 -0
- package/dist/fields/encrypted/components/EyeToggle.js.map +1 -0
- package/dist/fields/encrypted/components/MaskDots.js +20 -0
- package/dist/fields/encrypted/components/MaskDots.js.map +1 -0
- package/dist/fields/encrypted/components/ProtectedCell.d.ts +19 -0
- package/dist/fields/encrypted/components/ProtectedCell.js +44 -0
- package/dist/fields/encrypted/components/ProtectedCell.js.map +1 -0
- package/dist/fields/encrypted/components/ProtectedField.css +180 -0
- package/dist/fields/encrypted/components/ProtectedField.d.ts +30 -0
- package/dist/fields/encrypted/components/ProtectedField.js +95 -0
- package/dist/fields/encrypted/components/ProtectedField.js.map +1 -0
- package/dist/fields/encrypted/components/ProtectedRichText.d.ts +21 -0
- package/dist/fields/encrypted/components/ProtectedRichText.js +35 -0
- package/dist/fields/encrypted/components/ProtectedRichText.js.map +1 -0
- package/dist/fields/encrypted/components/RichTextRevealGate.d.ts +24 -0
- package/dist/fields/encrypted/components/RichTextRevealGate.js +87 -0
- package/dist/fields/encrypted/components/RichTextRevealGate.js.map +1 -0
- package/dist/fields/encrypted/components/StructuralField.js +64 -0
- package/dist/fields/encrypted/components/StructuralField.js.map +1 -0
- package/dist/fields/encrypted/components/facsimiles.js +219 -0
- package/dist/fields/encrypted/components/facsimiles.js.map +1 -0
- package/dist/fields/encrypted/components/placement.js +21 -0
- package/dist/fields/encrypted/components/placement.js.map +1 -0
- package/dist/fields/encrypted/crypto/aad.js +29 -0
- package/dist/fields/encrypted/crypto/aad.js.map +1 -0
- package/dist/fields/encrypted/crypto/bidx.js +26 -0
- package/dist/fields/encrypted/crypto/bidx.js.map +1 -0
- package/dist/fields/encrypted/crypto/keys.d.ts +31 -0
- package/dist/fields/encrypted/crypto/keys.js +113 -0
- package/dist/fields/encrypted/crypto/keys.js.map +1 -0
- package/dist/fields/encrypted/crypto/wire.d.ts +23 -0
- package/dist/fields/encrypted/crypto/wire.js +135 -0
- package/dist/fields/encrypted/crypto/wire.js.map +1 -0
- package/dist/fields/encrypted/encryptedField.d.ts +15 -0
- package/dist/fields/encrypted/encryptedField.js +187 -0
- package/dist/fields/encrypted/encryptedField.js.map +1 -0
- package/dist/fields/encrypted/hooks.d.ts +21 -0
- package/dist/fields/encrypted/hooks.js +326 -0
- package/dist/fields/encrypted/hooks.js.map +1 -0
- package/dist/fields/encrypted/maskDots.js +16 -0
- package/dist/fields/encrypted/maskDots.js.map +1 -0
- package/dist/fields/encrypted/plaintextStash.js +57 -0
- package/dist/fields/encrypted/plaintextStash.js.map +1 -0
- package/dist/fields/encrypted/queryRewrite.d.ts +12 -0
- package/dist/fields/encrypted/queryRewrite.js +134 -0
- package/dist/fields/encrypted/queryRewrite.js.map +1 -0
- package/dist/fields/encrypted/scan.js +36 -0
- package/dist/fields/encrypted/scan.js.map +1 -0
- package/dist/fields/encrypted/types.d.ts +35 -0
- package/dist/fields/encrypted/types.js +12 -0
- package/dist/fields/encrypted/types.js.map +1 -0
- package/dist/fields/encrypted/typescriptSchema.js +41 -0
- package/dist/fields/encrypted/typescriptSchema.js.map +1 -0
- package/dist/fields/encrypted/utils/decryptAllData.d.ts +17 -0
- package/dist/fields/encrypted/utils/decryptAllData.js +98 -0
- package/dist/fields/encrypted/utils/decryptAllData.js.map +1 -0
- package/dist/fields/encrypted/utils/encryptExistingData.d.ts +14 -0
- package/dist/fields/encrypted/utils/encryptExistingData.js +72 -0
- package/dist/fields/encrypted/utils/encryptExistingData.js.map +1 -0
- package/dist/fields/encrypted/utils/pageThrough.js +42 -0
- package/dist/fields/encrypted/utils/pageThrough.js.map +1 -0
- package/dist/fields/encrypted/utils/rotateEncryptedFields.d.ts +27 -0
- package/dist/fields/encrypted/utils/rotateEncryptedFields.js +92 -0
- package/dist/fields/encrypted/utils/rotateEncryptedFields.js.map +1 -0
- package/dist/fields/encrypted/validators.js +111 -0
- package/dist/fields/encrypted/validators.js.map +1 -0
- package/dist/fields/icon/adapters/lucide/adapter.d.ts +8 -0
- package/dist/fields/icon/adapters/lucide/adapter.js +15 -0
- package/dist/fields/icon/adapters/lucide/adapter.js.map +1 -0
- package/dist/fields/icon/adapters/lucide/client.d.ts +10 -0
- package/dist/fields/icon/adapters/lucide/client.js +41 -0
- package/dist/fields/icon/adapters/lucide/client.js.map +1 -0
- package/dist/fields/icon/adapters/lucide/generated/manifest.js +23338 -0
- package/dist/fields/icon/adapters/lucide/generated/manifest.js.map +1 -0
- package/dist/fields/icon/adapters/lucide/generated/nodes.js +14296 -0
- package/dist/fields/icon/adapters/lucide/generated/nodes.js.map +1 -0
- package/dist/fields/icon/adapters/lucide/renderer.d.ts +7 -0
- package/dist/fields/icon/adapters/lucide/renderer.js +13 -0
- package/dist/fields/icon/adapters/lucide/renderer.js.map +1 -0
- package/dist/fields/icon/adapters/radix/adapter.d.ts +8 -0
- package/dist/fields/icon/adapters/radix/adapter.js +14 -0
- package/dist/fields/icon/adapters/radix/adapter.js.map +1 -0
- package/dist/fields/icon/adapters/radix/client.d.ts +8 -0
- package/dist/fields/icon/adapters/radix/client.js +20 -0
- package/dist/fields/icon/adapters/radix/client.js.map +1 -0
- package/dist/fields/icon/adapters/radix/generated/imports.js +324 -0
- package/dist/fields/icon/adapters/radix/generated/imports.js.map +1 -0
- package/dist/fields/icon/adapters/radix/generated/manifest.js +1805 -0
- package/dist/fields/icon/adapters/radix/generated/manifest.js.map +1 -0
- package/dist/fields/icon/adapters/radix/renderer.d.ts +7 -0
- package/dist/fields/icon/adapters/radix/renderer.js +23 -0
- package/dist/fields/icon/adapters/radix/renderer.js.map +1 -0
- package/dist/fields/icon/adapters/tabler/adapter.d.ts +8 -0
- package/dist/fields/icon/adapters/tabler/adapter.js +15 -0
- package/dist/fields/icon/adapters/tabler/adapter.js.map +1 -0
- package/dist/fields/icon/adapters/tabler/client.d.ts +9 -0
- package/dist/fields/icon/adapters/tabler/client.js +18 -0
- package/dist/fields/icon/adapters/tabler/client.js.map +1 -0
- package/dist/fields/icon/adapters/tabler/generated/imports.js +4942 -0
- package/dist/fields/icon/adapters/tabler/generated/imports.js.map +1 -0
- package/dist/fields/icon/adapters/tabler/generated/manifest.js +61269 -0
- package/dist/fields/icon/adapters/tabler/generated/manifest.js.map +1 -0
- package/dist/fields/icon/adapters/tabler/generated/nodes.js +26749 -0
- package/dist/fields/icon/adapters/tabler/generated/nodes.js.map +1 -0
- package/dist/fields/icon/adapters/tabler/renderer.d.ts +7 -0
- package/dist/fields/icon/adapters/tabler/renderer.js +13 -0
- package/dist/fields/icon/adapters/tabler/renderer.js.map +1 -0
- package/dist/fields/icon/client/DrawerGlyph.js +32 -0
- package/dist/fields/icon/client/DrawerGlyph.js.map +1 -0
- package/dist/fields/icon/client/IconDrawerContent.js +294 -0
- package/dist/fields/icon/client/IconDrawerContent.js.map +1 -0
- package/dist/fields/icon/client/IconFieldClient.d.ts +19 -0
- package/dist/fields/icon/client/IconFieldClient.js +245 -0
- package/dist/fields/icon/client/IconFieldClient.js.map +1 -0
- package/dist/fields/icon/client/IconLibrarySelect.d.ts +14 -0
- package/dist/fields/icon/client/IconLibrarySelect.js +36 -0
- package/dist/fields/icon/client/IconLibrarySelect.js.map +1 -0
- package/dist/fields/icon/client/availableLibraries.js +25 -0
- package/dist/fields/icon/client/availableLibraries.js.map +1 -0
- package/dist/fields/icon/client/generatedAdapter.js +88 -0
- package/dist/fields/icon/client/generatedAdapter.js.map +1 -0
- package/dist/fields/icon/client/icon-field.css +524 -0
- package/dist/fields/icon/client/useRecentIcons.js +32 -0
- package/dist/fields/icon/client/useRecentIcons.js.map +1 -0
- package/dist/fields/icon/codegen/emit.js +81 -0
- package/dist/fields/icon/codegen/emit.js.map +1 -0
- package/dist/fields/icon/codegen/generate.d.ts +10 -0
- package/dist/fields/icon/codegen/generate.js +59 -0
- package/dist/fields/icon/codegen/generate.js.map +1 -0
- package/dist/fields/icon/codegen/sources/lucide.js +52 -0
- package/dist/fields/icon/codegen/sources/lucide.js.map +1 -0
- package/dist/fields/icon/codegen/sources/radix.js +33 -0
- package/dist/fields/icon/codegen/sources/radix.js.map +1 -0
- package/dist/fields/icon/codegen/sources/tabler.js +31 -0
- package/dist/fields/icon/codegen/sources/tabler.js.map +1 -0
- package/dist/fields/icon/codegen/types.d.ts +36 -0
- package/dist/fields/icon/defineIconAdapter.d.ts +8 -0
- package/dist/fields/icon/defineIconAdapter.js +10 -0
- package/dist/fields/icon/defineIconAdapter.js.map +1 -0
- package/dist/fields/icon/plugin.js +55 -0
- package/dist/fields/icon/plugin.js.map +1 -0
- package/dist/fields/icon/react/createIcon.d.ts +15 -0
- package/dist/fields/icon/react/createIcon.js +47 -0
- package/dist/fields/icon/react/createIcon.js.map +1 -0
- package/dist/fields/icon/react/createRscIcon.d.ts +12 -0
- package/dist/fields/icon/react/createRscIcon.js +27 -0
- package/dist/fields/icon/react/createRscIcon.js.map +1 -0
- package/dist/fields/icon/react/types.d.ts +24 -0
- package/dist/fields/icon/rsc/IconCell.d.ts +14 -0
- package/dist/fields/icon/rsc/IconCell.js +32 -0
- package/dist/fields/icon/rsc/IconCell.js.map +1 -0
- package/dist/fields/icon/rsc/IconFieldServer.d.ts +24 -0
- package/dist/fields/icon/rsc/IconFieldServer.js +74 -0
- package/dist/fields/icon/rsc/IconFieldServer.js.map +1 -0
- package/dist/fields/icon/rsc/IconLibrarySelectServer.d.ts +13 -0
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js +24 -0
- package/dist/fields/icon/rsc/IconLibrarySelectServer.js.map +1 -0
- package/dist/fields/icon/rsc/resolveStaticLabel.js +16 -0
- package/dist/fields/icon/rsc/resolveStaticLabel.js.map +1 -0
- package/dist/fields/icon/server/availability.d.ts +32 -0
- package/dist/fields/icon/server/availability.js +58 -0
- package/dist/fields/icon/server/availability.js.map +1 -0
- package/dist/fields/icon/server/iconField.d.ts +32 -0
- package/dist/fields/icon/server/iconField.js +48 -0
- package/dist/fields/icon/server/iconField.js.map +1 -0
- package/dist/fields/icon/server/iconLibraryField.d.ts +23 -0
- package/dist/fields/icon/server/iconLibraryField.js +50 -0
- package/dist/fields/icon/server/iconLibraryField.js.map +1 -0
- package/dist/fields/icon/server/libraryOptions.d.ts +12 -0
- package/dist/fields/icon/server/libraryOptions.js +13 -0
- package/dist/fields/icon/server/libraryOptions.js.map +1 -0
- package/dist/fields/icon/server/manifestCache.js +17 -0
- package/dist/fields/icon/server/manifestCache.js.map +1 -0
- package/dist/fields/icon/server/validate.js +37 -0
- package/dist/fields/icon/server/validate.js.map +1 -0
- package/dist/fields/icon/shared/adapterComponents.d.ts +23 -0
- package/dist/fields/icon/shared/formatIconLabel.js +16 -0
- package/dist/fields/icon/shared/formatIconLabel.js.map +1 -0
- package/dist/fields/icon/shared/search.js +58 -0
- package/dist/fields/icon/shared/search.js.map +1 -0
- package/dist/fields/icon/shared/value.d.ts +16 -0
- package/dist/fields/icon/shared/value.js +27 -0
- package/dist/fields/icon/shared/value.js.map +1 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin/registerTranslations.js +19 -0
- package/dist/plugin/registerTranslations.js.map +1 -0
- package/dist/plugin/registry.d.ts +9 -0
- package/dist/plugin/registry.js +14 -0
- package/dist/plugin/registry.js.map +1 -0
- package/dist/translations/de.js +49 -0
- package/dist/translations/de.js.map +1 -0
- package/dist/translations/en.js +49 -0
- package/dist/translations/en.js.map +1 -0
- package/dist/translations/index.d.ts +15 -0
- package/dist/translations/index.js +30 -0
- package/dist/translations/index.js.map +1 -0
- package/dist/translations/keys.d.ts +48 -0
- package/dist/translations/keys.js +48 -0
- package/dist/translations/keys.js.map +1 -0
- package/dist/translations/server.js +13 -0
- package/dist/translations/server.js.map +1 -0
- package/dist/translations/useTranslation.js +12 -0
- package/dist/translations/useTranslation.js.map +1 -0
- package/dist/types.d.ts +105 -0
- package/dist/utils/memoForRequest.js +25 -0
- package/dist/utils/memoForRequest.js.map +1 -0
- package/package.json +196 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { getEncryptedMarker } from "./types.js";
|
|
2
|
+
//#region src/fields/encrypted/scan.ts
|
|
3
|
+
/**
|
|
4
|
+
* Walks named containers (group, named tabs) and layout containers (row,
|
|
5
|
+
* collapsible, unnamed tabs) collecting encrypted markers by dot path.
|
|
6
|
+
* Fields inside arrays and blocks encrypt at runtime but are excluded here:
|
|
7
|
+
* query rewrite and bulk utilities do not address per-row paths in v1.
|
|
8
|
+
*/
|
|
9
|
+
const scanEncryptedFields = (fields, prefix = "") => {
|
|
10
|
+
const found = /* @__PURE__ */ new Map();
|
|
11
|
+
for (const field of fields ?? []) {
|
|
12
|
+
if ("name" in field && typeof field.name === "string") {
|
|
13
|
+
const marker = getEncryptedMarker(field);
|
|
14
|
+
if (marker) {
|
|
15
|
+
found.set(`${prefix}${field.name}`, marker);
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (field.type === "group" && "name" in field && field.name) for (const [path, marker] of scanEncryptedFields(field.fields, `${prefix}${field.name}.`)) found.set(path, marker);
|
|
20
|
+
else if (field.type === "row" || field.type === "collapsible") for (const [path, marker] of scanEncryptedFields(field.fields, prefix)) found.set(path, marker);
|
|
21
|
+
else if (field.type === "tabs") for (const tab of field.tabs) {
|
|
22
|
+
const tabPrefix = "name" in tab && tab.name ? `${prefix}${tab.name}.` : prefix;
|
|
23
|
+
for (const [path, marker] of scanEncryptedFields(tab.fields, tabPrefix)) found.set(path, marker);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return found;
|
|
27
|
+
};
|
|
28
|
+
const queryableOnly = (markers) => {
|
|
29
|
+
const out = /* @__PURE__ */ new Map();
|
|
30
|
+
for (const [path, marker] of markers) if (marker.queryable && marker.bidxName) out.set(path, marker);
|
|
31
|
+
return out;
|
|
32
|
+
};
|
|
33
|
+
//#endregion
|
|
34
|
+
export { queryableOnly, scanEncryptedFields };
|
|
35
|
+
|
|
36
|
+
//# sourceMappingURL=scan.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scan.js","names":[],"sources":["../../../src/fields/encrypted/scan.ts"],"sourcesContent":["import type { Field } from 'payload'\nimport { type EncryptedFieldMarker, getEncryptedMarker } from './types'\n\n/**\n * Walks named containers (group, named tabs) and layout containers (row,\n * collapsible, unnamed tabs) collecting encrypted markers by dot path.\n * Fields inside arrays and blocks encrypt at runtime but are excluded here:\n * query rewrite and bulk utilities do not address per-row paths in v1.\n */\nexport const scanEncryptedFields = (\n\tfields: Field[] | undefined,\n\tprefix = ''\n): Map<string, EncryptedFieldMarker> => {\n\tconst found = new Map<string, EncryptedFieldMarker>()\n\tfor (const field of fields ?? []) {\n\t\tif ('name' in field && typeof field.name === 'string') {\n\t\t\tconst marker = getEncryptedMarker(field as { custom?: Record<string, unknown> })\n\t\t\tif (marker) {\n\t\t\t\tfound.set(`${prefix}${field.name}`, marker)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\t\tif (field.type === 'group' && 'name' in field && field.name) {\n\t\t\tfor (const [path, marker] of scanEncryptedFields(field.fields, `${prefix}${field.name}.`)) {\n\t\t\t\tfound.set(path, marker)\n\t\t\t}\n\t\t} else if (field.type === 'row' || field.type === 'collapsible') {\n\t\t\tfor (const [path, marker] of scanEncryptedFields(field.fields, prefix)) {\n\t\t\t\tfound.set(path, marker)\n\t\t\t}\n\t\t} else if (field.type === 'tabs') {\n\t\t\tfor (const tab of field.tabs) {\n\t\t\t\tconst tabPrefix = 'name' in tab && tab.name ? `${prefix}${tab.name}.` : prefix\n\t\t\t\tfor (const [path, marker] of scanEncryptedFields(tab.fields, tabPrefix)) {\n\t\t\t\t\tfound.set(path, marker)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn found\n}\n\nexport const queryableOnly = (\n\tmarkers: Map<string, EncryptedFieldMarker>\n): Map<string, EncryptedFieldMarker> => {\n\tconst out = new Map<string, EncryptedFieldMarker>()\n\tfor (const [path, marker] of markers) {\n\t\tif (marker.queryable && marker.bidxName) {\n\t\t\tout.set(path, marker)\n\t\t}\n\t}\n\treturn out\n}\n"],"mappings":";;;;;;;;AASA,MAAa,uBACZ,QACA,SAAS,OAC8B;CACvC,MAAM,wBAAQ,IAAI,IAAkC;CACpD,KAAK,MAAM,SAAS,UAAU,CAAC,GAAG;EACjC,IAAI,UAAU,SAAS,OAAO,MAAM,SAAS,UAAU;GACtD,MAAM,SAAS,mBAAmB,KAA6C;GAC/E,IAAI,QAAQ;IACX,MAAM,IAAI,GAAG,SAAS,MAAM,QAAQ,MAAM;IAC1C;GACD;EACD;EACA,IAAI,MAAM,SAAS,WAAW,UAAU,SAAS,MAAM,MACtD,KAAK,MAAM,CAAC,MAAM,WAAW,oBAAoB,MAAM,QAAQ,GAAG,SAAS,MAAM,KAAK,EAAE,GACvF,MAAM,IAAI,MAAM,MAAM;OAEjB,IAAI,MAAM,SAAS,SAAS,MAAM,SAAS,eACjD,KAAK,MAAM,CAAC,MAAM,WAAW,oBAAoB,MAAM,QAAQ,MAAM,GACpE,MAAM,IAAI,MAAM,MAAM;OAEjB,IAAI,MAAM,SAAS,QACzB,KAAK,MAAM,OAAO,MAAM,MAAM;GAC7B,MAAM,YAAY,UAAU,OAAO,IAAI,OAAO,GAAG,SAAS,IAAI,KAAK,KAAK;GACxE,KAAK,MAAM,CAAC,MAAM,WAAW,oBAAoB,IAAI,QAAQ,SAAS,GACrE,MAAM,IAAI,MAAM,MAAM;EAExB;CAEF;CACA,OAAO;AACR;AAEA,MAAa,iBACZ,YACuC;CACvC,MAAM,sBAAM,IAAI,IAAkC;CAClD,KAAK,MAAM,CAAC,MAAM,WAAW,SAC5B,IAAI,OAAO,aAAa,OAAO,UAC9B,IAAI,IAAI,MAAM,MAAM;CAGtB,OAAO;AACR"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { DecryptFailurePolicy, KeysConfig } from "../../types.js";
|
|
2
|
+
import { CheckboxField, CodeField, DateField, EmailField, JSONField, NumberField, PointField, RadioField, RichTextField, SelectField, TextField, TextareaField } from "payload";
|
|
3
|
+
|
|
4
|
+
//#region src/fields/encrypted/types.d.ts
|
|
5
|
+
type EncryptedSourceField = CheckboxField | CodeField | DateField | EmailField | JSONField | NumberField | PointField | RadioField | RichTextField | SelectField | TextareaField | TextField;
|
|
6
|
+
type EncryptedSourceType = EncryptedSourceField['type'];
|
|
7
|
+
type EncryptedProtection = 'masked' | 'none';
|
|
8
|
+
interface EncryptedFieldOptions {
|
|
9
|
+
keys?: KeysConfig;
|
|
10
|
+
/**
|
|
11
|
+
* Cosmetic number of dots shown while the field is concealed in the admin.
|
|
12
|
+
* Decoupled from the real value length (unknown until reveal). Clamped to
|
|
13
|
+
* [1, 64] at the factory; defaults to 8. Ignored by the checkbox facsimile.
|
|
14
|
+
*/
|
|
15
|
+
maskDots?: number;
|
|
16
|
+
onDecryptFailure?: DecryptFailurePolicy;
|
|
17
|
+
overrides?: (args: {
|
|
18
|
+
field: TextField;
|
|
19
|
+
}) => TextField;
|
|
20
|
+
protection?: EncryptedProtection;
|
|
21
|
+
queryable?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** Serializable subset shipped to ProtectedField via clientProps. */
|
|
24
|
+
interface EncryptedFieldPatch {
|
|
25
|
+
admin?: Record<string, unknown>;
|
|
26
|
+
hasMany?: boolean;
|
|
27
|
+
options?: {
|
|
28
|
+
label: string;
|
|
29
|
+
value: string;
|
|
30
|
+
}[];
|
|
31
|
+
type: EncryptedSourceType;
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { EncryptedFieldOptions, EncryptedFieldPatch, EncryptedProtection, EncryptedSourceField };
|
|
35
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/fields/encrypted/types.ts
|
|
2
|
+
/** Request-context modes the encrypted hooks respect (utilities set them). */
|
|
3
|
+
const ENCRYPTED_CONTEXT_KEY = "tenxFieldsEncrypted";
|
|
4
|
+
const FIELDS_CUSTOM_KEY = "@10x-media/fields";
|
|
5
|
+
const getEncryptedMarker = (field) => {
|
|
6
|
+
const entry = field.custom?.[FIELDS_CUSTOM_KEY];
|
|
7
|
+
if (entry && typeof entry === "object" && "encrypted" in entry) return entry.encrypted;
|
|
8
|
+
};
|
|
9
|
+
//#endregion
|
|
10
|
+
export { ENCRYPTED_CONTEXT_KEY, FIELDS_CUSTOM_KEY, getEncryptedMarker };
|
|
11
|
+
|
|
12
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","names":[],"sources":["../../../src/fields/encrypted/types.ts"],"sourcesContent":["import type {\n\tCheckboxField,\n\tCodeField,\n\tDateField,\n\tEmailField,\n\tJSONField,\n\tNumberField,\n\tPointField,\n\tRadioField,\n\tRichTextField,\n\tSelectField,\n\tTextareaField,\n\tTextField,\n} from 'payload'\nimport type { DecryptFailurePolicy, KeysConfig } from '../../types'\nimport type { BidxNormalize } from './crypto/bidx'\n\nexport type EncryptedSourceField =\n\t| CheckboxField\n\t| CodeField\n\t| DateField\n\t| EmailField\n\t| JSONField\n\t| NumberField\n\t| PointField\n\t| RadioField\n\t| RichTextField\n\t| SelectField\n\t| TextareaField\n\t| TextField\n\nexport type EncryptedSourceType = EncryptedSourceField['type']\n\nexport type EncryptedProtection = 'masked' | 'none'\n\nexport interface EncryptedFieldOptions {\n\tkeys?: KeysConfig\n\t/**\n\t * Cosmetic number of dots shown while the field is concealed in the admin.\n\t * Decoupled from the real value length (unknown until reveal). Clamped to\n\t * [1, 64] at the factory; defaults to 8. Ignored by the checkbox facsimile.\n\t */\n\tmaskDots?: number\n\tonDecryptFailure?: DecryptFailurePolicy\n\toverrides?: (args: { field: TextField }) => TextField\n\tprotection?: EncryptedProtection\n\tqueryable?: boolean\n}\n\n/** Request-context modes the encrypted hooks respect (utilities set them). */\nexport const ENCRYPTED_CONTEXT_KEY = 'tenxFieldsEncrypted'\nexport type EncryptedContextMode = 'decrypt' | 'raw' | 'rotate'\n\nexport const FIELDS_CUSTOM_KEY = '@10x-media/fields'\n\n/**\n * Server-side marker the factory stamps on `field.custom`. `custom` is in\n * Payload's serverOnlyFieldProperties list, so KeysConfig functions never\n * reach the client. Consumed by the query rewrite, boot check, and utilities.\n */\nexport interface EncryptedFieldMarker {\n\tbidxName?: string\n\tfieldName: string\n\thasMany: boolean\n\tkeys?: KeysConfig\n\tlocalized: boolean\n\tnormalize: BidxNormalize\n\tonDecryptFailure?: DecryptFailurePolicy\n\tqueryable: boolean\n\tsourceType: EncryptedSourceType\n}\n\n/** Serializable subset shipped to ProtectedField via clientProps. */\nexport interface EncryptedFieldPatch {\n\tadmin?: Record<string, unknown>\n\thasMany?: boolean\n\toptions?: { label: string; value: string }[]\n\ttype: EncryptedSourceType\n}\n\nexport const getEncryptedMarker = (field: {\n\tcustom?: Record<string, unknown>\n}): EncryptedFieldMarker | undefined => {\n\tconst entry = field.custom?.[FIELDS_CUSTOM_KEY]\n\tif (entry && typeof entry === 'object' && 'encrypted' in entry) {\n\t\treturn (entry as { encrypted: EncryptedFieldMarker }).encrypted\n\t}\n\treturn undefined\n}\n"],"mappings":";;AAkDA,MAAa,wBAAwB;AAGrC,MAAa,oBAAoB;AA2BjC,MAAa,sBAAsB,UAEK;CACvC,MAAM,QAAQ,MAAM,SAAS;CAC7B,IAAI,SAAS,OAAO,UAAU,YAAY,eAAe,OACxD,OAAQ,MAA8C;AAGxD"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//#region src/fields/encrypted/typescriptSchema.ts
|
|
2
|
+
const optionValues = (source) => "options" in source ? source.options.map((option) => typeof option === "string" ? option : option.value) : [];
|
|
3
|
+
const baseSchema = (source) => {
|
|
4
|
+
switch (source.type) {
|
|
5
|
+
case "checkbox": return { type: "boolean" };
|
|
6
|
+
case "number": return { type: "number" };
|
|
7
|
+
case "json":
|
|
8
|
+
case "richText": return {
|
|
9
|
+
additionalProperties: true,
|
|
10
|
+
type: "object"
|
|
11
|
+
};
|
|
12
|
+
case "point": return {
|
|
13
|
+
items: { type: "number" },
|
|
14
|
+
maxItems: 2,
|
|
15
|
+
minItems: 2,
|
|
16
|
+
type: "array"
|
|
17
|
+
};
|
|
18
|
+
case "radio":
|
|
19
|
+
case "select": return {
|
|
20
|
+
enum: optionValues(source),
|
|
21
|
+
type: "string"
|
|
22
|
+
};
|
|
23
|
+
default: return { type: "string" };
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* The stored field is text, but generated TypeScript must keep the source
|
|
28
|
+
* type's shape (number stays number, checkbox stays boolean, ...).
|
|
29
|
+
*/
|
|
30
|
+
const typescriptSchemaFor = (source) => {
|
|
31
|
+
const single = baseSchema(source);
|
|
32
|
+
const schema = "hasMany" in source && source.hasMany === true ? {
|
|
33
|
+
items: single,
|
|
34
|
+
type: "array"
|
|
35
|
+
} : single;
|
|
36
|
+
return [() => schema];
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
export { typescriptSchemaFor };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=typescriptSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typescriptSchema.js","names":[],"sources":["../../../src/fields/encrypted/typescriptSchema.ts"],"sourcesContent":["import type { JSONSchema4 } from 'json-schema'\nimport type { EncryptedSourceField } from './types'\n\nconst optionValues = (source: EncryptedSourceField): string[] =>\n\t'options' in source\n\t\t? source.options.map((option) => (typeof option === 'string' ? option : option.value))\n\t\t: []\n\nconst baseSchema = (source: EncryptedSourceField): JSONSchema4 => {\n\tswitch (source.type) {\n\t\tcase 'checkbox':\n\t\t\treturn { type: 'boolean' }\n\t\tcase 'number':\n\t\t\treturn { type: 'number' }\n\t\tcase 'json':\n\t\tcase 'richText':\n\t\t\treturn { additionalProperties: true, type: 'object' }\n\t\tcase 'point':\n\t\t\treturn { items: { type: 'number' }, maxItems: 2, minItems: 2, type: 'array' }\n\t\tcase 'radio':\n\t\tcase 'select':\n\t\t\treturn { enum: optionValues(source), type: 'string' }\n\t\tdefault:\n\t\t\treturn { type: 'string' }\n\t}\n}\n\n/**\n * The stored field is text, but generated TypeScript must keep the source\n * type's shape (number stays number, checkbox stays boolean, ...).\n */\nexport const typescriptSchemaFor = (\n\tsource: EncryptedSourceField\n): Array<(args: { jsonSchema: JSONSchema4 }) => JSONSchema4> => {\n\tconst single = baseSchema(source)\n\tconst schema: JSONSchema4 =\n\t\t'hasMany' in source && source.hasMany === true ? { items: single, type: 'array' } : single\n\treturn [() => schema]\n}\n"],"mappings":";AAGA,MAAM,gBAAgB,WACrB,aAAa,SACV,OAAO,QAAQ,KAAK,WAAY,OAAO,WAAW,WAAW,SAAS,OAAO,KAAM,IACnF,CAAC;AAEL,MAAM,cAAc,WAA8C;CACjE,QAAQ,OAAO,MAAf;EACC,KAAK,YACJ,OAAO,EAAE,MAAM,UAAU;EAC1B,KAAK,UACJ,OAAO,EAAE,MAAM,SAAS;EACzB,KAAK;EACL,KAAK,YACJ,OAAO;GAAE,sBAAsB;GAAM,MAAM;EAAS;EACrD,KAAK,SACJ,OAAO;GAAE,OAAO,EAAE,MAAM,SAAS;GAAG,UAAU;GAAG,UAAU;GAAG,MAAM;EAAQ;EAC7E,KAAK;EACL,KAAK,UACJ,OAAO;GAAE,MAAM,aAAa,MAAM;GAAG,MAAM;EAAS;EACrD,SACC,OAAO,EAAE,MAAM,SAAS;CAC1B;AACD;;;;;AAMA,MAAa,uBACZ,WAC+D;CAC/D,MAAM,SAAS,WAAW,MAAM;CAChC,MAAM,SACL,aAAa,UAAU,OAAO,YAAY,OAAO;EAAE,OAAO;EAAQ,MAAM;CAAQ,IAAI;CACrF,OAAO,OAAO,MAAM;AACrB"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { RotateOptions, RotateReport } from "./rotateEncryptedFields.js";
|
|
2
|
+
import { Payload } from "payload";
|
|
3
|
+
|
|
4
|
+
//#region src/fields/encrypted/utils/decryptAllData.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Removal path: unseals every sealed value and stores it as plaintext, with
|
|
7
|
+
* hooks in 'decrypt' mode so nothing re-seals. Non-string plaintexts are
|
|
8
|
+
* stored JSON-stringified (the backing column is text); swapping the field
|
|
9
|
+
* back to its native type afterwards is a schema migration the consumer owns
|
|
10
|
+
* (documented in the removal guide). The decrypt-mode beforeChange hook nulls
|
|
11
|
+
* each blind-index sibling at its own container scope, so this never writes the
|
|
12
|
+
* index by hand (a flat write would mis-scope a nested field's index).
|
|
13
|
+
*/
|
|
14
|
+
declare const decryptAllData: (payload: Payload, options?: RotateOptions) => Promise<RotateReport>;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { decryptAllData };
|
|
17
|
+
//# sourceMappingURL=decryptAllData.d.ts.map
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { resolveKeys } from "../crypto/keys.js";
|
|
2
|
+
import { ENCRYPTED_CONTEXT_KEY } from "../types.js";
|
|
3
|
+
import { scanEncryptedFields } from "../scan.js";
|
|
4
|
+
import { getFieldsRegistry } from "../../../plugin/registry.js";
|
|
5
|
+
import { isSealed, unseal } from "../crypto/wire.js";
|
|
6
|
+
import { getAtPath, pageThrough, setAtPath } from "./pageThrough.js";
|
|
7
|
+
//#region src/fields/encrypted/utils/decryptAllData.ts
|
|
8
|
+
/**
|
|
9
|
+
* Removal path: unseals every sealed value and stores it as plaintext, with
|
|
10
|
+
* hooks in 'decrypt' mode so nothing re-seals. Non-string plaintexts are
|
|
11
|
+
* stored JSON-stringified (the backing column is text); swapping the field
|
|
12
|
+
* back to its native type afterwards is a schema migration the consumer owns
|
|
13
|
+
* (documented in the removal guide). The decrypt-mode beforeChange hook nulls
|
|
14
|
+
* each blind-index sibling at its own container scope, so this never writes the
|
|
15
|
+
* index by hand (a flat write would mis-scope a nested field's index).
|
|
16
|
+
*/
|
|
17
|
+
const decryptAllData = async (payload, options = {}) => {
|
|
18
|
+
const { batchSize = 100, collections, dryRun = false } = options;
|
|
19
|
+
const report = { collections: {} };
|
|
20
|
+
const registry = getFieldsRegistry(payload.config);
|
|
21
|
+
const localeCodes = payload.config.localization ? payload.config.localization.localeCodes : [];
|
|
22
|
+
for (const collection of payload.config.collections) {
|
|
23
|
+
if (collections && !collections.includes(collection.slug)) continue;
|
|
24
|
+
const markers = scanEncryptedFields(collection.fields);
|
|
25
|
+
if (markers.size === 0) continue;
|
|
26
|
+
const stats = {
|
|
27
|
+
rotated: 0,
|
|
28
|
+
scanned: 0
|
|
29
|
+
};
|
|
30
|
+
report.collections[collection.slug] = stats;
|
|
31
|
+
const openValue = async ({ locale, marker, slug, value }) => {
|
|
32
|
+
const ring = await resolveKeys(marker.keys ?? registry?.encrypted?.keys, payload.config.secret);
|
|
33
|
+
const base = `${slug}.${marker.fieldName}`;
|
|
34
|
+
const candidates = marker.localized ? [...locale ? [`${base}.${locale}`] : [], ...localeCodes.map((code) => `${base}.${code}`)] : [base];
|
|
35
|
+
const openOne = (item) => {
|
|
36
|
+
if (!isSealed(item)) return item;
|
|
37
|
+
const plaintext = unseal(item, ring.dataKeys, candidates);
|
|
38
|
+
return typeof plaintext === "string" ? plaintext : JSON.stringify(plaintext);
|
|
39
|
+
};
|
|
40
|
+
return Array.isArray(value) ? value.map(openOne) : openOne(value);
|
|
41
|
+
};
|
|
42
|
+
for await (const docs of pageThrough({
|
|
43
|
+
batchSize,
|
|
44
|
+
collection: collection.slug,
|
|
45
|
+
payload
|
|
46
|
+
})) for (const doc of docs) {
|
|
47
|
+
stats.scanned += 1;
|
|
48
|
+
const patch = {};
|
|
49
|
+
const localePatches = /* @__PURE__ */ new Map();
|
|
50
|
+
for (const [path, marker] of markers) {
|
|
51
|
+
const value = getAtPath(doc, path);
|
|
52
|
+
if (value === void 0 || value === null) continue;
|
|
53
|
+
if (marker.localized && typeof value === "object" && !Array.isArray(value)) for (const locale of localeCodes) {
|
|
54
|
+
const localeValue = value[locale];
|
|
55
|
+
if (localeValue === void 0 || localeValue === null) continue;
|
|
56
|
+
const localePatch = localePatches.get(locale) ?? {};
|
|
57
|
+
setAtPath(localePatch, path, await openValue({
|
|
58
|
+
locale,
|
|
59
|
+
marker,
|
|
60
|
+
slug: collection.slug,
|
|
61
|
+
value: localeValue
|
|
62
|
+
}));
|
|
63
|
+
localePatches.set(locale, localePatch);
|
|
64
|
+
}
|
|
65
|
+
else setAtPath(patch, path, await openValue({
|
|
66
|
+
marker,
|
|
67
|
+
slug: collection.slug,
|
|
68
|
+
value
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
if (!(Object.keys(patch).length > 0 || localePatches.size > 0)) continue;
|
|
72
|
+
stats.rotated += 1;
|
|
73
|
+
if (dryRun) continue;
|
|
74
|
+
if (Object.keys(patch).length > 0) await payload.update({
|
|
75
|
+
collection: collection.slug,
|
|
76
|
+
context: { [ENCRYPTED_CONTEXT_KEY]: "decrypt" },
|
|
77
|
+
data: patch,
|
|
78
|
+
depth: 0,
|
|
79
|
+
id: doc.id,
|
|
80
|
+
overrideAccess: true
|
|
81
|
+
});
|
|
82
|
+
for (const [locale, localePatch] of localePatches) await payload.update({
|
|
83
|
+
collection: collection.slug,
|
|
84
|
+
context: { [ENCRYPTED_CONTEXT_KEY]: "decrypt" },
|
|
85
|
+
data: localePatch,
|
|
86
|
+
depth: 0,
|
|
87
|
+
id: doc.id,
|
|
88
|
+
locale,
|
|
89
|
+
overrideAccess: true
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return report;
|
|
94
|
+
};
|
|
95
|
+
//#endregion
|
|
96
|
+
export { decryptAllData };
|
|
97
|
+
|
|
98
|
+
//# sourceMappingURL=decryptAllData.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decryptAllData.js","names":[],"sources":["../../../../src/fields/encrypted/utils/decryptAllData.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { getFieldsRegistry } from '../../../plugin/registry'\nimport { resolveKeys } from '../crypto/keys'\nimport { isSealed, unseal } from '../crypto/wire'\nimport { scanEncryptedFields } from '../scan'\nimport { ENCRYPTED_CONTEXT_KEY, type EncryptedFieldMarker } from '../types'\nimport { getAtPath, pageThrough, setAtPath } from './pageThrough'\nimport type { RotateOptions, RotateReport } from './rotateEncryptedFields'\n\ninterface OpenValueArgs {\n\tlocale?: string\n\tmarker: EncryptedFieldMarker\n\tslug: string\n\tvalue: unknown\n}\n\n/**\n * Removal path: unseals every sealed value and stores it as plaintext, with\n * hooks in 'decrypt' mode so nothing re-seals. Non-string plaintexts are\n * stored JSON-stringified (the backing column is text); swapping the field\n * back to its native type afterwards is a schema migration the consumer owns\n * (documented in the removal guide). The decrypt-mode beforeChange hook nulls\n * each blind-index sibling at its own container scope, so this never writes the\n * index by hand (a flat write would mis-scope a nested field's index).\n */\nexport const decryptAllData = async (\n\tpayload: Payload,\n\toptions: RotateOptions = {}\n): Promise<RotateReport> => {\n\tconst { batchSize = 100, collections, dryRun = false } = options\n\tconst report: RotateReport = { collections: {} }\n\tconst registry = getFieldsRegistry(payload.config)\n\tconst localeCodes = payload.config.localization ? payload.config.localization.localeCodes : []\n\n\tfor (const collection of payload.config.collections) {\n\t\tif (collections && !collections.includes(collection.slug)) {\n\t\t\tcontinue\n\t\t}\n\t\tconst markers = scanEncryptedFields(collection.fields)\n\t\tif (markers.size === 0) {\n\t\t\tcontinue\n\t\t}\n\t\tconst stats = { rotated: 0, scanned: 0 }\n\t\treport.collections[collection.slug] = stats\n\n\t\tconst openValue = async ({ locale, marker, slug, value }: OpenValueArgs): Promise<unknown> => {\n\t\t\tconst ring = await resolveKeys(\n\t\t\t\tmarker.keys ?? registry?.encrypted?.keys,\n\t\t\t\tpayload.config.secret\n\t\t\t)\n\t\t\tconst base = `${slug}.${marker.fieldName}`\n\t\t\tconst candidates = marker.localized\n\t\t\t\t? [\n\t\t\t\t\t\t...(locale ? [`${base}.${locale}`] : []),\n\t\t\t\t\t\t...localeCodes.map((code) => `${base}.${code}`),\n\t\t\t\t\t]\n\t\t\t\t: [base]\n\t\t\tconst openOne = (item: unknown): unknown => {\n\t\t\t\tif (!isSealed(item)) {\n\t\t\t\t\treturn item\n\t\t\t\t}\n\t\t\t\tconst plaintext = unseal(item, ring.dataKeys, candidates)\n\t\t\t\treturn typeof plaintext === 'string' ? plaintext : JSON.stringify(plaintext)\n\t\t\t}\n\t\t\treturn Array.isArray(value) ? value.map(openOne) : openOne(value)\n\t\t}\n\n\t\tfor await (const docs of pageThrough({ batchSize, collection: collection.slug, payload })) {\n\t\t\tfor (const doc of docs) {\n\t\t\t\tstats.scanned += 1\n\t\t\t\tconst patch: Record<string, unknown> = {}\n\t\t\t\tconst localePatches = new Map<string, Record<string, unknown>>()\n\t\t\t\tfor (const [path, marker] of markers) {\n\t\t\t\t\tconst value = getAtPath(doc, path)\n\t\t\t\t\tif (value === undefined || value === null) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tif (marker.localized && typeof value === 'object' && !Array.isArray(value)) {\n\t\t\t\t\t\tfor (const locale of localeCodes) {\n\t\t\t\t\t\t\tconst localeValue = (value as Record<string, unknown>)[locale]\n\t\t\t\t\t\t\tif (localeValue === undefined || localeValue === null) {\n\t\t\t\t\t\t\t\tcontinue\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tconst localePatch = localePatches.get(locale) ?? {}\n\t\t\t\t\t\t\tsetAtPath(\n\t\t\t\t\t\t\t\tlocalePatch,\n\t\t\t\t\t\t\t\tpath,\n\t\t\t\t\t\t\t\tawait openValue({ locale, marker, slug: collection.slug, value: localeValue })\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tlocalePatches.set(locale, localePatch)\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tsetAtPath(patch, path, await openValue({ marker, slug: collection.slug, value }))\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst hasWork = Object.keys(patch).length > 0 || localePatches.size > 0\n\t\t\t\tif (!hasWork) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tstats.rotated += 1\n\t\t\t\tif (dryRun) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tif (Object.keys(patch).length > 0) {\n\t\t\t\t\tawait payload.update({\n\t\t\t\t\t\tcollection: collection.slug,\n\t\t\t\t\t\tcontext: { [ENCRYPTED_CONTEXT_KEY]: 'decrypt' },\n\t\t\t\t\t\tdata: patch,\n\t\t\t\t\t\tdepth: 0,\n\t\t\t\t\t\tid: doc.id as string | number,\n\t\t\t\t\t\toverrideAccess: true,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tfor (const [locale, localePatch] of localePatches) {\n\t\t\t\t\tawait payload.update({\n\t\t\t\t\t\tcollection: collection.slug,\n\t\t\t\t\t\tcontext: { [ENCRYPTED_CONTEXT_KEY]: 'decrypt' },\n\t\t\t\t\t\tdata: localePatch,\n\t\t\t\t\t\tdepth: 0,\n\t\t\t\t\t\tid: doc.id as string | number,\n\t\t\t\t\t\tlocale: locale as never,\n\t\t\t\t\t\toverrideAccess: true,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn report\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAyBA,MAAa,iBAAiB,OAC7B,SACA,UAAyB,CAAC,MACC;CAC3B,MAAM,EAAE,YAAY,KAAK,aAAa,SAAS,UAAU;CACzD,MAAM,SAAuB,EAAE,aAAa,CAAC,EAAE;CAC/C,MAAM,WAAW,kBAAkB,QAAQ,MAAM;CACjD,MAAM,cAAc,QAAQ,OAAO,eAAe,QAAQ,OAAO,aAAa,cAAc,CAAC;CAE7F,KAAK,MAAM,cAAc,QAAQ,OAAO,aAAa;EACpD,IAAI,eAAe,CAAC,YAAY,SAAS,WAAW,IAAI,GACvD;EAED,MAAM,UAAU,oBAAoB,WAAW,MAAM;EACrD,IAAI,QAAQ,SAAS,GACpB;EAED,MAAM,QAAQ;GAAE,SAAS;GAAG,SAAS;EAAE;EACvC,OAAO,YAAY,WAAW,QAAQ;EAEtC,MAAM,YAAY,OAAO,EAAE,QAAQ,QAAQ,MAAM,YAA6C;GAC7F,MAAM,OAAO,MAAM,YAClB,OAAO,QAAQ,UAAU,WAAW,MACpC,QAAQ,OAAO,MAChB;GACA,MAAM,OAAO,GAAG,KAAK,GAAG,OAAO;GAC/B,MAAM,aAAa,OAAO,YACvB,CACA,GAAI,SAAS,CAAC,GAAG,KAAK,GAAG,QAAQ,IAAI,CAAC,GACtC,GAAG,YAAY,KAAK,SAAS,GAAG,KAAK,GAAG,MAAM,CAC/C,IACC,CAAC,IAAI;GACR,MAAM,WAAW,SAA2B;IAC3C,IAAI,CAAC,SAAS,IAAI,GACjB,OAAO;IAER,MAAM,YAAY,OAAO,MAAM,KAAK,UAAU,UAAU;IACxD,OAAO,OAAO,cAAc,WAAW,YAAY,KAAK,UAAU,SAAS;GAC5E;GACA,OAAO,MAAM,QAAQ,KAAK,IAAI,MAAM,IAAI,OAAO,IAAI,QAAQ,KAAK;EACjE;EAEA,WAAW,MAAM,QAAQ,YAAY;GAAE;GAAW,YAAY,WAAW;GAAM;EAAQ,CAAC,GACvF,KAAK,MAAM,OAAO,MAAM;GACvB,MAAM,WAAW;GACjB,MAAM,QAAiC,CAAC;GACxC,MAAM,gCAAgB,IAAI,IAAqC;GAC/D,KAAK,MAAM,CAAC,MAAM,WAAW,SAAS;IACrC,MAAM,QAAQ,UAAU,KAAK,IAAI;IACjC,IAAI,UAAU,KAAA,KAAa,UAAU,MACpC;IAED,IAAI,OAAO,aAAa,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,GACxE,KAAK,MAAM,UAAU,aAAa;KACjC,MAAM,cAAe,MAAkC;KACvD,IAAI,gBAAgB,KAAA,KAAa,gBAAgB,MAChD;KAED,MAAM,cAAc,cAAc,IAAI,MAAM,KAAK,CAAC;KAClD,UACC,aACA,MACA,MAAM,UAAU;MAAE;MAAQ;MAAQ,MAAM,WAAW;MAAM,OAAO;KAAY,CAAC,CAC9E;KACA,cAAc,IAAI,QAAQ,WAAW;IACtC;SAEA,UAAU,OAAO,MAAM,MAAM,UAAU;KAAE;KAAQ,MAAM,WAAW;KAAM;IAAM,CAAC,CAAC;GAElF;GAEA,IAAI,EADY,OAAO,KAAK,KAAK,EAAE,SAAS,KAAK,cAAc,OAAO,IAErE;GAED,MAAM,WAAW;GACjB,IAAI,QACH;GAED,IAAI,OAAO,KAAK,KAAK,EAAE,SAAS,GAC/B,MAAM,QAAQ,OAAO;IACpB,YAAY,WAAW;IACvB,SAAS,GAAG,wBAAwB,UAAU;IAC9C,MAAM;IACN,OAAO;IACP,IAAI,IAAI;IACR,gBAAgB;GACjB,CAAC;GAEF,KAAK,MAAM,CAAC,QAAQ,gBAAgB,eACnC,MAAM,QAAQ,OAAO;IACpB,YAAY,WAAW;IACvB,SAAS,GAAG,wBAAwB,UAAU;IAC9C,MAAM;IACN,OAAO;IACP,IAAI,IAAI;IACA;IACR,gBAAgB;GACjB,CAAC;EAEH;CAEF;CACA,OAAO;AACR"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { RotateOptions, RotateReport } from "./rotateEncryptedFields.js";
|
|
2
|
+
import { Payload } from "payload";
|
|
3
|
+
|
|
4
|
+
//#region src/fields/encrypted/utils/encryptExistingData.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Adoption path: seals rows that still hold plaintext (pre-plugin data).
|
|
7
|
+
* Reads run raw; writes run WITHOUT a mode flag so the normal seal hook
|
|
8
|
+
* validates the plaintext and computes the blind index. Legacy rows that
|
|
9
|
+
* fail the field's validation surface as errors naming the document.
|
|
10
|
+
*/
|
|
11
|
+
declare const encryptExistingData: (payload: Payload, options?: RotateOptions) => Promise<RotateReport>;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { encryptExistingData };
|
|
14
|
+
//# sourceMappingURL=encryptExistingData.d.ts.map
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { scanEncryptedFields } from "../scan.js";
|
|
2
|
+
import { isSealed } from "../crypto/wire.js";
|
|
3
|
+
import { getAtPath, pageThrough, setAtPath } from "./pageThrough.js";
|
|
4
|
+
//#region src/fields/encrypted/utils/encryptExistingData.ts
|
|
5
|
+
/** A value still holds plaintext when it (or any hasMany entry) is not sealed. */
|
|
6
|
+
const needsSeal = (item) => item !== null && item !== void 0 && !isSealed(item) && (!Array.isArray(item) || item.some((entry) => !isSealed(entry)));
|
|
7
|
+
/**
|
|
8
|
+
* Adoption path: seals rows that still hold plaintext (pre-plugin data).
|
|
9
|
+
* Reads run raw; writes run WITHOUT a mode flag so the normal seal hook
|
|
10
|
+
* validates the plaintext and computes the blind index. Legacy rows that
|
|
11
|
+
* fail the field's validation surface as errors naming the document.
|
|
12
|
+
*/
|
|
13
|
+
const encryptExistingData = async (payload, options = {}) => {
|
|
14
|
+
const { batchSize = 100, collections, dryRun = false } = options;
|
|
15
|
+
const report = { collections: {} };
|
|
16
|
+
const localeCodes = payload.config.localization ? payload.config.localization.localeCodes : [];
|
|
17
|
+
for (const collection of payload.config.collections) {
|
|
18
|
+
if (collections && !collections.includes(collection.slug)) continue;
|
|
19
|
+
const markers = scanEncryptedFields(collection.fields);
|
|
20
|
+
if (markers.size === 0) continue;
|
|
21
|
+
const stats = {
|
|
22
|
+
rotated: 0,
|
|
23
|
+
scanned: 0
|
|
24
|
+
};
|
|
25
|
+
report.collections[collection.slug] = stats;
|
|
26
|
+
for await (const docs of pageThrough({
|
|
27
|
+
batchSize,
|
|
28
|
+
collection: collection.slug,
|
|
29
|
+
payload
|
|
30
|
+
})) for (const doc of docs) {
|
|
31
|
+
stats.scanned += 1;
|
|
32
|
+
const patch = {};
|
|
33
|
+
const localePatches = /* @__PURE__ */ new Map();
|
|
34
|
+
for (const [path, marker] of markers) {
|
|
35
|
+
const value = getAtPath(doc, path);
|
|
36
|
+
if (value === void 0 || value === null) continue;
|
|
37
|
+
if (marker.localized && typeof value === "object" && !Array.isArray(value)) for (const locale of localeCodes) {
|
|
38
|
+
const localeValue = value[locale];
|
|
39
|
+
if (needsSeal(localeValue)) {
|
|
40
|
+
const localePatch = localePatches.get(locale) ?? {};
|
|
41
|
+
setAtPath(localePatch, path, localeValue);
|
|
42
|
+
localePatches.set(locale, localePatch);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
else if (needsSeal(value)) setAtPath(patch, path, value);
|
|
46
|
+
}
|
|
47
|
+
if (!(Object.keys(patch).length > 0 || localePatches.size > 0)) continue;
|
|
48
|
+
stats.rotated += 1;
|
|
49
|
+
if (dryRun) continue;
|
|
50
|
+
if (Object.keys(patch).length > 0) await payload.update({
|
|
51
|
+
collection: collection.slug,
|
|
52
|
+
data: patch,
|
|
53
|
+
depth: 0,
|
|
54
|
+
id: doc.id,
|
|
55
|
+
overrideAccess: true
|
|
56
|
+
});
|
|
57
|
+
for (const [locale, localePatch] of localePatches) await payload.update({
|
|
58
|
+
collection: collection.slug,
|
|
59
|
+
data: localePatch,
|
|
60
|
+
depth: 0,
|
|
61
|
+
id: doc.id,
|
|
62
|
+
locale,
|
|
63
|
+
overrideAccess: true
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return report;
|
|
68
|
+
};
|
|
69
|
+
//#endregion
|
|
70
|
+
export { encryptExistingData };
|
|
71
|
+
|
|
72
|
+
//# sourceMappingURL=encryptExistingData.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encryptExistingData.js","names":[],"sources":["../../../../src/fields/encrypted/utils/encryptExistingData.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { isSealed } from '../crypto/wire'\nimport { scanEncryptedFields } from '../scan'\nimport { getAtPath, pageThrough, setAtPath } from './pageThrough'\nimport type { RotateOptions, RotateReport } from './rotateEncryptedFields'\n\n/** A value still holds plaintext when it (or any hasMany entry) is not sealed. */\nconst needsSeal = (item: unknown): boolean =>\n\titem !== null &&\n\titem !== undefined &&\n\t!isSealed(item) &&\n\t(!Array.isArray(item) || item.some((entry) => !isSealed(entry)))\n\n/**\n * Adoption path: seals rows that still hold plaintext (pre-plugin data).\n * Reads run raw; writes run WITHOUT a mode flag so the normal seal hook\n * validates the plaintext and computes the blind index. Legacy rows that\n * fail the field's validation surface as errors naming the document.\n */\nexport const encryptExistingData = async (\n\tpayload: Payload,\n\toptions: RotateOptions = {}\n): Promise<RotateReport> => {\n\tconst { batchSize = 100, collections, dryRun = false } = options\n\tconst report: RotateReport = { collections: {} }\n\tconst localeCodes = payload.config.localization ? payload.config.localization.localeCodes : []\n\n\tfor (const collection of payload.config.collections) {\n\t\tif (collections && !collections.includes(collection.slug)) {\n\t\t\tcontinue\n\t\t}\n\t\tconst markers = scanEncryptedFields(collection.fields)\n\t\tif (markers.size === 0) {\n\t\t\tcontinue\n\t\t}\n\t\tconst stats = { rotated: 0, scanned: 0 }\n\t\treport.collections[collection.slug] = stats\n\n\t\tfor await (const docs of pageThrough({ batchSize, collection: collection.slug, payload })) {\n\t\t\tfor (const doc of docs) {\n\t\t\t\tstats.scanned += 1\n\t\t\t\tconst patch: Record<string, unknown> = {}\n\t\t\t\tconst localePatches = new Map<string, Record<string, unknown>>()\n\t\t\t\tfor (const [path, marker] of markers) {\n\t\t\t\t\tconst value = getAtPath(doc, path)\n\t\t\t\t\tif (value === undefined || value === null) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tif (marker.localized && typeof value === 'object' && !Array.isArray(value)) {\n\t\t\t\t\t\tfor (const locale of localeCodes) {\n\t\t\t\t\t\t\tconst localeValue = (value as Record<string, unknown>)[locale]\n\t\t\t\t\t\t\tif (needsSeal(localeValue)) {\n\t\t\t\t\t\t\t\tconst localePatch = localePatches.get(locale) ?? {}\n\t\t\t\t\t\t\t\tsetAtPath(localePatch, path, localeValue)\n\t\t\t\t\t\t\t\tlocalePatches.set(locale, localePatch)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t} else if (needsSeal(value)) {\n\t\t\t\t\t\tsetAtPath(patch, path, value)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst hasWork = Object.keys(patch).length > 0 || localePatches.size > 0\n\t\t\t\tif (!hasWork) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tstats.rotated += 1\n\t\t\t\tif (dryRun) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tif (Object.keys(patch).length > 0) {\n\t\t\t\t\tawait payload.update({\n\t\t\t\t\t\tcollection: collection.slug,\n\t\t\t\t\t\tdata: patch,\n\t\t\t\t\t\tdepth: 0,\n\t\t\t\t\t\tid: doc.id as string | number,\n\t\t\t\t\t\toverrideAccess: true,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tfor (const [locale, localePatch] of localePatches) {\n\t\t\t\t\tawait payload.update({\n\t\t\t\t\t\tcollection: collection.slug,\n\t\t\t\t\t\tdata: localePatch,\n\t\t\t\t\t\tdepth: 0,\n\t\t\t\t\t\tid: doc.id as string | number,\n\t\t\t\t\t\tlocale: locale as never,\n\t\t\t\t\t\toverrideAccess: true,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn report\n}\n"],"mappings":";;;;;AAOA,MAAM,aAAa,SAClB,SAAS,QACT,SAAS,KAAA,KACT,CAAC,SAAS,IAAI,MACb,CAAC,MAAM,QAAQ,IAAI,KAAK,KAAK,MAAM,UAAU,CAAC,SAAS,KAAK,CAAC;;;;;;;AAQ/D,MAAa,sBAAsB,OAClC,SACA,UAAyB,CAAC,MACC;CAC3B,MAAM,EAAE,YAAY,KAAK,aAAa,SAAS,UAAU;CACzD,MAAM,SAAuB,EAAE,aAAa,CAAC,EAAE;CAC/C,MAAM,cAAc,QAAQ,OAAO,eAAe,QAAQ,OAAO,aAAa,cAAc,CAAC;CAE7F,KAAK,MAAM,cAAc,QAAQ,OAAO,aAAa;EACpD,IAAI,eAAe,CAAC,YAAY,SAAS,WAAW,IAAI,GACvD;EAED,MAAM,UAAU,oBAAoB,WAAW,MAAM;EACrD,IAAI,QAAQ,SAAS,GACpB;EAED,MAAM,QAAQ;GAAE,SAAS;GAAG,SAAS;EAAE;EACvC,OAAO,YAAY,WAAW,QAAQ;EAEtC,WAAW,MAAM,QAAQ,YAAY;GAAE;GAAW,YAAY,WAAW;GAAM;EAAQ,CAAC,GACvF,KAAK,MAAM,OAAO,MAAM;GACvB,MAAM,WAAW;GACjB,MAAM,QAAiC,CAAC;GACxC,MAAM,gCAAgB,IAAI,IAAqC;GAC/D,KAAK,MAAM,CAAC,MAAM,WAAW,SAAS;IACrC,MAAM,QAAQ,UAAU,KAAK,IAAI;IACjC,IAAI,UAAU,KAAA,KAAa,UAAU,MACpC;IAED,IAAI,OAAO,aAAa,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,GACxE,KAAK,MAAM,UAAU,aAAa;KACjC,MAAM,cAAe,MAAkC;KACvD,IAAI,UAAU,WAAW,GAAG;MAC3B,MAAM,cAAc,cAAc,IAAI,MAAM,KAAK,CAAC;MAClD,UAAU,aAAa,MAAM,WAAW;MACxC,cAAc,IAAI,QAAQ,WAAW;KACtC;IACD;SACM,IAAI,UAAU,KAAK,GACzB,UAAU,OAAO,MAAM,KAAK;GAE9B;GAEA,IAAI,EADY,OAAO,KAAK,KAAK,EAAE,SAAS,KAAK,cAAc,OAAO,IAErE;GAED,MAAM,WAAW;GACjB,IAAI,QACH;GAED,IAAI,OAAO,KAAK,KAAK,EAAE,SAAS,GAC/B,MAAM,QAAQ,OAAO;IACpB,YAAY,WAAW;IACvB,MAAM;IACN,OAAO;IACP,IAAI,IAAI;IACR,gBAAgB;GACjB,CAAC;GAEF,KAAK,MAAM,CAAC,QAAQ,gBAAgB,eACnC,MAAM,QAAQ,OAAO;IACpB,YAAY,WAAW;IACvB,MAAM;IACN,OAAO;IACP,IAAI,IAAI;IACA;IACR,gBAAgB;GACjB,CAAC;EAEH;CAEF;CACA,OAAO;AACR"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { ENCRYPTED_CONTEXT_KEY } from "../types.js";
|
|
2
|
+
//#region src/fields/encrypted/utils/pageThrough.ts
|
|
3
|
+
/**
|
|
4
|
+
* Iterates a collection in id-sorted pages with hooks in 'raw' mode, so
|
|
5
|
+
* encrypted values arrive as stored ciphertext. Updates during iteration do
|
|
6
|
+
* not change membership (no filter), so page order stays stable.
|
|
7
|
+
*/
|
|
8
|
+
async function* pageThrough({ batchSize, collection, payload }) {
|
|
9
|
+
let page = 1;
|
|
10
|
+
for (;;) {
|
|
11
|
+
const result = await payload.find({
|
|
12
|
+
collection,
|
|
13
|
+
context: { [ENCRYPTED_CONTEXT_KEY]: "raw" },
|
|
14
|
+
depth: 0,
|
|
15
|
+
limit: batchSize,
|
|
16
|
+
locale: "all",
|
|
17
|
+
overrideAccess: true,
|
|
18
|
+
page,
|
|
19
|
+
sort: "id"
|
|
20
|
+
});
|
|
21
|
+
if (result.docs.length > 0) yield result.docs;
|
|
22
|
+
if (!result.hasNextPage) return;
|
|
23
|
+
page += 1;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const getAtPath = (doc, path) => path.split(".").reduce((node, segment) => {
|
|
27
|
+
if (node && typeof node === "object") return node[segment];
|
|
28
|
+
}, doc);
|
|
29
|
+
const setAtPath = (target, path, value) => {
|
|
30
|
+
const segments = path.split(".");
|
|
31
|
+
let node = target;
|
|
32
|
+
for (const segment of segments.slice(0, -1)) {
|
|
33
|
+
const next = node[segment];
|
|
34
|
+
if (!next || typeof next !== "object") node[segment] = {};
|
|
35
|
+
node = node[segment];
|
|
36
|
+
}
|
|
37
|
+
node[segments[segments.length - 1]] = value;
|
|
38
|
+
};
|
|
39
|
+
//#endregion
|
|
40
|
+
export { getAtPath, pageThrough, setAtPath };
|
|
41
|
+
|
|
42
|
+
//# sourceMappingURL=pageThrough.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pageThrough.js","names":[],"sources":["../../../../src/fields/encrypted/utils/pageThrough.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { ENCRYPTED_CONTEXT_KEY } from '../types'\n\nexport interface PageThroughArgs {\n\tbatchSize: number\n\tcollection: string\n\tpayload: Payload\n}\n\n/**\n * Iterates a collection in id-sorted pages with hooks in 'raw' mode, so\n * encrypted values arrive as stored ciphertext. Updates during iteration do\n * not change membership (no filter), so page order stays stable.\n */\nexport async function* pageThrough({\n\tbatchSize,\n\tcollection,\n\tpayload,\n}: PageThroughArgs): AsyncGenerator<Record<string, unknown>[]> {\n\tlet page = 1\n\tfor (;;) {\n\t\tconst result = await payload.find({\n\t\t\tcollection: collection as never,\n\t\t\tcontext: { [ENCRYPTED_CONTEXT_KEY]: 'raw' },\n\t\t\tdepth: 0,\n\t\t\tlimit: batchSize,\n\t\t\tlocale: 'all',\n\t\t\toverrideAccess: true,\n\t\t\tpage,\n\t\t\tsort: 'id',\n\t\t})\n\t\tif (result.docs.length > 0) {\n\t\t\tyield result.docs as unknown as Record<string, unknown>[]\n\t\t}\n\t\tif (!result.hasNextPage) {\n\t\t\treturn\n\t\t}\n\t\tpage += 1\n\t}\n}\n\nexport const getAtPath = (doc: Record<string, unknown>, path: string): unknown =>\n\tpath.split('.').reduce<unknown>((node, segment) => {\n\t\tif (node && typeof node === 'object') {\n\t\t\treturn (node as Record<string, unknown>)[segment]\n\t\t}\n\t\treturn undefined\n\t}, doc)\n\nexport const setAtPath = (target: Record<string, unknown>, path: string, value: unknown): void => {\n\tconst segments = path.split('.')\n\tlet node = target\n\tfor (const segment of segments.slice(0, -1)) {\n\t\tconst next = node[segment]\n\t\tif (!next || typeof next !== 'object') {\n\t\t\tnode[segment] = {}\n\t\t}\n\t\tnode = node[segment] as Record<string, unknown>\n\t}\n\tnode[segments[segments.length - 1] as string] = value\n}\n"],"mappings":";;;;;;;AAcA,gBAAuB,YAAY,EAClC,WACA,YACA,WAC8D;CAC9D,IAAI,OAAO;CACX,SAAS;EACR,MAAM,SAAS,MAAM,QAAQ,KAAK;GACrB;GACZ,SAAS,GAAG,wBAAwB,MAAM;GAC1C,OAAO;GACP,OAAO;GACP,QAAQ;GACR,gBAAgB;GAChB;GACA,MAAM;EACP,CAAC;EACD,IAAI,OAAO,KAAK,SAAS,GACxB,MAAM,OAAO;EAEd,IAAI,CAAC,OAAO,aACX;EAED,QAAQ;CACT;AACD;AAEA,MAAa,aAAa,KAA8B,SACvD,KAAK,MAAM,GAAG,EAAE,QAAiB,MAAM,YAAY;CAClD,IAAI,QAAQ,OAAO,SAAS,UAC3B,OAAQ,KAAiC;AAG3C,GAAG,GAAG;AAEP,MAAa,aAAa,QAAiC,MAAc,UAAyB;CACjG,MAAM,WAAW,KAAK,MAAM,GAAG;CAC/B,IAAI,OAAO;CACX,KAAK,MAAM,WAAW,SAAS,MAAM,GAAG,EAAE,GAAG;EAC5C,MAAM,OAAO,KAAK;EAClB,IAAI,CAAC,QAAQ,OAAO,SAAS,UAC5B,KAAK,WAAW,CAAC;EAElB,OAAO,KAAK;CACb;CACA,KAAK,SAAS,SAAS,SAAS,MAAgB;AACjD"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Payload } from "payload";
|
|
2
|
+
|
|
3
|
+
//#region src/fields/encrypted/utils/rotateEncryptedFields.d.ts
|
|
4
|
+
interface RotateOptions {
|
|
5
|
+
batchSize?: number;
|
|
6
|
+
collections?: string[];
|
|
7
|
+
dryRun?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface RotateReport {
|
|
10
|
+
collections: Record<string, {
|
|
11
|
+
rotated: number;
|
|
12
|
+
scanned: number;
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Re-seals every row whose embedded keyId differs from the active key, in
|
|
17
|
+
* paginated batches through the local API. Reads run in 'raw' mode (hooks
|
|
18
|
+
* pass ciphertext through); writes run in 'rotate' mode (the beforeChange
|
|
19
|
+
* hook unseals with the old key, re-seals with the active key, and refreshes
|
|
20
|
+
* the blind index). Localized fields update once per locale. Writes go
|
|
21
|
+
* through payload.update, so versioned collections record a new version per
|
|
22
|
+
* rotated document.
|
|
23
|
+
*/
|
|
24
|
+
declare const rotateEncryptedFields: (payload: Payload, options?: RotateOptions) => Promise<RotateReport>;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { RotateOptions, RotateReport, rotateEncryptedFields };
|
|
27
|
+
//# sourceMappingURL=rotateEncryptedFields.d.ts.map
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { resolveKeys } from "../crypto/keys.js";
|
|
2
|
+
import { ENCRYPTED_CONTEXT_KEY } from "../types.js";
|
|
3
|
+
import { scanEncryptedFields } from "../scan.js";
|
|
4
|
+
import { getFieldsRegistry } from "../../../plugin/registry.js";
|
|
5
|
+
import { isSealed, parseWire } from "../crypto/wire.js";
|
|
6
|
+
import { getAtPath, pageThrough, setAtPath } from "./pageThrough.js";
|
|
7
|
+
//#region src/fields/encrypted/utils/rotateEncryptedFields.ts
|
|
8
|
+
/**
|
|
9
|
+
* True when any sealed value under `value` carries a keyId other than the
|
|
10
|
+
* active one. Recurses into arrays (hasMany) and into locale maps, which is the
|
|
11
|
+
* `{ [locale]: value }` shape a locale 'all' read hands back.
|
|
12
|
+
*/
|
|
13
|
+
const staleIn = (value, activeId) => {
|
|
14
|
+
if (Array.isArray(value)) return value.some((item) => staleIn(item, activeId));
|
|
15
|
+
if (isSealed(value)) return parseWire(value).keyId !== activeId;
|
|
16
|
+
if (value && typeof value === "object") return Object.values(value).some((item) => staleIn(item, activeId));
|
|
17
|
+
return false;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Re-seals every row whose embedded keyId differs from the active key, in
|
|
21
|
+
* paginated batches through the local API. Reads run in 'raw' mode (hooks
|
|
22
|
+
* pass ciphertext through); writes run in 'rotate' mode (the beforeChange
|
|
23
|
+
* hook unseals with the old key, re-seals with the active key, and refreshes
|
|
24
|
+
* the blind index). Localized fields update once per locale. Writes go
|
|
25
|
+
* through payload.update, so versioned collections record a new version per
|
|
26
|
+
* rotated document.
|
|
27
|
+
*/
|
|
28
|
+
const rotateEncryptedFields = async (payload, options = {}) => {
|
|
29
|
+
const { batchSize = 100, collections, dryRun = false } = options;
|
|
30
|
+
const report = { collections: {} };
|
|
31
|
+
const registry = getFieldsRegistry(payload.config);
|
|
32
|
+
const localeCodes = payload.config.localization ? payload.config.localization.localeCodes : [];
|
|
33
|
+
for (const collection of payload.config.collections) {
|
|
34
|
+
if (collections && !collections.includes(collection.slug)) continue;
|
|
35
|
+
const markers = scanEncryptedFields(collection.fields);
|
|
36
|
+
if (markers.size === 0) continue;
|
|
37
|
+
const stats = {
|
|
38
|
+
rotated: 0,
|
|
39
|
+
scanned: 0
|
|
40
|
+
};
|
|
41
|
+
report.collections[collection.slug] = stats;
|
|
42
|
+
const ringFor = (marker) => resolveKeys(marker.keys ?? registry?.encrypted?.keys, payload.config.secret);
|
|
43
|
+
for await (const docs of pageThrough({
|
|
44
|
+
batchSize,
|
|
45
|
+
collection: collection.slug,
|
|
46
|
+
payload
|
|
47
|
+
})) for (const doc of docs) {
|
|
48
|
+
stats.scanned += 1;
|
|
49
|
+
const patch = {};
|
|
50
|
+
const localePatches = /* @__PURE__ */ new Map();
|
|
51
|
+
for (const [path, marker] of markers) {
|
|
52
|
+
const ring = await ringFor(marker);
|
|
53
|
+
const value = getAtPath(doc, path);
|
|
54
|
+
if (value === void 0 || !staleIn(value, ring.activeId)) continue;
|
|
55
|
+
if (marker.localized && value && typeof value === "object" && !Array.isArray(value)) for (const locale of localeCodes) {
|
|
56
|
+
const localeValue = value[locale];
|
|
57
|
+
if (localeValue !== void 0 && staleIn(localeValue, ring.activeId)) {
|
|
58
|
+
const localePatch = localePatches.get(locale) ?? {};
|
|
59
|
+
setAtPath(localePatch, path, localeValue);
|
|
60
|
+
localePatches.set(locale, localePatch);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else setAtPath(patch, path, value);
|
|
64
|
+
}
|
|
65
|
+
if (!(Object.keys(patch).length > 0 || localePatches.size > 0)) continue;
|
|
66
|
+
stats.rotated += 1;
|
|
67
|
+
if (dryRun) continue;
|
|
68
|
+
if (Object.keys(patch).length > 0) await payload.update({
|
|
69
|
+
collection: collection.slug,
|
|
70
|
+
context: { [ENCRYPTED_CONTEXT_KEY]: "rotate" },
|
|
71
|
+
data: patch,
|
|
72
|
+
depth: 0,
|
|
73
|
+
id: doc.id,
|
|
74
|
+
overrideAccess: true
|
|
75
|
+
});
|
|
76
|
+
for (const [locale, localePatch] of localePatches) await payload.update({
|
|
77
|
+
collection: collection.slug,
|
|
78
|
+
context: { [ENCRYPTED_CONTEXT_KEY]: "rotate" },
|
|
79
|
+
data: localePatch,
|
|
80
|
+
depth: 0,
|
|
81
|
+
id: doc.id,
|
|
82
|
+
locale,
|
|
83
|
+
overrideAccess: true
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return report;
|
|
88
|
+
};
|
|
89
|
+
//#endregion
|
|
90
|
+
export { rotateEncryptedFields };
|
|
91
|
+
|
|
92
|
+
//# sourceMappingURL=rotateEncryptedFields.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rotateEncryptedFields.js","names":[],"sources":["../../../../src/fields/encrypted/utils/rotateEncryptedFields.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport { getFieldsRegistry } from '../../../plugin/registry'\nimport { resolveKeys } from '../crypto/keys'\nimport { isSealed, parseWire } from '../crypto/wire'\nimport { scanEncryptedFields } from '../scan'\nimport { ENCRYPTED_CONTEXT_KEY, type EncryptedFieldMarker } from '../types'\nimport { getAtPath, pageThrough, setAtPath } from './pageThrough'\n\nexport interface RotateOptions {\n\tbatchSize?: number\n\tcollections?: string[]\n\tdryRun?: boolean\n}\n\nexport interface RotateReport {\n\tcollections: Record<string, { rotated: number; scanned: number }>\n}\n\n/**\n * True when any sealed value under `value` carries a keyId other than the\n * active one. Recurses into arrays (hasMany) and into locale maps, which is the\n * `{ [locale]: value }` shape a locale 'all' read hands back.\n */\nexport const staleIn = (value: unknown, activeId: string): boolean => {\n\tif (Array.isArray(value)) {\n\t\treturn value.some((item) => staleIn(item, activeId))\n\t}\n\tif (isSealed(value)) {\n\t\treturn parseWire(value).keyId !== activeId\n\t}\n\tif (value && typeof value === 'object') {\n\t\treturn Object.values(value).some((item) => staleIn(item, activeId))\n\t}\n\treturn false\n}\n\n/**\n * Re-seals every row whose embedded keyId differs from the active key, in\n * paginated batches through the local API. Reads run in 'raw' mode (hooks\n * pass ciphertext through); writes run in 'rotate' mode (the beforeChange\n * hook unseals with the old key, re-seals with the active key, and refreshes\n * the blind index). Localized fields update once per locale. Writes go\n * through payload.update, so versioned collections record a new version per\n * rotated document.\n */\nexport const rotateEncryptedFields = async (\n\tpayload: Payload,\n\toptions: RotateOptions = {}\n): Promise<RotateReport> => {\n\tconst { batchSize = 100, collections, dryRun = false } = options\n\tconst report: RotateReport = { collections: {} }\n\tconst registry = getFieldsRegistry(payload.config)\n\tconst localeCodes = payload.config.localization ? payload.config.localization.localeCodes : []\n\n\tfor (const collection of payload.config.collections) {\n\t\tif (collections && !collections.includes(collection.slug)) {\n\t\t\tcontinue\n\t\t}\n\t\tconst markers = scanEncryptedFields(collection.fields)\n\t\tif (markers.size === 0) {\n\t\t\tcontinue\n\t\t}\n\t\tconst stats = { rotated: 0, scanned: 0 }\n\t\treport.collections[collection.slug] = stats\n\n\t\tconst ringFor = (marker: EncryptedFieldMarker) =>\n\t\t\tresolveKeys(marker.keys ?? registry?.encrypted?.keys, payload.config.secret)\n\n\t\tfor await (const docs of pageThrough({ batchSize, collection: collection.slug, payload })) {\n\t\t\tfor (const doc of docs) {\n\t\t\t\tstats.scanned += 1\n\t\t\t\tconst patch: Record<string, unknown> = {}\n\t\t\t\tconst localePatches = new Map<string, Record<string, unknown>>()\n\t\t\t\tfor (const [path, marker] of markers) {\n\t\t\t\t\tconst ring = await ringFor(marker)\n\t\t\t\t\tconst value = getAtPath(doc, path)\n\t\t\t\t\tif (value === undefined || !staleIn(value, ring.activeId)) {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\t\t\t\t\tif (marker.localized && value && typeof value === 'object' && !Array.isArray(value)) {\n\t\t\t\t\t\tfor (const locale of localeCodes) {\n\t\t\t\t\t\t\tconst localeValue = (value as Record<string, unknown>)[locale]\n\t\t\t\t\t\t\tif (localeValue !== undefined && staleIn(localeValue, ring.activeId)) {\n\t\t\t\t\t\t\t\tconst localePatch = localePatches.get(locale) ?? {}\n\t\t\t\t\t\t\t\tsetAtPath(localePatch, path, localeValue)\n\t\t\t\t\t\t\t\tlocalePatches.set(locale, localePatch)\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t} else {\n\t\t\t\t\t\tsetAtPath(patch, path, value)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tconst hasWork = Object.keys(patch).length > 0 || localePatches.size > 0\n\t\t\t\tif (!hasWork) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tstats.rotated += 1\n\t\t\t\tif (dryRun) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tif (Object.keys(patch).length > 0) {\n\t\t\t\t\tawait payload.update({\n\t\t\t\t\t\tcollection: collection.slug,\n\t\t\t\t\t\tcontext: { [ENCRYPTED_CONTEXT_KEY]: 'rotate' },\n\t\t\t\t\t\tdata: patch,\n\t\t\t\t\t\tdepth: 0,\n\t\t\t\t\t\tid: doc.id as string | number,\n\t\t\t\t\t\toverrideAccess: true,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tfor (const [locale, localePatch] of localePatches) {\n\t\t\t\t\tawait payload.update({\n\t\t\t\t\t\tcollection: collection.slug,\n\t\t\t\t\t\tcontext: { [ENCRYPTED_CONTEXT_KEY]: 'rotate' },\n\t\t\t\t\t\tdata: localePatch,\n\t\t\t\t\t\tdepth: 0,\n\t\t\t\t\t\tid: doc.id as string | number,\n\t\t\t\t\t\tlocale: locale as never,\n\t\t\t\t\t\toverrideAccess: true,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn report\n}\n"],"mappings":";;;;;;;;;;;;AAuBA,MAAa,WAAW,OAAgB,aAA8B;CACrE,IAAI,MAAM,QAAQ,KAAK,GACtB,OAAO,MAAM,MAAM,SAAS,QAAQ,MAAM,QAAQ,CAAC;CAEpD,IAAI,SAAS,KAAK,GACjB,OAAO,UAAU,KAAK,EAAE,UAAU;CAEnC,IAAI,SAAS,OAAO,UAAU,UAC7B,OAAO,OAAO,OAAO,KAAK,EAAE,MAAM,SAAS,QAAQ,MAAM,QAAQ,CAAC;CAEnE,OAAO;AACR;;;;;;;;;;AAWA,MAAa,wBAAwB,OACpC,SACA,UAAyB,CAAC,MACC;CAC3B,MAAM,EAAE,YAAY,KAAK,aAAa,SAAS,UAAU;CACzD,MAAM,SAAuB,EAAE,aAAa,CAAC,EAAE;CAC/C,MAAM,WAAW,kBAAkB,QAAQ,MAAM;CACjD,MAAM,cAAc,QAAQ,OAAO,eAAe,QAAQ,OAAO,aAAa,cAAc,CAAC;CAE7F,KAAK,MAAM,cAAc,QAAQ,OAAO,aAAa;EACpD,IAAI,eAAe,CAAC,YAAY,SAAS,WAAW,IAAI,GACvD;EAED,MAAM,UAAU,oBAAoB,WAAW,MAAM;EACrD,IAAI,QAAQ,SAAS,GACpB;EAED,MAAM,QAAQ;GAAE,SAAS;GAAG,SAAS;EAAE;EACvC,OAAO,YAAY,WAAW,QAAQ;EAEtC,MAAM,WAAW,WAChB,YAAY,OAAO,QAAQ,UAAU,WAAW,MAAM,QAAQ,OAAO,MAAM;EAE5E,WAAW,MAAM,QAAQ,YAAY;GAAE;GAAW,YAAY,WAAW;GAAM;EAAQ,CAAC,GACvF,KAAK,MAAM,OAAO,MAAM;GACvB,MAAM,WAAW;GACjB,MAAM,QAAiC,CAAC;GACxC,MAAM,gCAAgB,IAAI,IAAqC;GAC/D,KAAK,MAAM,CAAC,MAAM,WAAW,SAAS;IACrC,MAAM,OAAO,MAAM,QAAQ,MAAM;IACjC,MAAM,QAAQ,UAAU,KAAK,IAAI;IACjC,IAAI,UAAU,KAAA,KAAa,CAAC,QAAQ,OAAO,KAAK,QAAQ,GACvD;IAED,IAAI,OAAO,aAAa,SAAS,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,GACjF,KAAK,MAAM,UAAU,aAAa;KACjC,MAAM,cAAe,MAAkC;KACvD,IAAI,gBAAgB,KAAA,KAAa,QAAQ,aAAa,KAAK,QAAQ,GAAG;MACrE,MAAM,cAAc,cAAc,IAAI,MAAM,KAAK,CAAC;MAClD,UAAU,aAAa,MAAM,WAAW;MACxC,cAAc,IAAI,QAAQ,WAAW;KACtC;IACD;SAEA,UAAU,OAAO,MAAM,KAAK;GAE9B;GAEA,IAAI,EADY,OAAO,KAAK,KAAK,EAAE,SAAS,KAAK,cAAc,OAAO,IAErE;GAED,MAAM,WAAW;GACjB,IAAI,QACH;GAED,IAAI,OAAO,KAAK,KAAK,EAAE,SAAS,GAC/B,MAAM,QAAQ,OAAO;IACpB,YAAY,WAAW;IACvB,SAAS,GAAG,wBAAwB,SAAS;IAC7C,MAAM;IACN,OAAO;IACP,IAAI,IAAI;IACR,gBAAgB;GACjB,CAAC;GAEF,KAAK,MAAM,CAAC,QAAQ,gBAAgB,eACnC,MAAM,QAAQ,OAAO;IACpB,YAAY,WAAW;IACvB,SAAS,GAAG,wBAAwB,SAAS;IAC7C,MAAM;IACN,OAAO;IACP,IAAI,IAAI;IACA;IACR,gBAAgB;GACjB,CAAC;EAEH;CAEF;CACA,OAAO;AACR"}
|