fix(project-detect): decode quoted Go module tokens

This commit is contained in:
Afloat committed 2026-11-11 21:41:52 +08:00
1 parent bbdfd133e0
commit b7de3157fd
2 files changed
+94 -2

No files matched your search

+38 -2
View File
@@ -245,6 +245,38 @@ function getPythonDeps(projectDir) {
return deps;
}
/**
* Decode the contents of a Go-quoted module path, rejecting malformed escapes.
* @param {string} value - Quoted token contents without the surrounding quotes
* @returns {string|null} Decoded UTF-8 path, or null for an invalid token
*/
function decodeGoQuotedPath(value) {
if (!/^(?:[^"\\\r\n]|\\(?:[abfnrtv\\"]|[0-7]{3}|x[\da-fA-F]{2}|u[\da-fA-F]{4}|U[\da-fA-F]{8}))*$/.test(value)) return null;
const escaped = { a: '\x07', b: '\b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v', '\\': '\\', '"': '"' };
const chunks = [];
for (const token of value.match(/[^\\]+|\\(?:[abfnrtv\\"]|[0-7]{3}|x[\da-fA-F]{2}|u[\da-fA-F]{4}|U[\da-fA-F]{8})/g) || []) {
if (!token.startsWith('\\')) {
chunks.push(Buffer.from(token));
continue;
}
const escape = token.slice(1);
if (Object.hasOwn(escaped, escape)) {
chunks.push(Buffer.from(escaped[escape]));
continue;
}
const octal = /^[0-7]/.test(escape);
const codePoint = Number.parseInt(octal ? escape : escape.slice(1), octal ? 8 : 16);
const byte = octal || escape.startsWith('x');
if (byte ? codePoint > 255 : codePoint > 0x10ffff || (codePoint >= 0xd800 && codePoint <= 0xdfff)) return null;
chunks.push(byte ? Buffer.from([codePoint]) : Buffer.from(String.fromCodePoint(codePoint)));
}
try {
return new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(Buffer.concat(chunks));
} catch {
return null;
}
}
/**
* Read go.mod for Go module dependencies
* @param {string} projectDir - Project root directory
@@ -271,8 +303,12 @@ function getGoDeps(projectDir) {
}
if (!directive && !inRequireBlock) continue;
const requirement = (directive ? directive[1] : line)
.match(/^(?:"([^"]+)"|`([^`]+)`|(\S+))\s+\S+/);
if (requirement) deps.push(requirement[1] || requirement[2] || requirement[3]);
.match(/^(?:"((?:[^"\\]|\\.)+)"[ \t]*|`([^`]+)`[ \t]*|([^"`\s]+)[ \t]+)\S+/);
if (!requirement) continue;
const modulePath = requirement[1] === undefined
? requirement[2] || requirement[3]
: decodeGoQuotedPath(requirement[1]);
if (modulePath) deps.push(modulePath);
}
return deps;
} catch {