fix(shared): escape backslashes before quotes in template preprocessor#1866
Conversation
Fixes CodeQL alert for incomplete string escaping (CWE-116). The path escaping now escapes backslash characters before double quotes to prevent bypass via backslash-escaped quotes.
There was a problem hiding this comment.
Pull request overview
This PR fixes a security vulnerability (CWE-116: Incomplete String Escaping) in the template preprocessor by ensuring backslashes are escaped before quotes when preparing path strings for injection into Handlebars templates. The fix prevents bypass attacks where an attacker could use \ to escape the escaping quote itself (e.g., foo\" becoming foo\\" which breaks the string boundary).
Key Changes
- Modified the
safePathescaping logic to escape backslashes first, then quotes, ensuring proper string sanitization - Maintains correct escaping order:
replace(/\\/g, '\\\\')beforereplace(/"/g, '\\"')
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
| } | ||
|
|
||
| const safePath = path.replace(/"/g, '\\"'); | ||
| const safePath = path.replace(/\\/g, '\\\\').replace(/"/g, '\\"'); |
There was a problem hiding this comment.
This security fix for CWE-116 (incomplete string escaping) lacks test coverage. Consider adding a test case that verifies paths containing backslashes are properly escaped. For example:
it('escapes backslashes in paths to prevent quote bypass', () => {
const input = '{{list foo\\bar}}';
const output = TemplatePreprocessor.preprocessTemplate(input);
expect(output).toBe('{{list (convertFromDotNotation this "foo\\\\bar")}}');
});
it('escapes backslashes before quotes in paths', () => {
const input = '{{nodes[\\"test\\"]}}';
const output = TemplatePreprocessor.preprocessTemplate(input);
expect(output).toBe('{{convertFromDotNotation this "nodes[\\\\\\"test\\\\\\"]"}}');
});Without tests, it's difficult to verify the fix works correctly and prevent regression in the future.
Description
Fixes CodeQL alert for incomplete string escaping (CWE-116). The path escaping now escapes backslash characters before double quotes to prevent bypass via backslash-escaped quotes.
https://github.com/finos/architecture-as-code/security/code-scanning/48
Type of Change
Affected Components
cli/)shared/)calm-widgets/)calm-hub/)calm-hub-ui/)docs/)calm-plugins/vscode/)Commit Message Format ✅
Testing
Checklist