Skip to content

Conversation

@capricorn86
Copy link
Owner

No description provided.

if (!match[7] || match[7] === 'esm') {
resolvableCircularImports.push({ url, properties });
}
newCodeStart += `let {${properties.map((property) => (property.alias ? `"${property.name.replace(/"/g, '\\"')}": ${property.alias}` : property.name)).join(', ')}} = $happy_dom.imports.get('${url}')${match[8]}`;

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This does not escape backslash characters in the input.

Copilot Autofix

AI 3 days ago

The best way to fix the issue is to ensure that both double quotes (") and backslashes (\) in the property name are escaped when embedding a property name within double quotes in generated JavaScript code. This should be done using a regular expression replacement that first escapes backslashes and then double quotes, in that order (str.replace(/\\/g, '\\\\').replace(/"/g, '\\"')). This is necessary to prevent any special meaning or accidental termination of the quoted string in the output. The fix is limited to line 269 in packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts, within the map for property names. No new methods are needed; only the replacement logic in the string interpolation is changed, and no new imports are required.

Suggested changeset 1
packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts b/packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts
--- a/packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts
+++ b/packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts
@@ -266,7 +266,7 @@
 						if (!match[7] || match[7] === 'esm') {
 							resolvableCircularImports.push({ url, properties });
 						}
-						newCodeStart += `let {${properties.map((property) => (property.alias ? `"${property.name.replace(/"/g, '\\"')}": ${property.alias}` : property.name)).join(', ')}} = $happy_dom.imports.get('${url}')${match[8]}`;
+						newCodeStart += `let {${properties.map((property) => (property.alias ? `"${property.name.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}": ${property.alias}` : property.name)).join(', ')}} = $happy_dom.imports.get('${url}')${match[8]}`;
 					} else if (importMatch[2]) {
 						// Import all as
 						newCodeStart += `const ${importMatch[2]} = $happy_dom.imports.get('${url}')${match[8]}`;
EOF
@@ -266,7 +266,7 @@
if (!match[7] || match[7] === 'esm') {
resolvableCircularImports.push({ url, properties });
}
newCodeStart += `let {${properties.map((property) => (property.alias ? `"${property.name.replace(/"/g, '\\"')}": ${property.alias}` : property.name)).join(', ')}} = $happy_dom.imports.get('${url}')${match[8]}`;
newCodeStart += `let {${properties.map((property) => (property.alias ? `"${property.name.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}": ${property.alias}` : property.name)).join(', ')}} = $happy_dom.imports.get('${url}')${match[8]}`;
} else if (importMatch[2]) {
// Import all as
newCodeStart += `const ${importMatch[2]} = $happy_dom.imports.get('${url}')${match[8]}`;
Copilot is powered by AI and may make mistakes. Always verify output.
if (match[11]) {
newCode += match[0].replace(EXPORT_DEFAULT_REGEXP, '');
newCodeEnd += `$happy_dom.exports.default = ${name};\n`;
} else {

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This replaces only the first occurrence of '*'.

Copilot Autofix

AI 3 days ago

To fix the problem, change the string .replace('*', '') to use a regex with the global flag: .replace(/\*/g, ''). This ensures that all asterisks in the string are replaced, not just the first occurrence.

  • Only update the specific line in packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts where match[14].replace('*', '') occurs (line 333).
  • No new imports are required, as JavaScript RegExp literals are built-in.
Suggested changeset 1
packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts b/packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts
--- a/packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts
+++ b/packages/happy-dom/src/module/ECMAScriptModuleCompiler.ts
@@ -330,7 +330,7 @@
 			) {
 				// Export function or class type
 
-				const name = match[14].replace('*', '');
+				const name = match[14].replace(/\*/g, '');
 
 				if (name) {
 					if (match[11]) {
EOF
@@ -330,7 +330,7 @@
) {
// Export function or class type

const name = match[14].replace('*', '');
const name = match[14].replace(/\*/g, '');

if (name) {
if (match[11]) {
Copilot is powered by AI and may make mistakes. Always verify output.
if (exportName && importName) {
exportCode.push(`$happy_dom.exports['${exportName}'] = ${importName}`);
}
}

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '\t'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '\t'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '\t'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of '\t'.
: packageJson.exports[key].import;
if (importEntry) {
const regExp = new RegExp(
`^${key.replace('./', '').replace('.', '\\.').replace('*', '(.*)')}$`

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This replaces only the first occurrence of '*'.

Copilot Autofix

AI 12 days ago

To generically escape or encode all occurrences of the meta-character *, replace .replace('*', '(.*)') with .replace(/\*/g, '(.*)'). This ensures every occurrence of * in the key is handled, producing a regular expression that matches all wildcards in the package export key. The change should be made on line 160 in packages/happy-dom/src/module/ModuleURLUtility.ts. No additional imports or new library dependencies are required, as this uses native JavaScript RegExp functionality.

Suggested changeset 1
packages/happy-dom/src/module/ModuleURLUtility.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/happy-dom/src/module/ModuleURLUtility.ts b/packages/happy-dom/src/module/ModuleURLUtility.ts
--- a/packages/happy-dom/src/module/ModuleURLUtility.ts
+++ b/packages/happy-dom/src/module/ModuleURLUtility.ts
@@ -157,7 +157,7 @@
 					: packageJson.exports[key].import;
 				if (importEntry) {
 					const regExp = new RegExp(
-						`^${key.replace('./', '').replace('.', '\\.').replace('*', '(.*)')}$`
+						`^${key.replace('./', '').replace('.', '\\.').replace(/\*/g, '(.*)')}$`
 					);
 					const match = subPath.match(regExp);
 					if (match) {
EOF
@@ -157,7 +157,7 @@
: packageJson.exports[key].import;
if (importEntry) {
const regExp = new RegExp(
`^${key.replace('./', '').replace('.', '\\.').replace('*', '(.*)')}$`
`^${key.replace('./', '').replace('.', '\\.').replace(/\*/g, '(.*)')}$`
);
const match = subPath.match(regExp);
if (match) {
Copilot is powered by AI and may make mistakes. Always verify output.
);
const match = subPath.match(regExp);
if (match) {
const resolvedSubPath = importEntry.replace('./', '').replace('*', match[1]);

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This replaces only the first occurrence of '*'.

Copilot Autofix

AI 12 days ago

To correctly replace all instances of the asterisk (*) in importEntry, use the .replace() method with a global regular expression: .replace(/\*/g, match[1]). This change ensures that all wildcard characters are replaced with the intended value, not just the first occurrence. The fix should only modify line 164 within the method, updating the replacement logic. No new imports or definitions are required.


Suggested changeset 1
packages/happy-dom/src/module/ModuleURLUtility.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/happy-dom/src/module/ModuleURLUtility.ts b/packages/happy-dom/src/module/ModuleURLUtility.ts
--- a/packages/happy-dom/src/module/ModuleURLUtility.ts
+++ b/packages/happy-dom/src/module/ModuleURLUtility.ts
@@ -161,7 +161,7 @@
 					);
 					const match = subPath.match(regExp);
 					if (match) {
-						const resolvedSubPath = importEntry.replace('./', '').replace('*', match[1]);
+						const resolvedSubPath = importEntry.replace('./', '').replace(/\*/g, match[1]);
 						const resolvedURL = `${baseURL}${packageName}/${resolvedSubPath}`;
 						this.nodeModuleResolveCache.set(url, resolvedURL);
 						return resolvedURL;
EOF
@@ -161,7 +161,7 @@
);
const match = subPath.match(regExp);
if (match) {
const resolvedSubPath = importEntry.replace('./', '').replace('*', match[1]);
const resolvedSubPath = importEntry.replace('./', '').replace(/\*/g, match[1]);
const resolvedURL = `${baseURL}${packageName}/${resolvedSubPath}`;
this.nodeModuleResolveCache.set(url, resolvedURL);
return resolvedURL;
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants