Skip to content

feat(input_schema): Enable secret objects #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/input_schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
},
"dependencies": {
"@apify/consts": "^2.43.0",
"@apify/input_secrets": "^1.1.76",
"acorn-loose": "^8.4.0",
"countries-list": "^3.0.0"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/input_schema/src/intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const intlStrings = {
'Field schema.properties.{fieldKey} does not exist, but it is specified in schema.required. Either define the field or remove it from schema.required.',
'inputSchema.validation.proxyGroupMustBeArrayOfStrings':
'Field {rootName}.{fieldKey}.apifyProxyGroups must be an array of strings.',
'inputSchema.validation.secretFieldSchemaChanged':
'The field schema.properties.{fieldKey} is a secret field, but its schema has changed. Please update the value in the input editor.',
};

/**
Expand Down
175 changes: 123 additions & 52 deletions packages/input_schema/src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@
"title": { "type": "string" },
"description": { "type": "string" },
"example": { "type": "string" },
"pattern": { "type": "string" },
"nullable": { "type": "boolean" },
"minLength": { "type": "integer" },
"maxLength": { "type": "integer" },
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can now validate these for secret strings too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure here - wouldn't we end up applying the validation to the encrypted value in this case? (That would not be useful)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I removed these three properties for now. We can add it later but would need to catch the Ajv errors

"editor": { "enum": ["textfield", "textarea", "hidden"] },
"isSecret": { "enum": [true] },
"sectionCaption": { "type": "string" },
Expand All @@ -166,96 +169,164 @@
"type": "object",
"properties": {
"type": { "enum": ["array"] },
"editor": { "enum": ["json", "requestListSources", "pseudoUrls", "globs", "keyValue", "stringList", "select", "hidden"] }
"editor": { "enum": ["json", "requestListSources", "pseudoUrls", "globs", "keyValue", "stringList", "select", "hidden"] },
"isSecret": { "type": "boolean" }
},
"additionalProperties": true,
"required": ["type", "title", "description", "editor"],
"if": {
"properties": {
"editor": { "const": "select" }
"isSecret": {
"not": {
"const": true
}
}
}
},
"then": {
"additionalProperties": false,
"required": ["items"],
"properties": {
"type": { "enum": ["array"] },
"editor": { "enum": ["select"] },
"title": { "type": "string" },
"description": { "type": "string" },
"default": { "type": "array" },
"prefill": { "type": "array" },
"example": { "type": "array" },
"nullable": { "type": "boolean" },
"minItems": { "type": "integer" },
"maxItems": { "type": "integer" },
"uniqueItems": { "type": "boolean" },
"sectionCaption": { "type": "string" },
"sectionDescription": { "type": "string" },
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"type": { "enum": ["string"] },
"enum": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true
"if": {
"properties": {
"editor": { "const": "select" }
}
},
"then": {
"additionalProperties": false,
"required": ["items"],
"properties": {
"type": { "enum": ["array"] },
"editor": { "enum": ["select"] },
"title": { "type": "string" },
"description": { "type": "string" },
"default": { "type": "array" },
"prefill": { "type": "array" },
"example": { "type": "array" },
"nullable": { "type": "boolean" },
"minItems": { "type": "integer" },
"maxItems": { "type": "integer" },
"uniqueItems": { "type": "boolean" },
"sectionCaption": { "type": "string" },
"sectionDescription": { "type": "string" },
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"type": { "enum": ["string"] },
"enum": {
"type": "array",
"items": { "type": "string" },
"uniqueItems": true
},
"enumTitles": {
"type": "array",
"items": { "type": "string" }
}
},
"enumTitles": {
"type": "array",
"items": { "type": "string" }
}
"required": ["type", "enum"]
},
"required": ["type", "enum"]
"isSecret": { "enum": [false] }
}
},
"else": {
"additionalProperties": false,
"properties": {
"type": { "enum": ["array"] },
"editor": { "enum": ["json", "requestListSources", "pseudoUrls", "globs", "keyValue", "stringList", "hidden"] },
"title": { "type": "string" },
"description": { "type": "string" },
"default": { "type": "array" },
"prefill": { "type": "array" },
"example": { "type": "array" },
"nullable": { "type": "boolean" },
"minItems": { "type": "integer" },
"maxItems": { "type": "integer" },
"uniqueItems": { "type": "boolean" },
"sectionCaption": { "type": "string" },
"sectionDescription": { "type": "string" },
"placeholderKey": { "type": "string" },
"placeholderValue": { "type": "string" },
"patternKey": { "type": "string" },
"patternValue": { "type": "string" },
"isSecret": { "enum": [false] }
}
}
},
"else": {
"additionalProperties": false,
"properties": {
"type": { "enum": ["array"] },
"editor": { "enum": ["json", "requestListSources", "pseudoUrls", "globs", "keyValue", "stringList", "hidden"] },
"editor": { "enum": ["json", "hidden"] },
"title": { "type": "string" },
"description": { "type": "string" },
"default": { "type": "array" },
"prefill": { "type": "array" },
"example": { "type": "array" },
"nullable": { "type": "boolean" },
"minItems": { "type": "integer" },
"maxItems": { "type": "integer" },
"uniqueItems": { "type": "boolean" },
"sectionCaption": { "type": "string" },
"sectionDescription": { "type": "string" },
"placeholderKey": { "type": "string" },
"placeholderValue": { "type": "string" },
"patternKey": { "type": "string" },
"patternValue": { "type": "string" }
"isSecret": { "enum": [true] }
}
}
},
"objectProperty": {
"title": "Object property",
"type": "object",
"additionalProperties": false,
"additionalProperties": true,
"properties": {
"type": { "enum": ["object"] },
"title": { "type": "string" },
"description": { "type": "string" },
"default": { "type": "object" },
"prefill": { "type": "object" },
"example": { "type": "object" },
"patternKey": { "type": "string" },
"patternValue": { "type": "string" },
"nullable": { "type": "boolean" },
"minProperties": { "type": "integer" },
"maxProperties": { "type": "integer" },

"editor": { "enum": ["json", "proxy", "hidden"] },
"sectionCaption": { "type": "string" },
"sectionDescription": { "type": "string" }
"isSecret": { "type": "boolean" }
},
"required": ["type", "title", "description", "editor"]
"required": ["type", "title", "description", "editor"],
"if": {
"properties": {
"isSecret": {
"not": {
"const": true
}
}
}
},
"then": {
"additionalProperties": false,
"properties": {
"type": { "enum": ["object"] },
"title": { "type": "string" },
"description": { "type": "string" },
"default": { "type": "object" },
"prefill": { "type": "object" },
"example": { "type": "object" },
"patternKey": { "type": "string" },
"patternValue": { "type": "string" },
"nullable": { "type": "boolean" },
"minProperties": { "type": "integer" },
"maxProperties": { "type": "integer" },
"editor": { "enum": ["json", "proxy", "hidden"] },
"sectionCaption": { "type": "string" },
"sectionDescription": { "type": "string" },
"isSecret": { "enum": [false] }
}
},
"else": {
"additionalProperties": false,
"properties": {
"type": { "enum": ["object"] },
"title": { "type": "string" },
"description": { "type": "string" },
"example": { "type": "object" },
"patternKey": { "type": "string" },
"patternValue": { "type": "string" },
"nullable": { "type": "boolean" },
"minProperties": { "type": "integer" },
"maxProperties": { "type": "integer" },
"editor": { "enum": ["json", "hidden"] },
"sectionCaption": { "type": "string" },
"sectionDescription": { "type": "string" },
"isSecret": { "enum": [true] }
}
}
},
"integerProperty": {
"title": "Integer property",
Expand Down
36 changes: 34 additions & 2 deletions packages/input_schema/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { ValidateFunction } from 'ajv';
import { countries } from 'countries-list';

import { PROXY_URL_REGEX, URL_REGEX } from '@apify/consts';
import { isEncryptedValueForFieldSchema, isEncryptedValueForFieldType } from '@apify/input_secrets';

import { parseAjvError } from './input_schema';
import { m } from './intl';
Expand Down Expand Up @@ -133,13 +134,34 @@ export function validateInputUsingValidator(
// Process AJV validation errors
if (!isValid) {
errors = validator.errors!
.filter((error) => {
// We are storing encrypted objects/arrays as strings, so AJV will throw type the error here.
// So we need to skip these errors.
if (error.keyword === 'type' && error.instancePath) {
const path = error.instancePath.replace(/^\//, '').split('/')[0];
const propSchema = inputSchema.properties?.[path];
const value = input[path];

// Check if the property is a secret and if the value is an encrypted value.
// We do additional validation of the field schema in the later part of this function
if (
propSchema?.isSecret
&& typeof value === 'string'
&& (propSchema.type === 'object' || propSchema.type === 'array')
&& isEncryptedValueForFieldType(value, propSchema.type)
) {
return false;
}
}
return true;
})
Comment on lines 136 to +157
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ignores type errors produced by Ajv validation when secret string is passed to object/array fields.

.map((error) => parseAjvError(error, 'input', properties, input))
.filter((error) => !!error) as any[];
}

Object.keys(properties).forEach((property) => {
const value = input[property];
const { type, editor, patternKey, patternValue } = properties[property];
const { type, editor, patternKey, patternValue, isSecret } = properties[property];
const fieldErrors = [];
// Check that proxy is required, if yes, valides that it's correctly setup
if (type === 'object' && editor === 'proxy') {
Expand Down Expand Up @@ -215,7 +237,7 @@ export function validateInputUsingValidator(
}
}
// Check that object items fit patternKey and patternValue
if (type === 'object' && value) {
if (type === 'object' && value && typeof value === 'object') {
if (patternKey) {
const check = new RegExp(patternKey);
const invalidKeys: any[] = [];
Expand Down Expand Up @@ -249,6 +271,16 @@ export function validateInputUsingValidator(
}
}

// Additional validation for secret fields
if (isSecret && value && typeof value === 'string') {
// If the value is a valid encrypted string for the field type,
// we check if the field schema is likely to be still valid (is unchanged from the time of encryption).
if (isEncryptedValueForFieldType(value, type) && !isEncryptedValueForFieldSchema(value, properties[property])) {
// If not, we add an error message to the field errors and user needs to update the value in the input editor.
fieldErrors.push(m('inputSchema.validation.secretFieldSchemaChanged', { rootName: 'input', fieldKey: property }));
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we store field schema's hash to encrypted value, here we validate that the field schema (ignoring properties like title, description,..) doesn't change from the time of encryption.

if (fieldErrors.length > 0) {
const message = fieldErrors.join(', ');
errors.push({ fieldKey: property, message });
Expand Down
42 changes: 42 additions & 0 deletions packages/input_secrets/src/field_schema_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import crypto from 'node:crypto';

/**
* These keys are omitted from the field schema normalization process
* because they are not relevant for validation of values against the schema.
*/
const OMIT_KEYS = new Set(['title', 'description', 'sectionCaption', 'sectionDescription', 'nullable', 'example', 'editor']);

/**
* Normalizes the field schema by removing irrelevant keys and sorting the remaining keys.
*/
function normalizeFieldSchema(value: any): any {
if (Array.isArray(value)) {
return value.map(normalizeFieldSchema);
}

if (value && typeof value === 'object') {
const result: Record<string, any> = {};
Object.keys(value)
.filter((key) => !OMIT_KEYS.has(key))
.sort()
.forEach((key) => {
result[key] = normalizeFieldSchema(value[key]);
});
return result;
}

return value;
}

/**
* Generates a stable hash for the field schema.
* @param fieldSchema
*/
export function getFieldSchemaHash(fieldSchema: Record<string, any>): string {
try {
const stringifiedSchema = JSON.stringify(normalizeFieldSchema(fieldSchema));
return crypto.createHash('sha256').update(stringifiedSchema).digest('base64');
} catch (err) {
throw new Error(`The field schema could not be stringified for hash: ${err}`);
}
}
Loading
Loading