(pattern, flags, input)
| 21 | |
| 22 | // IMPORTANT: This function must remain synchronous. See vmContext comment above. |
| 23 | function safeRegexTest(pattern, flags, input) { |
| 24 | try { |
| 25 | if (!regexTimeout) { |
| 26 | var re = new RegExp(pattern, flags); |
| 27 | return re.test(input); |
| 28 | } |
| 29 | var cacheKey = flags + ':' + pattern; |
| 30 | var script = scriptCache.get(cacheKey); |
| 31 | if (!script) { |
| 32 | if (scriptCache.size >= SCRIPT_CACHE_MAX) { scriptCache.clear(); } |
| 33 | script = new vm.Script('new RegExp(pattern, flags).test(input)'); |
| 34 | scriptCache.set(cacheKey, script); |
| 35 | } |
| 36 | vmContext.pattern = pattern; |
| 37 | vmContext.flags = flags; |
| 38 | vmContext.input = input; |
| 39 | return script.runInContext(vmContext, { timeout: regexTimeout }); |
| 40 | } catch (e) { |
| 41 | if (e.code === 'ERR_SCRIPT_EXECUTION_TIMEOUT') { |
| 42 | logger.warn(`Regex timeout: pattern "${pattern}" with flags "${flags}" exceeded ${regexTimeout}ms limit`); |
| 43 | } else { |
| 44 | logger.warn(`Invalid regex: pattern "${pattern}" with flags "${flags}": ${e.message}`); |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Query Hashes are deterministic hashes for Parse Queries. |
no test coverage detected