fix: add invalid regex validation for commitConfig#1355
fix: add invalid regex validation for commitConfig#1355jescalada wants to merge 19 commits intofinos:mainfrom
commitConfig#1355Conversation
✅ Deploy Preview for endearing-brigadeiros-63f9d0 canceled.
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1355 +/- ##
==========================================
+ Coverage 81.25% 81.51% +0.26%
==========================================
Files 65 66 +1
Lines 4657 4713 +56
Branches 792 814 +22
==========================================
+ Hits 3784 3842 +58
+ Misses 858 856 -2
Partials 15 15 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…stead of exiting process
kriswest
left a comment
There was a problem hiding this comment.
Possibly a double call to loadFullConfiguration that needs looking at
kriswest
left a comment
There was a problem hiding this comment.
This generally LGTM.
However, I can't help thinking about the fact we have multiple types of validation in different places (validation via the schema and quicktype during load, then this later). A slightly deeper refactor could combine those steps, parsing individual configs from strings using quicktype then applying other validators - resulting in a n easier to follow and maintain codebase..
I'm ok with merging in the current form and leaving such a refactor for later if you want to - hence approving.
src/config/validators.ts
Outdated
| try { | ||
| new RegExp(config.commitConfig.author.email.local.block); | ||
| } catch (error: unknown) { | ||
| console.error( | ||
| `Invalid regular expression for commitConfig.author.email.local.block: ${config.commitConfig.author.email.local.block}`, | ||
| ); | ||
| return false; | ||
| } |
There was a problem hiding this comment.
This could be extracted to a helper function validateConfigRegex(config: GitProxyConfig, path:string) extracts the value (checking existence), checks if its an array, validates and logs. That would reduce repeated code.
There was a problem hiding this comment.
I tried doing this previously - had a bunch of failing tests so I gave up on it. Will take another look now with fresh eyes 🙂
There was a problem hiding this comment.
Managed to extract a few functions out of it - potentially useful for extending commitConfig checks or other regex validation down the line:
/**
* Validates that a regular expression is valid.
* @param pattern The regular expression to validate
* @param context The context of the regular expression
* @returns true if the regular expression is valid, false otherwise
*/
function isValidRegex(pattern: string, context: string): boolean {
try {
new RegExp(pattern);
return true;
} catch {
console.error(`Invalid regular expression for ${context}: ${pattern}`);
return false;
}
}
/**
* Validates that a value in the configuration is a valid regular expression.
* @param config The configuration to validate
* @param path The path to the value to validate
* @returns true if the value is a valid regular expression, false otherwise
*/
function validateConfigRegex(config: GitProxyConfig, path: string): boolean {
const getValueAtPath = (obj: unknown, path: string): unknown => {
return path.split('.').reduce((current, key) => {
if (current == null || typeof current !== 'object') {
return undefined;
}
return (current as Record<string, unknown>)[key];
}, obj);
};
const value = getValueAtPath(config, path);
if (!value) return true;
if (typeof value === 'string') {
return isValidRegex(value, path);
}
if (Array.isArray(value)) {
for (const pattern of value) {
if (!isValidRegex(pattern, path)) return false;
}
return true;
}
if (typeof value === 'object') {
return Object.values(value).every((pattern) => isValidRegex(pattern as string, path));
}
return true;
}The main validator function turns into this:
function validateCommitConfig(config: GitProxyConfig): boolean {
return (
validateConfigRegex(config, 'commitConfig.author.email.local.block') &&
validateConfigRegex(config, 'commitConfig.author.email.domain.allow') &&
validateConfigRegex(config, 'commitConfig.message.block.patterns') &&
validateConfigRegex(config, 'commitConfig.diff.block.patterns') &&
validateConfigRegex(config, 'commitConfig.diff.block.providers')
);
}|
@kriswest Did some refactoring here, although not quite a full-blown refactor for the QuickType stuff. At least, the actual QuickType I'd consider further refactoring if there's any extra feature requests or bugs in the ConfigLoader. A final check is much appreciated 😃 |
Fixes #1336.
Most of the changes are unit tests for the new code and
/src/config/index.ts.