Skip to content

Commit 4a92fcc

Browse files
authored
fix(input_schema): Remove safe regex check (#558)
The used package for malicious regex detection (`safe-regex`) produces lot of false-positives, so this PR now removes the check completely and I will figure out what next later. Reported here: https://apify.slack.com/archives/C0L33UM7Z/p1761580328353969
1 parent 4009e38 commit 4a92fcc

File tree

4 files changed

+9
-71
lines changed

4 files changed

+9
-71
lines changed

package-lock.json

Lines changed: 1 addition & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/input_schema/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@
5252
"@apify/input_secrets": "^1.2.11",
5353
"@apify/json_schemas": "^0.7.0",
5454
"acorn-loose": "^8.4.0",
55-
"countries-list": "^3.0.0",
56-
"safe-regex": "^2.1.1"
55+
"countries-list": "^3.0.0"
5756
},
5857
"peerDependencies": {
5958
"ajv": "^8.0.0"

packages/input_schema/src/utilities.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { parse } from 'acorn-loose';
22
import type { ValidateFunction } from 'ajv';
33
import type Ajv from 'ajv/dist/2019';
44
import { countries } from 'countries-list';
5-
import safe from 'safe-regex';
65

76
import { PROXY_URL_REGEX, URL_REGEX } from '@apify/consts';
87
import { isEncryptedValueForFieldSchema, isEncryptedValueForFieldType } from '@apify/input_secrets';
@@ -369,19 +368,19 @@ export function ensureAjvSupportsDraft2019(ajvInstance: Ajv) {
369368
* @param fieldKey The field key where the pattern is used (for error messages).
370369
*/
371370
export function validateRegexpPattern(pattern: string, fieldKey: string) {
372-
let regex: RegExp;
373-
374371
try {
375372
// Validate that the pattern is a valid regular expression
376-
regex = new RegExp(pattern);
373+
// eslint-disable-next-line
374+
new RegExp(pattern);
377375
} catch {
378376
const message = m('inputSchema.validation.regexpNotValid', { pattern, fieldKey });
379377
throw new Error(`Input schema is not valid (${message})`);
380378
}
381379

380+
// TODO: add check for safe regex but figure out how to avoid false positives with some valid regexes
382381
// Check if the regex is safe (to avoid ReDoS attacks)
383-
if (!safe(regex)) {
384-
const message = m('inputSchema.validation.regexpNotSafe', { pattern, fieldKey });
385-
throw new Error(`Input schema is not valid (${message})`);
386-
}
382+
// if (!safe(regex)) {
383+
// const message = m('inputSchema.validation.regexpNotSafe', { pattern, fieldKey });
384+
// throw new Error(`Input schema is not valid (${message})`);
385+
// }
387386
}

test/input_schema.test.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,49 +1066,6 @@ describe('input_schema.json', () => {
10661066
'Input schema is not valid (The regular expression "^[0-9+$" in field schema.properties.objectField.patternValue must be valid.)',
10671067
);
10681068
});
1069-
1070-
it('should throw error on not safe regexp', () => {
1071-
const invalidRegexps = [
1072-
'(a+)+$',
1073-
'^(a|a?)+$',
1074-
'^(a|a*)+$',
1075-
'^(a|a+)+$',
1076-
'^(a?)+$',
1077-
'^(a*)+$',
1078-
'^(a+)*$',
1079-
'^(a|aa?)+$',
1080-
'^(a|aa*)+$',
1081-
'^(a|a+)*$',
1082-
'^(a|a?)*$',
1083-
'^(a|a*)*$',
1084-
'^(a?)*$',
1085-
'^(a*)*$',
1086-
'^(a+)?$',
1087-
'^(a*)?$',
1088-
'a*b*c*d*e*f*g*h*i*j*k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z*',
1089-
];
1090-
1091-
for (const pattern of invalidRegexps) {
1092-
const schema = {
1093-
title: 'Test input schema',
1094-
type: 'object',
1095-
schemaVersion: 1,
1096-
properties: {
1097-
myField: {
1098-
title: 'Field title',
1099-
type: 'string',
1100-
description: 'Some description ...',
1101-
editor: 'textfield',
1102-
pattern,
1103-
},
1104-
},
1105-
};
1106-
1107-
expect(() => validateInputSchema(validator, schema)).toThrow(
1108-
`Input schema is not valid (The regular expression "${pattern}" in field schema.properties.myField.pattern may cause excessive backtracking or be unsafe to execute.)`,
1109-
);
1110-
}
1111-
});
11121069
});
11131070
});
11141071
});

0 commit comments

Comments
 (0)