@apify/input_secrets 1.2.60 → 2.0.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/field_schema_utils.d.ts +5 -0
- package/field_schema_utils.js +49 -0
- package/field_schema_utils.js.map +1 -0
- package/index.d.ts +2 -0
- package/index.js +3 -0
- package/index.js.map +1 -0
- package/{cjs/index.d.ts → input_secrets.d.ts} +6 -9
- package/input_secrets.js +158 -0
- package/input_secrets.js.map +1 -0
- package/package.json +19 -20
- package/cjs/index.cjs +0 -210
- package/cjs/index.cjs.map +0 -1
- package/esm/index.d.mts +0 -49
- package/esm/index.mjs +0 -170
- package/esm/index.mjs.map +0 -1
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* These keys are omitted from the field schema normalization process
|
|
4
|
+
* because they are not relevant for validation of values against the schema.
|
|
5
|
+
*/
|
|
6
|
+
const OMIT_KEYS = new Set([
|
|
7
|
+
'title',
|
|
8
|
+
'description',
|
|
9
|
+
'sectionCaption',
|
|
10
|
+
'sectionDescription',
|
|
11
|
+
'nullable',
|
|
12
|
+
'example',
|
|
13
|
+
'prefill',
|
|
14
|
+
'editor',
|
|
15
|
+
]);
|
|
16
|
+
/**
|
|
17
|
+
* Normalizes the field schema by removing irrelevant keys and sorting the remaining keys.
|
|
18
|
+
*/
|
|
19
|
+
function normalizeFieldSchema(value) {
|
|
20
|
+
if (Array.isArray(value)) {
|
|
21
|
+
return value.map(normalizeFieldSchema);
|
|
22
|
+
}
|
|
23
|
+
if (value && typeof value === 'object') {
|
|
24
|
+
const result = {};
|
|
25
|
+
Object.keys(value)
|
|
26
|
+
.filter((key) => !OMIT_KEYS.has(key))
|
|
27
|
+
.sort()
|
|
28
|
+
.forEach((key) => {
|
|
29
|
+
result[key] = normalizeFieldSchema(value[key]);
|
|
30
|
+
});
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Generates a stable hash for the field schema.
|
|
37
|
+
* @param fieldSchema
|
|
38
|
+
*/
|
|
39
|
+
export function getFieldSchemaHash(fieldSchema) {
|
|
40
|
+
try {
|
|
41
|
+
const stringifiedSchema = JSON.stringify(normalizeFieldSchema(fieldSchema));
|
|
42
|
+
// Create a SHA-256 hash of the stringified schema and return the first 10 characters in hex.
|
|
43
|
+
return crypto.createHash('sha256').update(stringifiedSchema).digest('hex').slice(0, 10);
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
throw new Error(`The field schema could not be stringified for hash: ${err}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=field_schema_utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"field_schema_utils.js","sourceRoot":"","sources":["../src/field_schema_utils.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,aAAa,CAAC;AAEjC;;;GAGG;AACH,MAAM,SAAS,GAAG,IAAI,GAAG,CAAC;IACtB,OAAO;IACP,aAAa;IACb,gBAAgB;IAChB,oBAAoB;IACpB,UAAU;IACV,SAAS;IACT,SAAS;IACT,QAAQ;CACX,CAAC,CAAC;AAEH;;GAEG;AACH,SAAS,oBAAoB,CAAC,KAAU;IACpC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACvB,OAAO,KAAK,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;IAC3C,CAAC;IAED,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QACrC,MAAM,MAAM,GAAwB,EAAE,CAAC;QACvC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;aACb,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;aACpC,IAAI,EAAE;aACN,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YACb,MAAM,CAAC,GAAG,CAAC,GAAG,oBAAoB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;QACnD,CAAC,CAAC,CAAC;QACP,OAAO,MAAM,CAAC;IAClB,CAAC;IAED,OAAO,KAAK,CAAC;AACjB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,WAAgC;IAC/D,IAAI,CAAC;QACD,MAAM,iBAAiB,GAAG,IAAI,CAAC,SAAS,CAAC,oBAAoB,CAAC,WAAW,CAAC,CAAC,CAAC;QAC5E,6FAA6F;QAC7F,OAAO,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IAC5F,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACX,MAAM,IAAI,KAAK,CAAC,uDAAuD,GAAG,EAAE,CAAC,CAAC;IAClF,CAAC;AACL,CAAC","sourcesContent":["import crypto from 'node:crypto';\n\n/**\n * These keys are omitted from the field schema normalization process\n * because they are not relevant for validation of values against the schema.\n */\nconst OMIT_KEYS = new Set([\n 'title',\n 'description',\n 'sectionCaption',\n 'sectionDescription',\n 'nullable',\n 'example',\n 'prefill',\n 'editor',\n]);\n\n/**\n * Normalizes the field schema by removing irrelevant keys and sorting the remaining keys.\n */\nfunction normalizeFieldSchema(value: any): any {\n if (Array.isArray(value)) {\n return value.map(normalizeFieldSchema);\n }\n\n if (value && typeof value === 'object') {\n const result: Record<string, any> = {};\n Object.keys(value)\n .filter((key) => !OMIT_KEYS.has(key))\n .sort()\n .forEach((key) => {\n result[key] = normalizeFieldSchema(value[key]);\n });\n return result;\n }\n\n return value;\n}\n\n/**\n * Generates a stable hash for the field schema.\n * @param fieldSchema\n */\nexport function getFieldSchemaHash(fieldSchema: Record<string, any>): string {\n try {\n const stringifiedSchema = JSON.stringify(normalizeFieldSchema(fieldSchema));\n // Create a SHA-256 hash of the stringified schema and return the first 10 characters in hex.\n return crypto.createHash('sha256').update(stringifiedSchema).digest('hex').slice(0, 10);\n } catch (err) {\n throw new Error(`The field schema could not be stringified for hash: ${err}`);\n }\n}\n"]}
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC","sourcesContent":["export * from './input_secrets.js';\nexport { ArgumentValidationError } from '@apify/validations';\n"]}
|
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import { KeyObject } from 'node:crypto';
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* Get keys of secret fields from input schema
|
|
5
4
|
*/
|
|
6
|
-
declare function getInputSchemaSecretFieldKeys(inputSchema: any): string[];
|
|
5
|
+
export declare function getInputSchemaSecretFieldKeys(inputSchema: any): string[];
|
|
7
6
|
/**
|
|
8
7
|
* Encrypts input secret value
|
|
9
8
|
* Depending on the type of value, it returns either a string (for strings) or an object (for objects) with the `secret` key.
|
|
10
9
|
*/
|
|
11
|
-
declare function encryptInputSecretValue<T extends string | object>({ value, publicKey, schema, }: {
|
|
10
|
+
export declare function encryptInputSecretValue<T extends string | object>({ value, publicKey, schema, }: {
|
|
12
11
|
value: T;
|
|
13
12
|
publicKey: KeyObject;
|
|
14
13
|
schema?: Record<string, any>;
|
|
@@ -19,7 +18,7 @@ declare function encryptInputSecretValue<T extends string | object>({ value, pub
|
|
|
19
18
|
* @param value - encrypted value to check
|
|
20
19
|
* @param fieldType - type of the field, can be 'string', 'object' or 'array'
|
|
21
20
|
*/
|
|
22
|
-
declare function isEncryptedValueForFieldType(value: string, fieldType: 'string' | 'object' | 'array'): boolean;
|
|
21
|
+
export declare function isEncryptedValueForFieldType(value: string, fieldType: 'string' | 'object' | 'array'): boolean;
|
|
23
22
|
/**
|
|
24
23
|
* Checks if the value is an encrypted value for a specific field schema.
|
|
25
24
|
* It validates the string value against the regular expression and checks the schema hash in
|
|
@@ -27,11 +26,11 @@ declare function isEncryptedValueForFieldType(value: string, fieldType: 'string'
|
|
|
27
26
|
* @param value - encrypted value to check
|
|
28
27
|
* @param fieldSchema - schema of the field, used to get the hash
|
|
29
28
|
*/
|
|
30
|
-
declare function isEncryptedValueForFieldSchema(value: string, fieldSchema: Record<string, any>): boolean;
|
|
29
|
+
export declare function isEncryptedValueForFieldSchema(value: string, fieldSchema: Record<string, any>): boolean;
|
|
31
30
|
/**
|
|
32
31
|
* Encrypts actor input secrets
|
|
33
32
|
*/
|
|
34
|
-
declare function encryptInputSecrets<T extends Record<string, any>>({ input, inputSchema, publicKey, }: {
|
|
33
|
+
export declare function encryptInputSecrets<T extends Record<string, any>>({ input, inputSchema, publicKey, }: {
|
|
35
34
|
input: T;
|
|
36
35
|
inputSchema: object;
|
|
37
36
|
publicKey: KeyObject;
|
|
@@ -41,9 +40,7 @@ declare function encryptInputSecrets<T extends Record<string, any>>({ input, inp
|
|
|
41
40
|
* @param input - actor input object that may contain encrypted secret values
|
|
42
41
|
* @param privateKey - private key used to decrypt the secret values
|
|
43
42
|
*/
|
|
44
|
-
declare function decryptInputSecrets<T>({ input, privateKey }: {
|
|
43
|
+
export declare function decryptInputSecrets<T>({ input, privateKey }: {
|
|
45
44
|
input: T;
|
|
46
45
|
privateKey: KeyObject;
|
|
47
46
|
}): T;
|
|
48
|
-
|
|
49
|
-
export { decryptInputSecrets, encryptInputSecretValue, encryptInputSecrets, getInputSchemaSecretFieldKeys, isEncryptedValueForFieldSchema, isEncryptedValueForFieldType };
|
package/input_secrets.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { KeyObject } from 'node:crypto';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { privateDecrypt, publicEncrypt } from '@apify/utilities';
|
|
4
|
+
import { getFieldSchemaHash } from './field_schema_utils.js';
|
|
5
|
+
import { objectSchema, parseArgument } from '@apify/validations';
|
|
6
|
+
// `z.instanceof()` rejects `KeyObject` because its constructor is not public in @types/node
|
|
7
|
+
const keyObjectSchema = z.custom((value) => value instanceof KeyObject, {
|
|
8
|
+
error: 'Invalid input: expected an instance of KeyObject',
|
|
9
|
+
});
|
|
10
|
+
const BASE64_REGEXP = /[-A-Za-z0-9+/]*={0,3}/;
|
|
11
|
+
// The encrypted value has a prefix, optional schema hash, encrypted password and encrypted value.
|
|
12
|
+
// - The prefix tells if the value is a string or a JSON object and needs to be parsed back after decryption.
|
|
13
|
+
// - The schema hash is optional and is used to verify if the schema has changed since the value was encrypted.
|
|
14
|
+
// - The encrypted password is used to decrypt the value.
|
|
15
|
+
// - The encrypted value is the actual encrypted data.
|
|
16
|
+
// used for backward compatibility with old encrypted string values
|
|
17
|
+
const ENCRYPTED_STRING_VALUE_PREFIX = 'ENCRYPTED_VALUE';
|
|
18
|
+
// we use this for all types of encrypted values (string, object, array)
|
|
19
|
+
const ENCRYPTED_JSON_VALUE_PREFIX = 'ENCRYPTED_JSON';
|
|
20
|
+
// All encrypted values must match this regular expression.
|
|
21
|
+
const ENCRYPTED_VALUE_REGEXP = new RegExp(`^(${ENCRYPTED_STRING_VALUE_PREFIX}|${ENCRYPTED_JSON_VALUE_PREFIX}):(?:(${BASE64_REGEXP.source}):)?(${BASE64_REGEXP.source}):(${BASE64_REGEXP.source})$`);
|
|
22
|
+
/**
|
|
23
|
+
* Get keys of secret fields from input schema
|
|
24
|
+
*/
|
|
25
|
+
export function getInputSchemaSecretFieldKeys(inputSchema) {
|
|
26
|
+
return Object.keys(inputSchema.properties).filter((key) => !!inputSchema.properties[key].isSecret);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Encrypts input secret value
|
|
30
|
+
* Depending on the type of value, it returns either a string (for strings) or an object (for objects) with the `secret` key.
|
|
31
|
+
*/
|
|
32
|
+
export function encryptInputSecretValue({ value, publicKey, schema, }) {
|
|
33
|
+
parseArgument(value, z.union([z.string(), objectSchema]), 'encryptInputSecretValue');
|
|
34
|
+
parseArgument(publicKey, keyObjectSchema, 'encryptInputSecretValue');
|
|
35
|
+
parseArgument(schema, objectSchema.optional(), 'encryptInputSecretValue');
|
|
36
|
+
// TODO to make string encryption compatible with current SDK, we need to use the old form.
|
|
37
|
+
// Remove this once SDK is updated to use the new form
|
|
38
|
+
if (typeof value === 'string') {
|
|
39
|
+
const { encryptedValue, encryptedPassword } = publicEncrypt({ value, publicKey });
|
|
40
|
+
return `${ENCRYPTED_STRING_VALUE_PREFIX}:${encryptedPassword}:${encryptedValue}`;
|
|
41
|
+
}
|
|
42
|
+
const schemaHash = schema ? getFieldSchemaHash(schema) : null;
|
|
43
|
+
// We are encrypting the value as a JSON string, so we need to stringify it first.
|
|
44
|
+
let valueStr;
|
|
45
|
+
try {
|
|
46
|
+
valueStr = JSON.stringify(value);
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
throw new Error(`The input value could not be stringified for encryption: ${err}`);
|
|
50
|
+
}
|
|
51
|
+
const { encryptedValue, encryptedPassword } = publicEncrypt({ value: valueStr, publicKey });
|
|
52
|
+
return `${ENCRYPTED_JSON_VALUE_PREFIX}:${schemaHash ? `${schemaHash}:` : ''}${encryptedPassword}:${encryptedValue}`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Checks if the value is an encrypted value for a specific field type.
|
|
56
|
+
* It validates the string value against the regular expression and checks the prefix.
|
|
57
|
+
* @param value - encrypted value to check
|
|
58
|
+
* @param fieldType - type of the field, can be 'string', 'object' or 'array'
|
|
59
|
+
*/
|
|
60
|
+
export function isEncryptedValueForFieldType(value, fieldType) {
|
|
61
|
+
parseArgument(value, z.string(), 'isEncryptedValueForFieldType');
|
|
62
|
+
parseArgument(fieldType, z.enum(['string', 'object', 'array']), 'isEncryptedValueForFieldType');
|
|
63
|
+
const match = value.match(ENCRYPTED_VALUE_REGEXP);
|
|
64
|
+
if (!match)
|
|
65
|
+
return false;
|
|
66
|
+
const [, prefix] = match;
|
|
67
|
+
// For backward compatibility, we allow the old prefix only for string values.
|
|
68
|
+
if (prefix === ENCRYPTED_STRING_VALUE_PREFIX && fieldType !== 'string')
|
|
69
|
+
return false;
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Checks if the value is an encrypted value for a specific field schema.
|
|
74
|
+
* It validates the string value against the regular expression and checks the schema hash in
|
|
75
|
+
* the encrypted value against the hash of the field schema.
|
|
76
|
+
* @param value - encrypted value to check
|
|
77
|
+
* @param fieldSchema - schema of the field, used to get the hash
|
|
78
|
+
*/
|
|
79
|
+
export function isEncryptedValueForFieldSchema(value, fieldSchema) {
|
|
80
|
+
parseArgument(value, z.string(), 'isEncryptedValueForFieldSchema');
|
|
81
|
+
parseArgument(fieldSchema, objectSchema, 'isEncryptedValueForFieldSchema');
|
|
82
|
+
const match = value.match(ENCRYPTED_VALUE_REGEXP);
|
|
83
|
+
if (!match)
|
|
84
|
+
return false;
|
|
85
|
+
const [, prefix, schemaHash] = match;
|
|
86
|
+
if (prefix !== ENCRYPTED_STRING_VALUE_PREFIX && prefix !== ENCRYPTED_JSON_VALUE_PREFIX)
|
|
87
|
+
return false;
|
|
88
|
+
if (schemaHash) {
|
|
89
|
+
const fieldSchemaHash = getFieldSchemaHash(fieldSchema);
|
|
90
|
+
return schemaHash === fieldSchemaHash;
|
|
91
|
+
}
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Encrypts actor input secrets
|
|
96
|
+
*/
|
|
97
|
+
export function encryptInputSecrets({ input, inputSchema, publicKey, }) {
|
|
98
|
+
parseArgument(input, objectSchema, 'encryptInputSecrets');
|
|
99
|
+
parseArgument(inputSchema, objectSchema, 'encryptInputSecrets');
|
|
100
|
+
parseArgument(publicKey, keyObjectSchema, 'encryptInputSecrets');
|
|
101
|
+
const secretsInInputKeys = getInputSchemaSecretFieldKeys(inputSchema);
|
|
102
|
+
if (secretsInInputKeys.length === 0)
|
|
103
|
+
return input;
|
|
104
|
+
const encryptedInput = {};
|
|
105
|
+
for (const key of secretsInInputKeys) {
|
|
106
|
+
const value = input[key];
|
|
107
|
+
// NOTE: Skips already encrypted values. It can happens in case client already encrypted values, before
|
|
108
|
+
// sending them using API. Or input was takes from task, run console or scheduler, where input is stored encrypted.
|
|
109
|
+
if (value && !(typeof value === 'string' && ENCRYPTED_VALUE_REGEXP.test(value))) {
|
|
110
|
+
try {
|
|
111
|
+
encryptedInput[key] = encryptInputSecretValue({
|
|
112
|
+
value: input[key],
|
|
113
|
+
publicKey,
|
|
114
|
+
schema: inputSchema.properties[key],
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
throw new Error(`The input field "${key}" could not be encrypted. Try updating the field's value in the input editor. ` +
|
|
119
|
+
`Encryption error: ${err}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return { ...input, ...encryptedInput };
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Decrypts actor input secrets
|
|
127
|
+
* @param input - actor input object that may contain encrypted secret values
|
|
128
|
+
* @param privateKey - private key used to decrypt the secret values
|
|
129
|
+
*/
|
|
130
|
+
export function decryptInputSecrets({ input, privateKey }) {
|
|
131
|
+
const validatedInput = parseArgument(input, objectSchema, 'decryptInputSecrets');
|
|
132
|
+
parseArgument(privateKey, keyObjectSchema, 'decryptInputSecrets');
|
|
133
|
+
const decryptedInput = {};
|
|
134
|
+
for (const [key, value] of Object.entries(validatedInput)) {
|
|
135
|
+
if (typeof value === 'string' && ENCRYPTED_VALUE_REGEXP.test(value)) {
|
|
136
|
+
const match = value.match(ENCRYPTED_VALUE_REGEXP);
|
|
137
|
+
if (!match)
|
|
138
|
+
continue;
|
|
139
|
+
const [, prefix, , encryptedPassword, encryptedValue] = match;
|
|
140
|
+
try {
|
|
141
|
+
const decryptedValue = privateDecrypt({ privateKey, encryptedPassword, encryptedValue });
|
|
142
|
+
if (prefix === ENCRYPTED_STRING_VALUE_PREFIX) {
|
|
143
|
+
decryptedInput[key] = decryptedValue;
|
|
144
|
+
}
|
|
145
|
+
else if (prefix === ENCRYPTED_JSON_VALUE_PREFIX) {
|
|
146
|
+
// For JSON values, we need to parse the decrypted string into an object.
|
|
147
|
+
decryptedInput[key] = JSON.parse(decryptedValue);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
catch (err) {
|
|
151
|
+
throw new Error(`The input field "${key}" could not be decrypted. Try updating the field's value in the input editor. ` +
|
|
152
|
+
`Decryption error: ${err}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return { ...input, ...decryptedInput };
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=input_secrets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input_secrets.js","sourceRoot":"","sources":["../src/input_secrets.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEjE,4FAA4F;AAC5F,MAAM,eAAe,GAAG,CAAC,CAAC,MAAM,CAAY,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,YAAY,SAAS,EAAE;IAC/E,KAAK,EAAE,kDAAkD;CAC5D,CAAC,CAAC;AAEH,MAAM,aAAa,GAAG,uBAAuB,CAAC;AAE9C,kGAAkG;AAClG,6GAA6G;AAC7G,+GAA+G;AAC/G,yDAAyD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gBAEzF,IAAI,MAAM,KAAK,6BAA6B,EAAE,CAAC;oBAC3C,cAAc,CAAC,GAAG,CAAC,GAAG,cAAc,CAAC;gBACzC,CAAC;qBAAM,IAAI,MAAM,KAAK,2BAA2B,EAAE,CAAC;oBAChD,yEAAyE;oBACzE,cAAc,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,CAAC;gBACrD,CAAC;YACL,CAAC;YAAC,OAAO,GAAG,EAAE,CAAC;gBACX,MAAM,IAAI,KAAK,CACX,oBAAoB,GAAG,gFAAgF;oBACnG,qBAAqB,GAAG,EAAE,CACjC,CAAC;YACN,CAAC;QACL,CAAC;IACL,CAAC;IAED,OAAO,EAAE,GAAG,KAAK,EAAE,GAAG,cAAc,EAAE,CAAC;AAC3C,CAAC","sourcesContent":["import { KeyObject } from 'node:crypto';\n\nimport { z } from 'zod';\n\nimport { privateDecrypt, publicEncrypt } from '@apify/utilities';\n\nimport { getFieldSchemaHash } from './field_schema_utils.js';\nimport { objectSchema, parseArgument } from '@apify/validations';\n\n// `z.instanceof()` rejects `KeyObject` because its constructor is not public in @types/node\nconst keyObjectSchema = z.custom<KeyObject>((value) => value instanceof KeyObject, {\n error: 'Invalid input: expected an instance of KeyObject',\n});\n\nconst BASE64_REGEXP = /[-A-Za-z0-9+/]*={0,3}/;\n\n// The encrypted value has a prefix, optional schema hash, encrypted password and encrypted value.\n// - The prefix tells if the value is a string or a JSON object and needs to be parsed back after decryption.\n// - The schema hash is optional and is used to verify if the schema has changed since the value was encrypted.\n// - The encrypted password is used to decrypt the value.\n// - The encrypted value is the actual encrypted data.\n\n// used for backward compatibility with old encrypted string values\nconst ENCRYPTED_STRING_VALUE_PREFIX = 'ENCRYPTED_VALUE';\n// we use this for all types of encrypted values (string, object, array)\nconst ENCRYPTED_JSON_VALUE_PREFIX = 'ENCRYPTED_JSON';\n\n// All encrypted values must match this regular expression.\nconst ENCRYPTED_VALUE_REGEXP = new RegExp(\n `^(${ENCRYPTED_STRING_VALUE_PREFIX}|${ENCRYPTED_JSON_VALUE_PREFIX}):(?:(${BASE64_REGEXP.source}):)?(${BASE64_REGEXP.source}):(${BASE64_REGEXP.source})$`,\n);\n\n/**\n * Get keys of secret fields from input schema\n */\nexport function getInputSchemaSecretFieldKeys(inputSchema: any): string[] {\n return Object.keys(inputSchema.properties).filter((key) => !!inputSchema.properties[key].isSecret);\n}\n\n/**\n * Encrypts input secret value\n * Depending on the type of value, it returns either a string (for strings) or an object (for objects) with the `secret` key.\n */\nexport function encryptInputSecretValue<T extends string | object>({\n value,\n publicKey,\n schema,\n}: {\n value: T;\n publicKey: KeyObject;\n schema?: Record<string, any>;\n}): string {\n parseArgument(value, z.union([z.string(), objectSchema]), 'encryptInputSecretValue');\n parseArgument(publicKey, keyObjectSchema, 'encryptInputSecretValue');\n parseArgument(schema, objectSchema.optional(), 'encryptInputSecretValue');\n\n // TODO to make string encryption compatible with current SDK, we need to use the old form.\n // Remove this once SDK is updated to use the new form\n if (typeof value === 'string') {\n const { encryptedValue, encryptedPassword } = publicEncrypt({ value, publicKey });\n return `${ENCRYPTED_STRING_VALUE_PREFIX}:${encryptedPassword}:${encryptedValue}`;\n }\n\n const schemaHash = schema ? getFieldSchemaHash(schema) : null;\n\n // We are encrypting the value as a JSON string, so we need to stringify it first.\n let valueStr: string;\n try {\n valueStr = JSON.stringify(value);\n } catch (err) {\n throw new Error(`The input value could not be stringified for encryption: ${err}`);\n }\n\n const { encryptedValue, encryptedPassword } = publicEncrypt({ value: valueStr, publicKey });\n return `${ENCRYPTED_JSON_VALUE_PREFIX}:${schemaHash ? `${schemaHash}:` : ''}${encryptedPassword}:${encryptedValue}`;\n}\n\n/**\n * Checks if the value is an encrypted value for a specific field type.\n * It validates the string value against the regular expression and checks the prefix.\n * @param value - encrypted value to check\n * @param fieldType - type of the field, can be 'string', 'object' or 'array'\n */\nexport function isEncryptedValueForFieldType(value: string, fieldType: 'string' | 'object' | 'array') {\n parseArgument(value, z.string(), 'isEncryptedValueForFieldType');\n parseArgument(fieldType, z.enum(['string', 'object', 'array']), 'isEncryptedValueForFieldType');\n\n const match = value.match(ENCRYPTED_VALUE_REGEXP);\n if (!match) return false;\n\n const [, prefix] = match;\n\n // For backward compatibility, we allow the old prefix only for string values.\n if (prefix === ENCRYPTED_STRING_VALUE_PREFIX && fieldType !== 'string') return false;\n\n return true;\n}\n\n/**\n * Checks if the value is an encrypted value for a specific field schema.\n * It validates the string value against the regular expression and checks the schema hash in\n * the encrypted value against the hash of the field schema.\n * @param value - encrypted value to check\n * @param fieldSchema - schema of the field, used to get the hash\n */\nexport function isEncryptedValueForFieldSchema(value: string, fieldSchema: Record<string, any>) {\n parseArgument(value, z.string(), 'isEncryptedValueForFieldSchema');\n parseArgument(fieldSchema, objectSchema, 'isEncryptedValueForFieldSchema');\n\n const match = value.match(ENCRYPTED_VALUE_REGEXP);\n if (!match) return false;\n\n const [, prefix, schemaHash] = match;\n\n if (prefix !== ENCRYPTED_STRING_VALUE_PREFIX && prefix !== ENCRYPTED_JSON_VALUE_PREFIX) return false;\n\n if (schemaHash) {\n const fieldSchemaHash = getFieldSchemaHash(fieldSchema);\n return schemaHash === fieldSchemaHash;\n }\n\n return true;\n}\n\n/**\n * Encrypts actor input secrets\n */\nexport function encryptInputSecrets<T extends Record<string, any>>({\n input,\n inputSchema,\n publicKey,\n}: {\n input: T;\n inputSchema: object;\n publicKey: KeyObject;\n}): T {\n parseArgument(input, objectSchema, 'encryptInputSecrets');\n parseArgument(inputSchema, objectSchema, 'encryptInputSecrets');\n parseArgument(publicKey, keyObjectSchema, 'encryptInputSecrets');\n\n const secretsInInputKeys = getInputSchemaSecretFieldKeys(inputSchema);\n if (secretsInInputKeys.length === 0) return input;\n\n const encryptedInput = {} as Record<string, any>;\n for (const key of secretsInInputKeys) {\n const value = input[key];\n // NOTE: Skips already encrypted values. It can happens in case client already encrypted values, before\n // sending them using API. Or input was takes from task, run console or scheduler, where input is stored encrypted.\n if (value && !(typeof value === 'string' && ENCRYPTED_VALUE_REGEXP.test(value))) {\n try {\n encryptedInput[key] = encryptInputSecretValue({\n value: input[key],\n publicKey,\n schema: (inputSchema as any).properties[key],\n });\n } catch (err) {\n throw new Error(\n `The input field \"${key}\" could not be encrypted. Try updating the field's value in the input editor. ` +\n `Encryption error: ${err}`,\n );\n }\n }\n }\n\n return { ...input, ...encryptedInput };\n}\n\n/**\n * Decrypts actor input secrets\n * @param input - actor input object that may contain encrypted secret values\n * @param privateKey - private key used to decrypt the secret values\n */\nexport function decryptInputSecrets<T>({ input, privateKey }: { input: T; privateKey: KeyObject }): T {\n const validatedInput = parseArgument(input, objectSchema, 'decryptInputSecrets');\n parseArgument(privateKey, keyObjectSchema, 'decryptInputSecrets');\n\n const decryptedInput = {} as Record<string, any>;\n for (const [key, value] of Object.entries(validatedInput)) {\n if (typeof value === 'string' && ENCRYPTED_VALUE_REGEXP.test(value)) {\n const match = value.match(ENCRYPTED_VALUE_REGEXP);\n if (!match) continue;\n const [, prefix, , encryptedPassword, encryptedValue] = match;\n try {\n const decryptedValue = privateDecrypt({ privateKey, encryptedPassword, encryptedValue });\n\n if (prefix === ENCRYPTED_STRING_VALUE_PREFIX) {\n decryptedInput[key] = decryptedValue;\n } else if (prefix === ENCRYPTED_JSON_VALUE_PREFIX) {\n // For JSON values, we need to parse the decrypted string into an object.\n decryptedInput[key] = JSON.parse(decryptedValue);\n }\n } catch (err) {\n throw new Error(\n `The input field \"${key}\" could not be decrypted. Try updating the field's value in the input editor. ` +\n `Decryption error: ${err}`,\n );\n }\n }\n }\n\n return { ...input, ...decryptedInput };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@apify/input_secrets",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-beta.0",
|
|
4
4
|
"description": "Package to encrypt and decrypt apify actor input secrets.",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
"require": {
|
|
15
|
-
"types": "./cjs/index.d.ts",
|
|
16
|
-
"default": "./cjs/index.cjs"
|
|
17
|
-
}
|
|
18
|
-
}
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"default": "./index.js"
|
|
12
|
+
},
|
|
13
|
+
"./package.json": "./package.json"
|
|
19
14
|
},
|
|
20
15
|
"keywords": [
|
|
21
16
|
"apify"
|
|
@@ -36,17 +31,21 @@
|
|
|
36
31
|
"homepage": "https://apify.com",
|
|
37
32
|
"scripts": {
|
|
38
33
|
"build": "pnpm clean && pnpm compile && pnpm copy",
|
|
39
|
-
"clean": "
|
|
40
|
-
"compile": "
|
|
41
|
-
"copy": "
|
|
34
|
+
"clean": "node ../../scripts/clean.ts",
|
|
35
|
+
"compile": "tsc -p tsconfig.build.json",
|
|
36
|
+
"copy": "node ../../scripts/copy.ts"
|
|
42
37
|
},
|
|
43
38
|
"publishConfig": {
|
|
44
39
|
"access": "public"
|
|
45
40
|
},
|
|
46
41
|
"dependencies": {
|
|
47
|
-
"@apify/log": "^
|
|
48
|
-
"@apify/utilities": "^
|
|
49
|
-
"
|
|
42
|
+
"@apify/log": "^3.0.0-beta.2",
|
|
43
|
+
"@apify/utilities": "^3.0.0-beta.2",
|
|
44
|
+
"@apify/validations": "^1.0.0-beta.1",
|
|
45
|
+
"zod": "^4.0.0"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=22"
|
|
50
49
|
},
|
|
51
50
|
"lerna": {
|
|
52
51
|
"command": {
|
|
@@ -55,5 +54,5 @@
|
|
|
55
54
|
}
|
|
56
55
|
}
|
|
57
56
|
},
|
|
58
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "bfcf1b6ac826f248d84cc43c56e81bc09232a97a"
|
|
59
58
|
}
|
package/cjs/index.cjs
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
const __ow_import = require('ow');
|
|
2
|
-
const __injectedOw = __ow_import.default || __ow_import;
|
|
3
|
-
"use strict";
|
|
4
|
-
var __create = Object.create;
|
|
5
|
-
var __defProp = Object.defineProperty;
|
|
6
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
11
|
-
var __export = (target, all) => {
|
|
12
|
-
for (var name in all)
|
|
13
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
14
|
-
};
|
|
15
|
-
var __copyProps = (to, from, except, desc) => {
|
|
16
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
-
for (let key of __getOwnPropNames(from))
|
|
18
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
-
}
|
|
21
|
-
return to;
|
|
22
|
-
};
|
|
23
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
-
mod
|
|
30
|
-
));
|
|
31
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
32
|
-
|
|
33
|
-
// src/index.ts
|
|
34
|
-
var index_exports = {};
|
|
35
|
-
__export(index_exports, {
|
|
36
|
-
decryptInputSecrets: () => decryptInputSecrets,
|
|
37
|
-
encryptInputSecretValue: () => encryptInputSecretValue,
|
|
38
|
-
encryptInputSecrets: () => encryptInputSecrets,
|
|
39
|
-
getInputSchemaSecretFieldKeys: () => getInputSchemaSecretFieldKeys,
|
|
40
|
-
isEncryptedValueForFieldSchema: () => isEncryptedValueForFieldSchema,
|
|
41
|
-
isEncryptedValueForFieldType: () => isEncryptedValueForFieldType
|
|
42
|
-
});
|
|
43
|
-
module.exports = __toCommonJS(index_exports);
|
|
44
|
-
|
|
45
|
-
// src/input_secrets.ts
|
|
46
|
-
var import_node_crypto2 = require("crypto");
|
|
47
|
-
var import_ow = __toESM(require("ow"));
|
|
48
|
-
var import_utilities = require("@apify/utilities");
|
|
49
|
-
|
|
50
|
-
// src/field_schema_utils.ts
|
|
51
|
-
var import_node_crypto = __toESM(require("crypto"));
|
|
52
|
-
var OMIT_KEYS = /* @__PURE__ */ new Set([
|
|
53
|
-
"title",
|
|
54
|
-
"description",
|
|
55
|
-
"sectionCaption",
|
|
56
|
-
"sectionDescription",
|
|
57
|
-
"nullable",
|
|
58
|
-
"example",
|
|
59
|
-
"prefill",
|
|
60
|
-
"editor"
|
|
61
|
-
]);
|
|
62
|
-
function normalizeFieldSchema(value) {
|
|
63
|
-
if (Array.isArray(value)) {
|
|
64
|
-
return value.map(normalizeFieldSchema);
|
|
65
|
-
}
|
|
66
|
-
if (value && typeof value === "object") {
|
|
67
|
-
const result = {};
|
|
68
|
-
Object.keys(value).filter((key) => !OMIT_KEYS.has(key)).sort().forEach((key) => {
|
|
69
|
-
result[key] = normalizeFieldSchema(value[key]);
|
|
70
|
-
});
|
|
71
|
-
return result;
|
|
72
|
-
}
|
|
73
|
-
return value;
|
|
74
|
-
}
|
|
75
|
-
__name(normalizeFieldSchema, "normalizeFieldSchema");
|
|
76
|
-
function getFieldSchemaHash(fieldSchema) {
|
|
77
|
-
try {
|
|
78
|
-
const stringifiedSchema = JSON.stringify(normalizeFieldSchema(fieldSchema));
|
|
79
|
-
return import_node_crypto.default.createHash("sha256").update(stringifiedSchema).digest("hex").slice(0, 10);
|
|
80
|
-
} catch (err) {
|
|
81
|
-
throw new Error(`The field schema could not be stringified for hash: ${err}`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
__name(getFieldSchemaHash, "getFieldSchemaHash");
|
|
85
|
-
|
|
86
|
-
// src/input_secrets.ts
|
|
87
|
-
var ow = typeof __injectedOw === "undefined" ? import_ow.default : __injectedOw || import_ow.default;
|
|
88
|
-
var BASE64_REGEXP = /[-A-Za-z0-9+/]*={0,3}/;
|
|
89
|
-
var ENCRYPTED_STRING_VALUE_PREFIX = "ENCRYPTED_VALUE";
|
|
90
|
-
var ENCRYPTED_JSON_VALUE_PREFIX = "ENCRYPTED_JSON";
|
|
91
|
-
var ENCRYPTED_VALUE_REGEXP = new RegExp(
|
|
92
|
-
`^(${ENCRYPTED_STRING_VALUE_PREFIX}|${ENCRYPTED_JSON_VALUE_PREFIX}):(?:(${BASE64_REGEXP.source}):)?(${BASE64_REGEXP.source}):(${BASE64_REGEXP.source})$`
|
|
93
|
-
);
|
|
94
|
-
function getInputSchemaSecretFieldKeys(inputSchema) {
|
|
95
|
-
return Object.keys(inputSchema.properties).filter((key) => !!inputSchema.properties[key].isSecret);
|
|
96
|
-
}
|
|
97
|
-
__name(getInputSchemaSecretFieldKeys, "getInputSchemaSecretFieldKeys");
|
|
98
|
-
function encryptInputSecretValue({
|
|
99
|
-
value,
|
|
100
|
-
publicKey,
|
|
101
|
-
schema
|
|
102
|
-
}) {
|
|
103
|
-
ow(value, ow.any(ow.string, ow.object));
|
|
104
|
-
ow(publicKey, ow.object.instanceOf(import_node_crypto2.KeyObject));
|
|
105
|
-
ow(schema, ow.optional.object);
|
|
106
|
-
if (typeof value === "string") {
|
|
107
|
-
const { encryptedValue: encryptedValue2, encryptedPassword: encryptedPassword2 } = (0, import_utilities.publicEncrypt)({ value, publicKey });
|
|
108
|
-
return `${ENCRYPTED_STRING_VALUE_PREFIX}:${encryptedPassword2}:${encryptedValue2}`;
|
|
109
|
-
}
|
|
110
|
-
const schemaHash = schema ? getFieldSchemaHash(schema) : null;
|
|
111
|
-
let valueStr;
|
|
112
|
-
try {
|
|
113
|
-
valueStr = JSON.stringify(value);
|
|
114
|
-
} catch (err) {
|
|
115
|
-
throw new Error(`The input value could not be stringified for encryption: ${err}`);
|
|
116
|
-
}
|
|
117
|
-
const { encryptedValue, encryptedPassword } = (0, import_utilities.publicEncrypt)({ value: valueStr, publicKey });
|
|
118
|
-
return `${ENCRYPTED_JSON_VALUE_PREFIX}:${schemaHash ? `${schemaHash}:` : ""}${encryptedPassword}:${encryptedValue}`;
|
|
119
|
-
}
|
|
120
|
-
__name(encryptInputSecretValue, "encryptInputSecretValue");
|
|
121
|
-
function isEncryptedValueForFieldType(value, fieldType) {
|
|
122
|
-
ow(value, ow.string);
|
|
123
|
-
ow(fieldType, ow.string.oneOf(["string", "object", "array"]));
|
|
124
|
-
const match = value.match(ENCRYPTED_VALUE_REGEXP);
|
|
125
|
-
if (!match) return false;
|
|
126
|
-
const [, prefix] = match;
|
|
127
|
-
if (prefix === ENCRYPTED_STRING_VALUE_PREFIX && fieldType !== "string") return false;
|
|
128
|
-
return true;
|
|
129
|
-
}
|
|
130
|
-
__name(isEncryptedValueForFieldType, "isEncryptedValueForFieldType");
|
|
131
|
-
function isEncryptedValueForFieldSchema(value, fieldSchema) {
|
|
132
|
-
ow(value, ow.string);
|
|
133
|
-
ow(fieldSchema, ow.object);
|
|
134
|
-
const match = value.match(ENCRYPTED_VALUE_REGEXP);
|
|
135
|
-
if (!match) return false;
|
|
136
|
-
const [, prefix, schemaHash] = match;
|
|
137
|
-
if (prefix !== ENCRYPTED_STRING_VALUE_PREFIX && prefix !== ENCRYPTED_JSON_VALUE_PREFIX) return false;
|
|
138
|
-
if (schemaHash) {
|
|
139
|
-
const fieldSchemaHash = getFieldSchemaHash(fieldSchema);
|
|
140
|
-
return schemaHash === fieldSchemaHash;
|
|
141
|
-
}
|
|
142
|
-
return true;
|
|
143
|
-
}
|
|
144
|
-
__name(isEncryptedValueForFieldSchema, "isEncryptedValueForFieldSchema");
|
|
145
|
-
function encryptInputSecrets({
|
|
146
|
-
input,
|
|
147
|
-
inputSchema,
|
|
148
|
-
publicKey
|
|
149
|
-
}) {
|
|
150
|
-
ow(input, ow.object);
|
|
151
|
-
ow(inputSchema, ow.object);
|
|
152
|
-
ow(publicKey, ow.object.instanceOf(import_node_crypto2.KeyObject));
|
|
153
|
-
const secretsInInputKeys = getInputSchemaSecretFieldKeys(inputSchema);
|
|
154
|
-
if (secretsInInputKeys.length === 0) return input;
|
|
155
|
-
const encryptedInput = {};
|
|
156
|
-
for (const key of secretsInInputKeys) {
|
|
157
|
-
const value = input[key];
|
|
158
|
-
if (value && !(ow.isValid(value, ow.string) && ENCRYPTED_VALUE_REGEXP.test(value))) {
|
|
159
|
-
try {
|
|
160
|
-
encryptedInput[key] = encryptInputSecretValue({
|
|
161
|
-
value: input[key],
|
|
162
|
-
publicKey,
|
|
163
|
-
schema: inputSchema.properties[key]
|
|
164
|
-
});
|
|
165
|
-
} catch (err) {
|
|
166
|
-
throw new Error(
|
|
167
|
-
`The input field "${key}" could not be encrypted. Try updating the field's value in the input editor. Encryption error: ${err}`
|
|
168
|
-
);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
return { ...input, ...encryptedInput };
|
|
173
|
-
}
|
|
174
|
-
__name(encryptInputSecrets, "encryptInputSecrets");
|
|
175
|
-
function decryptInputSecrets({ input, privateKey }) {
|
|
176
|
-
ow(input, ow.object);
|
|
177
|
-
ow(privateKey, ow.object.instanceOf(import_node_crypto2.KeyObject));
|
|
178
|
-
const decryptedInput = {};
|
|
179
|
-
for (const [key, value] of Object.entries(input)) {
|
|
180
|
-
if (typeof value === "string" && ENCRYPTED_VALUE_REGEXP.test(value)) {
|
|
181
|
-
const match = value.match(ENCRYPTED_VALUE_REGEXP);
|
|
182
|
-
if (!match) continue;
|
|
183
|
-
const [, prefix, , encryptedPassword, encryptedValue] = match;
|
|
184
|
-
try {
|
|
185
|
-
const decryptedValue = (0, import_utilities.privateDecrypt)({ privateKey, encryptedPassword, encryptedValue });
|
|
186
|
-
if (prefix === ENCRYPTED_STRING_VALUE_PREFIX) {
|
|
187
|
-
decryptedInput[key] = decryptedValue;
|
|
188
|
-
} else if (prefix === ENCRYPTED_JSON_VALUE_PREFIX) {
|
|
189
|
-
decryptedInput[key] = JSON.parse(decryptedValue);
|
|
190
|
-
}
|
|
191
|
-
} catch (err) {
|
|
192
|
-
throw new Error(
|
|
193
|
-
`The input field "${key}" could not be decrypted. Try updating the field's value in the input editor. Decryption error: ${err}`
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
return { ...input, ...decryptedInput };
|
|
199
|
-
}
|
|
200
|
-
__name(decryptInputSecrets, "decryptInputSecrets");
|
|
201
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
202
|
-
0 && (module.exports = {
|
|
203
|
-
decryptInputSecrets,
|
|
204
|
-
encryptInputSecretValue,
|
|
205
|
-
encryptInputSecrets,
|
|
206
|
-
getInputSchemaSecretFieldKeys,
|
|
207
|
-
isEncryptedValueForFieldSchema,
|
|
208
|
-
isEncryptedValueForFieldType
|
|
209
|
-
});
|
|
210
|
-
//# sourceMappingURL=index.cjs.map
|
package/cjs/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/input_secrets.ts","../../src/field_schema_utils.ts"],"sourcesContent":["export * from './input_secrets';\n","import { KeyObject } from 'node:crypto';\n\nimport _testOw, { type Ow } from 'ow';\n\nimport { privateDecrypt, publicEncrypt } from '@apify/utilities';\n\nimport { getFieldSchemaHash } from './field_schema_utils';\n\n// eslint-disable-next-line no-underscore-dangle\ndeclare const __injectedOw: Ow;\n\nconst ow: Ow = typeof __injectedOw === 'undefined' ? _testOw : __injectedOw || _testOw;\n\nconst BASE64_REGEXP = /[-A-Za-z0-9+/]*={0,3}/;\n\n// The encrypted value has a prefix, optional schema hash, encrypted password and encrypted value.\n// - The prefix tells if the value is a string or a JSON object and needs to be parsed back after decryption.\n// - The schema hash is optional and is used to verify if the schema has changed since the value was encrypted.\n// - The encrypted password is used to decrypt the value.\n// - The encrypted value is the actual encrypted data.\n\n// used for backward compatibility with old encrypted string values\nconst ENCRYPTED_STRING_VALUE_PREFIX = 'ENCRYPTED_VALUE';\n// we use this for all types of encrypted values (string, object, array)\nconst ENCRYPTED_JSON_VALUE_PREFIX = 'ENCRYPTED_JSON';\n\n// All encrypted values must match this regular expression.\nconst ENCRYPTED_VALUE_REGEXP = new RegExp(\n `^(${ENCRYPTED_STRING_VALUE_PREFIX}|${ENCRYPTED_JSON_VALUE_PREFIX}):(?:(${BASE64_REGEXP.source}):)?(${BASE64_REGEXP.source}):(${BASE64_REGEXP.source})$`,\n);\n\n/**\n * Get keys of secret fields from input schema\n */\nexport function getInputSchemaSecretFieldKeys(inputSchema: any): string[] {\n return Object.keys(inputSchema.properties).filter((key) => !!inputSchema.properties[key].isSecret);\n}\n\n/**\n * Encrypts input secret value\n * Depending on the type of value, it returns either a string (for strings) or an object (for objects) with the `secret` key.\n */\nexport function encryptInputSecretValue<T extends string | object>({\n value,\n publicKey,\n schema,\n}: {\n value: T;\n publicKey: KeyObject;\n schema?: Record<string, any>;\n}): string {\n ow(value, ow.any(ow.string, ow.object));\n ow(publicKey, ow.object.instanceOf(KeyObject));\n ow(schema, ow.optional.object);\n\n // TODO to make string encryption compatible with current SDK, we need to use the old form.\n // Remove this once SDK is updated to use the new form\n if (typeof value === 'string') {\n const { encryptedValue, encryptedPassword } = publicEncrypt({ value, publicKey });\n return `${ENCRYPTED_STRING_VALUE_PREFIX}:${encryptedPassword}:${encryptedValue}`;\n }\n\n const schemaHash = schema ? getFieldSchemaHash(schema) : null;\n\n // We are encrypting the value as a JSON string, so we need to stringify it first.\n let valueStr: string;\n try {\n valueStr = JSON.stringify(value);\n } catch (err) {\n throw new Error(`The input value could not be stringified for encryption: ${err}`);\n }\n\n const { encryptedValue, encryptedPassword } = publicEncrypt({ value: valueStr, publicKey });\n return `${ENCRYPTED_JSON_VALUE_PREFIX}:${schemaHash ? `${schemaHash}:` : ''}${encryptedPassword}:${encryptedValue}`;\n}\n\n/**\n * Checks if the value is an encrypted value for a specific field type.\n * It validates the string value against the regular expression and checks the prefix.\n * @param value - encrypted value to check\n * @param fieldType - type of the field, can be 'string', 'object' or 'array'\n */\nexport function isEncryptedValueForFieldType(value: string, fieldType: 'string' | 'object' | 'array') {\n ow(value, ow.string);\n ow(fieldType, ow.string.oneOf(['string', 'object', 'array']));\n\n const match = value.match(ENCRYPTED_VALUE_REGEXP);\n if (!match) return false;\n\n const [, prefix] = match;\n\n // For backward compatibility, we allow the old prefix only for string values.\n if (prefix === ENCRYPTED_STRING_VALUE_PREFIX && fieldType !== 'string') return false;\n\n return true;\n}\n\n/**\n * Checks if the value is an encrypted value for a specific field schema.\n * It validates the string value against the regular expression and checks the schema hash in\n * the encrypted value against the hash of the field schema.\n * @param value - encrypted value to check\n * @param fieldSchema - schema of the field, used to get the hash\n */\nexport function isEncryptedValueForFieldSchema(value: string, fieldSchema: Record<string, any>) {\n ow(value, ow.string);\n ow(fieldSchema, ow.object);\n\n const match = value.match(ENCRYPTED_VALUE_REGEXP);\n if (!match) return false;\n\n const [, prefix, schemaHash] = match;\n\n if (prefix !== ENCRYPTED_STRING_VALUE_PREFIX && prefix !== ENCRYPTED_JSON_VALUE_PREFIX) return false;\n\n if (schemaHash) {\n const fieldSchemaHash = getFieldSchemaHash(fieldSchema);\n return schemaHash === fieldSchemaHash;\n }\n\n return true;\n}\n\n/**\n * Encrypts actor input secrets\n */\nexport function encryptInputSecrets<T extends Record<string, any>>({\n input,\n inputSchema,\n publicKey,\n}: {\n input: T;\n inputSchema: object;\n publicKey: KeyObject;\n}): T {\n ow(input, ow.object);\n ow(inputSchema, ow.object);\n ow(publicKey, ow.object.instanceOf(KeyObject));\n\n const secretsInInputKeys = getInputSchemaSecretFieldKeys(inputSchema);\n if (secretsInInputKeys.length === 0) return input;\n\n const encryptedInput = {} as Record<string, any>;\n for (const key of secretsInInputKeys) {\n const value = input[key];\n // NOTE: Skips already encrypted values. It can happens in case client already encrypted values, before\n // sending them using API. Or input was takes from task, run console or scheduler, where input is stored encrypted.\n if (value && !(ow.isValid(value, ow.string) && ENCRYPTED_VALUE_REGEXP.test(value))) {\n try {\n encryptedInput[key] = encryptInputSecretValue({\n value: input[key],\n publicKey,\n schema: (inputSchema as any).properties[key],\n });\n } catch (err) {\n throw new Error(\n `The input field \"${key}\" could not be encrypted. Try updating the field's value in the input editor. ` +\n `Encryption error: ${err}`,\n );\n }\n }\n }\n\n return { ...input, ...encryptedInput };\n}\n\n/**\n * Decrypts actor input secrets\n * @param input - actor input object that may contain encrypted secret values\n * @param privateKey - private key used to decrypt the secret values\n */\nexport function decryptInputSecrets<T>({ input, privateKey }: { input: T; privateKey: KeyObject }): T {\n ow(input, ow.object);\n ow(privateKey, ow.object.instanceOf(KeyObject));\n\n const decryptedInput = {} as Record<string, any>;\n for (const [key, value] of Object.entries(input)) {\n if (typeof value === 'string' && ENCRYPTED_VALUE_REGEXP.test(value)) {\n const match = value.match(ENCRYPTED_VALUE_REGEXP);\n if (!match) continue;\n const [, prefix, , encryptedPassword, encryptedValue] = match;\n try {\n const decryptedValue = privateDecrypt({ privateKey, encryptedPassword, encryptedValue });\n\n if (prefix === ENCRYPTED_STRING_VALUE_PREFIX) {\n decryptedInput[key] = decryptedValue;\n } else if (prefix === ENCRYPTED_JSON_VALUE_PREFIX) {\n // For JSON values, we need to parse the decrypted string into an object.\n decryptedInput[key] = JSON.parse(decryptedValue);\n }\n } catch (err) {\n throw new Error(\n `The input field \"${key}\" could not be decrypted. Try updating the field's value in the input editor. ` +\n `Decryption error: ${err}`,\n );\n }\n }\n }\n\n return { ...input, ...decryptedInput };\n}\n","import crypto from 'node:crypto';\n\n/**\n * These keys are omitted from the field schema normalization process\n * because they are not relevant for validation of values against the schema.\n */\nconst OMIT_KEYS = new Set([\n 'title',\n 'description',\n 'sectionCaption',\n 'sectionDescription',\n 'nullable',\n 'example',\n 'prefill',\n 'editor',\n]);\n\n/**\n * Normalizes the field schema by removing irrelevant keys and sorting the remaining keys.\n */\nfunction normalizeFieldSchema(value: any): any {\n if (Array.isArray(value)) {\n return value.map(normalizeFieldSchema);\n }\n\n if (value && typeof value === 'object') {\n const result: Record<string, any> = {};\n Object.keys(value)\n .filter((key) => !OMIT_KEYS.has(key))\n .sort()\n .forEach((key) => {\n result[key] = normalizeFieldSchema(value[key]);\n });\n return result;\n }\n\n return value;\n}\n\n/**\n * Generates a stable hash for the field schema.\n * @param fieldSchema\n */\nexport function getFieldSchemaHash(fieldSchema: Record<string, any>): string {\n try {\n const stringifiedSchema = JSON.stringify(normalizeFieldSchema(fieldSchema));\n // Create a SHA-256 hash of the stringified schema and return the first 10 characters in hex.\n return crypto.createHash('sha256').update(stringifiedSchema).digest('hex').slice(0, 10);\n } catch (err) {\n throw new Error(`The field schema could not be stringified for hash: ${err}`);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,sBAA0B;AAE1B,gBAAiC;AAEjC,uBAA8C;;;ACJ9C,yBAAmB;AAMnB,IAAM,YAAY,oBAAI,IAAI;AAAA,EACtB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACJ,CAAC;AAKD,SAAS,qBAAqB,OAAiB;AAC3C,MAAI,MAAM,QAAQ,KAAK,GAAG;AACtB,WAAO,MAAM,IA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,WAAW,+BAA+B;AAC1C,yBAAe,GAAG,IAAI;AAAA,QAC1B,WAAW,WAAW,6BAA6B;AAE/C,yBAAe,GAAG,IAAI,KAAK,MAAM,cAAc;AAAA,QACnD;AAAA,MACJ,SAAS,KAAK;AACV,cAAM,IAAI;AAAA,UACN,oBAAoB,GAAG,mGACE,GAAG;AAAA,QAChC;AAAA,MACJ;AAAA,IACJ;AAAA,EACJ;AAEA,SAAO,EAAE,GAAG,OAAO,GAAG,eAAe;AACzC;AA7BgB;","names":["import_node_crypto","crypto","_testOw","encryptedValue","encryptedPassword"]}
|
package/esm/index.d.mts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { KeyObject } from 'node:crypto';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Get keys of secret fields from input schema
|
|
5
|
-
*/
|
|
6
|
-
declare function getInputSchemaSecretFieldKeys(inputSchema: any): string[];
|
|
7
|
-
/**
|
|
8
|
-
* Encrypts input secret value
|
|
9
|
-
* Depending on the type of value, it returns either a string (for strings) or an object (for objects) with the `secret` key.
|
|
10
|
-
*/
|
|
11
|
-
declare function encryptInputSecretValue<T extends string | object>({ value, publicKey, schema, }: {
|
|
12
|
-
value: T;
|
|
13
|
-
publicKey: KeyObject;
|
|
14
|
-
schema?: Record<string, any>;
|
|
15
|
-
}): string;
|
|
16
|
-
/**
|
|
17
|
-
* Checks if the value is an encrypted value for a specific field type.
|
|
18
|
-
* It validates the string value against the regular expression and checks the prefix.
|
|
19
|
-
* @param value - encrypted value to check
|
|
20
|
-
* @param fieldType - type of the field, can be 'string', 'object' or 'array'
|
|
21
|
-
*/
|
|
22
|
-
declare function isEncryptedValueForFieldType(value: string, fieldType: 'string' | 'object' | 'array'): boolean;
|
|
23
|
-
/**
|
|
24
|
-
* Checks if the value is an encrypted value for a specific field schema.
|
|
25
|
-
* It validates the string value against the regular expression and checks the schema hash in
|
|
26
|
-
* the encrypted value against the hash of the field schema.
|
|
27
|
-
* @param value - encrypted value to check
|
|
28
|
-
* @param fieldSchema - schema of the field, used to get the hash
|
|
29
|
-
*/
|
|
30
|
-
declare function isEncryptedValueForFieldSchema(value: string, fieldSchema: Record<string, any>): boolean;
|
|
31
|
-
/**
|
|
32
|
-
* Encrypts actor input secrets
|
|
33
|
-
*/
|
|
34
|
-
declare function encryptInputSecrets<T extends Record<string, any>>({ input, inputSchema, publicKey, }: {
|
|
35
|
-
input: T;
|
|
36
|
-
inputSchema: object;
|
|
37
|
-
publicKey: KeyObject;
|
|
38
|
-
}): T;
|
|
39
|
-
/**
|
|
40
|
-
* Decrypts actor input secrets
|
|
41
|
-
* @param input - actor input object that may contain encrypted secret values
|
|
42
|
-
* @param privateKey - private key used to decrypt the secret values
|
|
43
|
-
*/
|
|
44
|
-
declare function decryptInputSecrets<T>({ input, privateKey }: {
|
|
45
|
-
input: T;
|
|
46
|
-
privateKey: KeyObject;
|
|
47
|
-
}): T;
|
|
48
|
-
|
|
49
|
-
export { decryptInputSecrets, encryptInputSecretValue, encryptInputSecrets, getInputSchemaSecretFieldKeys, isEncryptedValueForFieldSchema, isEncryptedValueForFieldType };
|
package/esm/index.mjs
DELETED
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
import __ow_import from 'ow';
|
|
2
|
-
const __injectedOw = __ow_import.default || __ow_import;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
-
|
|
6
|
-
// src/input_secrets.ts
|
|
7
|
-
import { KeyObject } from "crypto";
|
|
8
|
-
import _testOw from "ow";
|
|
9
|
-
import { privateDecrypt, publicEncrypt } from "@apify/utilities";
|
|
10
|
-
|
|
11
|
-
// src/field_schema_utils.ts
|
|
12
|
-
import crypto from "crypto";
|
|
13
|
-
var OMIT_KEYS = /* @__PURE__ */ new Set([
|
|
14
|
-
"title",
|
|
15
|
-
"description",
|
|
16
|
-
"sectionCaption",
|
|
17
|
-
"sectionDescription",
|
|
18
|
-
"nullable",
|
|
19
|
-
"example",
|
|
20
|
-
"prefill",
|
|
21
|
-
"editor"
|
|
22
|
-
]);
|
|
23
|
-
function normalizeFieldSchema(value) {
|
|
24
|
-
if (Array.isArray(value)) {
|
|
25
|
-
return value.map(normalizeFieldSchema);
|
|
26
|
-
}
|
|
27
|
-
if (value && typeof value === "object") {
|
|
28
|
-
const result = {};
|
|
29
|
-
Object.keys(value).filter((key) => !OMIT_KEYS.has(key)).sort().forEach((key) => {
|
|
30
|
-
result[key] = normalizeFieldSchema(value[key]);
|
|
31
|
-
});
|
|
32
|
-
return result;
|
|
33
|
-
}
|
|
34
|
-
return value;
|
|
35
|
-
}
|
|
36
|
-
__name(normalizeFieldSchema, "normalizeFieldSchema");
|
|
37
|
-
function getFieldSchemaHash(fieldSchema) {
|
|
38
|
-
try {
|
|
39
|
-
const stringifiedSchema = JSON.stringify(normalizeFieldSchema(fieldSchema));
|
|
40
|
-
return crypto.createHash("sha256").update(stringifiedSchema).digest("hex").slice(0, 10);
|
|
41
|
-
} catch (err) {
|
|
42
|
-
throw new Error(`The field schema could not be stringified for hash: ${err}`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
__name(getFieldSchemaHash, "getFieldSchemaHash");
|
|
46
|
-
|
|
47
|
-
// src/input_secrets.ts
|
|
48
|
-
var ow = typeof __injectedOw === "undefined" ? _testOw : __injectedOw || _testOw;
|
|
49
|
-
var BASE64_REGEXP = /[-A-Za-z0-9+/]*={0,3}/;
|
|
50
|
-
var ENCRYPTED_STRING_VALUE_PREFIX = "ENCRYPTED_VALUE";
|
|
51
|
-
var ENCRYPTED_JSON_VALUE_PREFIX = "ENCRYPTED_JSON";
|
|
52
|
-
var ENCRYPTED_VALUE_REGEXP = new RegExp(
|
|
53
|
-
`^(${ENCRYPTED_STRING_VALUE_PREFIX}|${ENCRYPTED_JSON_VALUE_PREFIX}):(?:(${BASE64_REGEXP.source}):)?(${BASE64_REGEXP.source}):(${BASE64_REGEXP.source})$`
|
|
54
|
-
);
|
|
55
|
-
function getInputSchemaSecretFieldKeys(inputSchema) {
|
|
56
|
-
return Object.keys(inputSchema.properties).filter((key) => !!inputSchema.properties[key].isSecret);
|
|
57
|
-
}
|
|
58
|
-
__name(getInputSchemaSecretFieldKeys, "getInputSchemaSecretFieldKeys");
|
|
59
|
-
function encryptInputSecretValue({
|
|
60
|
-
value,
|
|
61
|
-
publicKey,
|
|
62
|
-
schema
|
|
63
|
-
}) {
|
|
64
|
-
ow(value, ow.any(ow.string, ow.object));
|
|
65
|
-
ow(publicKey, ow.object.instanceOf(KeyObject));
|
|
66
|
-
ow(schema, ow.optional.object);
|
|
67
|
-
if (typeof value === "string") {
|
|
68
|
-
const { encryptedValue: encryptedValue2, encryptedPassword: encryptedPassword2 } = publicEncrypt({ value, publicKey });
|
|
69
|
-
return `${ENCRYPTED_STRING_VALUE_PREFIX}:${encryptedPassword2}:${encryptedValue2}`;
|
|
70
|
-
}
|
|
71
|
-
const schemaHash = schema ? getFieldSchemaHash(schema) : null;
|
|
72
|
-
let valueStr;
|
|
73
|
-
try {
|
|
74
|
-
valueStr = JSON.stringify(value);
|
|
75
|
-
} catch (err) {
|
|
76
|
-
throw new Error(`The input value could not be stringified for encryption: ${err}`);
|
|
77
|
-
}
|
|
78
|
-
const { encryptedValue, encryptedPassword } = publicEncrypt({ value: valueStr, publicKey });
|
|
79
|
-
return `${ENCRYPTED_JSON_VALUE_PREFIX}:${schemaHash ? `${schemaHash}:` : ""}${encryptedPassword}:${encryptedValue}`;
|
|
80
|
-
}
|
|
81
|
-
__name(encryptInputSecretValue, "encryptInputSecretValue");
|
|
82
|
-
function isEncryptedValueForFieldType(value, fieldType) {
|
|
83
|
-
ow(value, ow.string);
|
|
84
|
-
ow(fieldType, ow.string.oneOf(["string", "object", "array"]));
|
|
85
|
-
const match = value.match(ENCRYPTED_VALUE_REGEXP);
|
|
86
|
-
if (!match) return false;
|
|
87
|
-
const [, prefix] = match;
|
|
88
|
-
if (prefix === ENCRYPTED_STRING_VALUE_PREFIX && fieldType !== "string") return false;
|
|
89
|
-
return true;
|
|
90
|
-
}
|
|
91
|
-
__name(isEncryptedValueForFieldType, "isEncryptedValueForFieldType");
|
|
92
|
-
function isEncryptedValueForFieldSchema(value, fieldSchema) {
|
|
93
|
-
ow(value, ow.string);
|
|
94
|
-
ow(fieldSchema, ow.object);
|
|
95
|
-
const match = value.match(ENCRYPTED_VALUE_REGEXP);
|
|
96
|
-
if (!match) return false;
|
|
97
|
-
const [, prefix, schemaHash] = match;
|
|
98
|
-
if (prefix !== ENCRYPTED_STRING_VALUE_PREFIX && prefix !== ENCRYPTED_JSON_VALUE_PREFIX) return false;
|
|
99
|
-
if (schemaHash) {
|
|
100
|
-
const fieldSchemaHash = getFieldSchemaHash(fieldSchema);
|
|
101
|
-
return schemaHash === fieldSchemaHash;
|
|
102
|
-
}
|
|
103
|
-
return true;
|
|
104
|
-
}
|
|
105
|
-
__name(isEncryptedValueForFieldSchema, "isEncryptedValueForFieldSchema");
|
|
106
|
-
function encryptInputSecrets({
|
|
107
|
-
input,
|
|
108
|
-
inputSchema,
|
|
109
|
-
publicKey
|
|
110
|
-
}) {
|
|
111
|
-
ow(input, ow.object);
|
|
112
|
-
ow(inputSchema, ow.object);
|
|
113
|
-
ow(publicKey, ow.object.instanceOf(KeyObject));
|
|
114
|
-
const secretsInInputKeys = getInputSchemaSecretFieldKeys(inputSchema);
|
|
115
|
-
if (secretsInInputKeys.length === 0) return input;
|
|
116
|
-
const encryptedInput = {};
|
|
117
|
-
for (const key of secretsInInputKeys) {
|
|
118
|
-
const value = input[key];
|
|
119
|
-
if (value && !(ow.isValid(value, ow.string) && ENCRYPTED_VALUE_REGEXP.test(value))) {
|
|
120
|
-
try {
|
|
121
|
-
encryptedInput[key] = encryptInputSecretValue({
|
|
122
|
-
value: input[key],
|
|
123
|
-
publicKey,
|
|
124
|
-
schema: inputSchema.properties[key]
|
|
125
|
-
});
|
|
126
|
-
} catch (err) {
|
|
127
|
-
throw new Error(
|
|
128
|
-
`The input field "${key}" could not be encrypted. Try updating the field's value in the input editor. Encryption error: ${err}`
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
return { ...input, ...encryptedInput };
|
|
134
|
-
}
|
|
135
|
-
__name(encryptInputSecrets, "encryptInputSecrets");
|
|
136
|
-
function decryptInputSecrets({ input, privateKey }) {
|
|
137
|
-
ow(input, ow.object);
|
|
138
|
-
ow(privateKey, ow.object.instanceOf(KeyObject));
|
|
139
|
-
const decryptedInput = {};
|
|
140
|
-
for (const [key, value] of Object.entries(input)) {
|
|
141
|
-
if (typeof value === "string" && ENCRYPTED_VALUE_REGEXP.test(value)) {
|
|
142
|
-
const match = value.match(ENCRYPTED_VALUE_REGEXP);
|
|
143
|
-
if (!match) continue;
|
|
144
|
-
const [, prefix, , encryptedPassword, encryptedValue] = match;
|
|
145
|
-
try {
|
|
146
|
-
const decryptedValue = privateDecrypt({ privateKey, encryptedPassword, encryptedValue });
|
|
147
|
-
if (prefix === ENCRYPTED_STRING_VALUE_PREFIX) {
|
|
148
|
-
decryptedInput[key] = decryptedValue;
|
|
149
|
-
} else if (prefix === ENCRYPTED_JSON_VALUE_PREFIX) {
|
|
150
|
-
decryptedInput[key] = JSON.parse(decryptedValue);
|
|
151
|
-
}
|
|
152
|
-
} catch (err) {
|
|
153
|
-
throw new Error(
|
|
154
|
-
`The input field "${key}" could not be decrypted. Try updating the field's value in the input editor. Decryption error: ${err}`
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
return { ...input, ...decryptedInput };
|
|
160
|
-
}
|
|
161
|
-
__name(decryptInputSecrets, "decryptInputSecrets");
|
|
162
|
-
export {
|
|
163
|
-
decryptInputSecrets,
|
|
164
|
-
encryptInputSecretValue,
|
|
165
|
-
encryptInputSecrets,
|
|
166
|
-
getInputSchemaSecretFieldKeys,
|
|
167
|
-
isEncryptedValueForFieldSchema,
|
|
168
|
-
isEncryptedValueForFieldType
|
|
169
|
-
};
|
|
170
|
-
//# sourceMappingURL=index.mjs.map
|
package/esm/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/input_secrets.ts","../../src/field_schema_utils.ts"],"sourcesContent":["import { KeyObject } from 'node:crypto';\n\nimport _testOw, { type Ow } from 'ow';\n\nimport { privateDecrypt, publicEncrypt } from '@apify/utilities';\n\nimport { getFieldSchemaHash } from './field_schema_utils';\n\n// eslint-disable-next-line no-underscore-dangle\ndeclare const __injectedOw: Ow;\n\nconst ow: Ow = typeof __injectedOw === 'undefined' ? _testOw : __injectedOw || _testOw;\n\nconst BASE64_REGEXP = /[-A-Za-z0-9+/]*={0,3}/;\n\n// The encrypted value has a prefix, optional schema hash, encrypted password and encrypted value.\n// - The prefix tells if the value is a string or a JSON object and needs to be parsed back after decryption.\n// - The schema hash is optional and is used to verify if the schema has changed since the value was encrypted.\n// - The encrypted password is used to decrypt the value.\n// - The encrypted value is the actual encrypted data.\n\n// used for backward compatibility with old encrypted string values\nconst ENCRYPTED_STRING_VALUE_PREFIX = 'ENCRYPTED_VALUE';\n// we use this for all types of encrypted values (string, object, array)\nconst ENCRYPTED_JSON_VALUE_PREFIX = 'ENCRYPTED_JSON';\n\n// All encrypted values must match this regular expression.\nconst ENCRYPTED_VALUE_REGEXP = new RegExp(\n `^(${ENCRYPTED_STRING_VALUE_PREFIX}|${ENCRYPTED_JSON_VALUE_PREFIX}):(?:(${BASE64_REGEXP.source}):)?(${BASE64_REGEXP.source}):(${BASE64_REGEXP.source})$`,\n);\n\n/**\n * Get keys of secret fields from input schema\n */\nexport function getInputSchemaSecretFieldKeys(inputSchema: any): string[] {\n return Object.keys(inputSchema.properties).filter((key) => !!inputSchema.properties[key].isSecret);\n}\n\n/**\n * Encrypts input secret value\n * Depending on the type of value, it returns either a string (for strings) or an object (for objects) with the `secret` key.\n */\nexport function encryptInputSecretValue<T extends string | object>({\n value,\n publicKey,\n schema,\n}: {\n value: T;\n publicKey: KeyObject;\n schema?: Record<string, any>;\n}): string {\n ow(value, ow.any(ow.string, ow.object));\n ow(publicKey, ow.object.instanceOf(KeyObject));\n ow(schema, ow.optional.object);\n\n // TODO to make string encryption compatible with current SDK, we need to use the old form.\n // Remove this once SDK is updated to use the new form\n if (typeof value === 'string') {\n const { encryptedValue, encryptedPassword } = publicEncrypt({ value, publicKey });\n return `${ENCRYPTED_STRING_VALUE_PREFIX}:${encryptedPassword}:${encryptedValue}`;\n }\n\n const schemaHash = schema ? getFieldSchemaHash(schema) : null;\n\n // We are encrypting the value as a JSON string, so we need to stringify it first.\n let valueStr: string;\n try {\n valueStr = JSON.stringify(value);\n } catch (err) {\n throw new Error(`The input value could not be stringified for encryption: ${err}`);\n }\n\n const { encryptedValue, encryptedPassword } = publicEncrypt({ value: valueStr, publicKey });\n return `${ENCRYPTED_JSON_VALUE_PREFIX}:${schemaHash ? `${schemaHash}:` : ''}${encryptedPassword}:${encryptedValue}`;\n}\n\n/**\n * Checks if the value is an encrypted value for a specific field type.\n * It validates the string value against the regular expression and checks the prefix.\n * @param value - encrypted value to check\n * @param fieldType - type of the field, can be 'string', 'object' or 'array'\n */\nexport function isEncryptedValueForFieldType(value: string, fieldType: 'string' | 'object' | 'array') {\n ow(value, ow.string);\n ow(fieldType, ow.string.oneOf(['string', 'object', 'array']));\n\n const match = value.match(ENCRYPTED_VALUE_REGEXP);\n if (!match) return false;\n\n const [, prefix] = match;\n\n // For backward compatibility, we allow the old prefix only for string values.\n if (prefix === ENCRYPTED_STRING_VALUE_PREFIX && fieldType !== 'string') return false;\n\n return true;\n}\n\n/**\n * Checks if the value is an encrypted value for a specific field schema.\n * It validates the string value against the regular expression and checks the schema hash in\n * the encrypted value against the hash of the field schema.\n * @param value - encrypted value to check\n * @param fieldSchema - schema of the field, used to get the hash\n */\nexport function isEncryptedValueForFieldSchema(value: string, fieldSchema: Record<string, any>) {\n ow(value, ow.string);\n ow(fieldSchema, ow.object);\n\n const match = value.match(ENCRYPTED_VALUE_REGEXP);\n if (!match) return false;\n\n const [, prefix, schemaHash] = match;\n\n if (prefix !== ENCRYPTED_STRING_VALUE_PREFIX && prefix !== ENCRYPTED_JSON_VALUE_PREFIX) return false;\n\n if (schemaHash) {\n const fieldSchemaHash = getFieldSchemaHash(fieldSchema);\n return schemaHash === fieldSchemaHash;\n }\n\n return true;\n}\n\n/**\n * Encrypts actor input secrets\n */\nexport function encryptInputSecrets<T extends Record<string, any>>({\n input,\n inputSchema,\n publicKey,\n}: {\n input: T;\n inputSchema: object;\n publicKey: KeyObject;\n}): T {\n ow(input, ow.object);\n ow(inputSchema, ow.object);\n ow(publicKey, ow.object.instanceOf(KeyObject));\n\n const secretsInInputKeys = getInputSchemaSecretFieldKeys(inputSchema);\n if (secretsInInputKeys.length === 0) return input;\n\n const encryptedInput = {} as Record<string, any>;\n for (const key of secretsInInputKeys) {\n const value = input[key];\n // NOTE: Skips already encrypted values. It can happens in case client already encrypted values, before\n // sending them using API. Or input was takes from task, run console or scheduler, where input is stored encrypted.\n if (value && !(ow.isValid(value, ow.string) && ENCRYPTED_VALUE_REGEXP.test(value))) {\n try {\n encryptedInput[key] = encryptInputSecretValue({\n value: input[key],\n publicKey,\n schema: (inputSchema as any).properties[key],\n });\n } catch (err) {\n throw new Error(\n `The input field \"${key}\" could not be encrypted. Try updating the field's value in the input editor. ` +\n `Encryption error: ${err}`,\n );\n }\n }\n }\n\n return { ...input, ...encryptedInput };\n}\n\n/**\n * Decrypts actor input secrets\n * @param input - actor input object that may contain encrypted secret values\n * @param privateKey - private key used to decrypt the secret values\n */\nexport function decryptInputSecrets<T>({ input, privateKey }: { input: T; privateKey: KeyObject }): T {\n ow(input, ow.object);\n ow(privateKey, ow.object.instanceOf(KeyObject));\n\n const decryptedInput = {} as Record<string, any>;\n for (const [key, value] of Object.entries(input)) {\n if (typeof value === 'string' && ENCRYPTED_VALUE_REGEXP.test(value)) {\n const match = value.match(ENCRYPTED_VALUE_REGEXP);\n if (!match) continue;\n const [, prefix, , encryptedPassword, encryptedValue] = match;\n try {\n const decryptedValue = privateDecrypt({ privateKey, encryptedPassword, encryptedValue });\n\n if (prefix === ENCRYPTED_STRING_VALUE_PREFIX) {\n decryptedInput[key] = decryptedValue;\n } else if (prefix === ENCRYPTED_JSON_VALUE_PREFIX) {\n // For JSON values, we need to parse the decrypted string into an object.\n decryptedInput[key] = JSON.parse(decryptedValue);\n }\n } catch (err) {\n throw new Error(\n `The input field \"${key}\" could not be decrypted. Try updating the field's value in the input editor. ` +\n `Decryption error: ${err}`,\n );\n }\n }\n }\n\n return { ...input, ...decryptedInput };\n}\n","import crypto from 'node:crypto';\n\n/**\n * These keys are omitted from the field schema normalization process\n * because they are not relevant for validation of values against the schema.\n */\nconst OMIT_KEYS = new Set([\n 'title',\n 'description',\n 'sectionCaption',\n 'sectionDescription',\n 'nullable',\n 'example',\n 'prefill',\n 'editor',\n]);\n\n/**\n * Normalizes the field schema by removing irrelevant keys and sorting the remaining keys.\n */\nfunction normalizeFieldSchema(value: any): any {\n if (Array.isArray(value)) {\n return value.map(normalizeFieldSchema);\n }\n\n if (value && typeof value === 'object') {\n const result: Record<string, any> = {};\n Object.keys(value)\n .filter((key) => !OMIT_KEYS.has(key))\n .sort()\n .forEach((key) => {\n result[key] = normalizeFieldSchema(value[key]);\n });\n return result;\n }\n\n return value;\n}\n\n/**\n * Generates a stable hash for the field schema.\n * @param fieldSchema\n */\nexport function getFieldSchemaHash(fieldSchema: Record<string, any>): string {\n try {\n const stringifiedSchema = JSON.stringify(normalizeFieldSchema(fieldSchema));\n // Create a SHA-256 hash of the stringified schema and return the first 10 characters in hex.\n return crypto.createHash('sha256').update(stringifiedSchema).digest('hex').slice(0, 10);\n } catch (err) {\n throw new Error(`The field schema could not be stringified for hash: ${err}`);\n }\n}\n"],"mappings":";;;;;;AAAA,SAAS,iBAAiB;AAE1B,OAAO,aAA0B;AAEjC,SAAS,gBAAgB,qBAAqB;;;ACJ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