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 all 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
173 changes: 122 additions & 51 deletions packages/input_schema/src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
"type": { "enum": ["string"] },
"title": { "type": "string" },
"description": { "type": "string" },
"prefill": { "type": "string" },
"example": { "type": "string" },
"nullable": { "type": "boolean" },
"editor": { "enum": ["textfield", "textarea", "hidden"] },
Expand All @@ -166,59 +167,94 @@
"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" },
Expand All @@ -227,35 +263,70 @@
"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" },
"prefill": { "type": "object" },
"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', { fieldKey: property }));
}
Comment on lines +276 to +281
Copy link
Member

Choose a reason for hiding this comment

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

I have a slight concern here, but I'm not sure how big of a problem it will really be.

If you have e.g. an array secret field, and you increase its maxItems, the encrypted field value will still be semantically valid, but this validation will start throwing errors, which will be annoying. You'll have to hunt for all the passwords / secret keys you put in the field before, some of which might not be available anymore (since some secrets are only shown once after generation).

I'm not sure what changes to the input schema happen more often, but I would wager it's backwards compatible ones, so we'll be forcing people to update their inputs even when not necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, maybe showing this only as a warning in the UI would be better?

Ajv does not have anything like warning, but we can updated the validateInputUsingValidator function to return list of errors and list of warnings and show this warnings in the UI.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can keep it it as an error for now, see how many of these validation errors are happening, and possibly change it to a warning later?

If we put it as a warning now, but decide to change it to an error later, that would be a breaking change.

}

if (fieldErrors.length > 0) {
const message = fieldErrors.join(', ');
errors.push({ fieldKey: property, message });
Expand Down
43 changes: 43 additions & 0 deletions packages/input_secrets/src/field_schema_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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', 'prefill', '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));
// Create a SHA-256 hash of the stringified schema and return the first 10 characters in hex.
return crypto.createHash('sha256').update(stringifiedSchema).digest('hex').slice(0, 10);
} catch (err) {
throw new Error(`The field schema could not be stringified for hash: ${err}`);
}
}
Loading