-
-
Notifications
You must be signed in to change notification settings - Fork 374
chore: update enhanced test tool #4252
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
base: main
Are you sure you want to change the base?
Conversation
|
✅ Deploy Preview for module-federation-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
| ); | ||
| const code = `(function(${args.join( | ||
| ', ', | ||
| )}) {${content}\n})`; |
Check warning
Code scanning / CodeQL
Improper code sanitization Medium test
improperly sanitized value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 8 days ago
To fix the improper code sanitization, we need to ensure that values interpolated into generated JavaScript code (here, as part of require(...) in content) are properly sanitized and any potentially dangerous characters are escaped. Following the recommendation, we should define a function (e.g., escapeUnsafeChars) that escapes characters like <, >, /, \, and other relevant Unicode/control chars after running JSON.stringify. We will update line 783 so that instead of just JSON.stringify('./' + arg), we run this escape function on the result. This means:
- Add a helper escape function (e.g.,
escapeUnsafeChars) in this file. - Change the string interpolation in the mapping on line 783 to embed the escaped string instead.
- No other code needs to be changed.
This fix is fully contained in packages/enhanced/test/ConfigTestCases.rstest.ts, and only requires code additions and a small refactor to the mapping logic.
-
Copy modified lines R17-R36 -
Copy modified line R803
| @@ -14,6 +14,26 @@ | ||
| rs, | ||
| } from '@rstest/core'; | ||
|
|
||
| // Escape potentially dangerous characters for insertion into generated JS code | ||
| const charMap: { [key: string]: string } = { | ||
| '<': '\\u003C', | ||
| '>': '\\u003E', | ||
| '/': '\\u002F', | ||
| '\\': '\\\\', | ||
| '\b': '\\b', | ||
| '\f': '\\f', | ||
| '\n': '\\n', | ||
| '\r': '\\r', | ||
| '\t': '\\t', | ||
| '\0': '\\0', | ||
| '\u2028': '\\u2028', | ||
| '\u2029': '\\u2029' | ||
| }; | ||
| function escapeUnsafeChars(str: string): string { | ||
| // Only escape characters appearing in charMap | ||
| return str.replace(/[<>\b\f\n\r\t\0\u2028\u2029\\/]/g, x => charMap[x] || x); | ||
| } | ||
|
|
||
| // 预热 webpack(与原逻辑一致) | ||
| require('./helpers/warmup-webpack'); | ||
|
|
||
| @@ -780,7 +800,7 @@ | ||
| content = `module.exports = (${module | ||
| .map( | ||
| (arg: any) => | ||
| `require(${JSON.stringify(`./${arg}`)})`, | ||
| `require(${escapeUnsafeChars(JSON.stringify(`./${arg}`))})`, | ||
| ) | ||
| .join(', ')});`; | ||
| } else { |
| ); | ||
| const code = `(function(${args.join( | ||
| ', ', | ||
| )}) {${content}\n})`; |
Check warning
Code scanning / CodeQL
Improper code sanitization Medium test
improperly sanitized value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 8 days ago
To fix this issue, we should fully sanitize all strings that will be injected into executable code, not relying solely on JSON.stringify. As per recommended patterns (and CodeQL docs), we should introduce an escape function that replaces potentially dangerous characters (e.g. <, >, /, \, and line breaks) with safe escape sequences before including them.
Specifically:
- Implement an
escapeUnsafeCharsfunction in the relevant region of the file. - In the dynamic code construction at line 783, wrap the output of
JSON.stringifywithescapeUnsafeChars. - This ensures that even if
argincludes risky characters, they will be safely escaped and cannot break out of the script context or cause injection.
All changes are confined to the appropriate edit region.
-
Copy modified lines R780-R800 -
Copy modified line R804
| @@ -777,10 +777,31 @@ | ||
| currentDirectory, | ||
| '.array-require.js', | ||
| ); | ||
| // Add escapeUnsafeChars helper | ||
| const charMap = { | ||
| '<': '\\u003C', | ||
| '>': '\\u003E', | ||
| '/': '\\u002F', | ||
| '\\': '\\\\', | ||
| '\b': '\\b', | ||
| '\f': '\\f', | ||
| '\n': '\\n', | ||
| '\r': '\\r', | ||
| '\t': '\\t', | ||
| '\0': '\\0', | ||
| '\u2028': '\\u2028', | ||
| '\u2029': '\\u2029' | ||
| }; | ||
| function escapeUnsafeChars(str: string) { | ||
| return str.replace( | ||
| /[<>\b\f\n\r\t\0\u2028\u2029/\\]/g, | ||
| x => (charMap as any)[x] || x, | ||
| ); | ||
| } | ||
| content = `module.exports = (${module | ||
| .map( | ||
| (arg: any) => | ||
| `require(${JSON.stringify(`./${arg}`)})`, | ||
| `require(${escapeUnsafeChars(JSON.stringify(`./${arg}`))})`, | ||
| ) | ||
| .join(', ')});`; | ||
| } else { |
Description
Related Issue
Types of changes
Checklist