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 {
@@ -87,3 +87,59 @@ test('accepts adjacent block punctuation and raw string tokens', () => {
assert.deepEqual(getGoDeps(dir), ['github.com/gin-gonic/gin', 'github.com/labstack/echo/v4']);
});
});
test('decodes Go escapes in quoted module paths before framework detection', () => {
for (const escaped of [String.raw`\x67`, String.raw`\147`, String.raw`\u0067`, String.raw`\U00000067`]) {
for (const newline of ['\n', '\r\n']) {
withGoMod([
'module example.com/app',
`require "github.com/gin-gonic/${escaped}in" "\\x761.10.0"`,
'require(',
String.raw` "github.com/labstack/\x65cho/v4" v4.13.0`,
')',
].join(newline), dir => {
assert.deepEqual(getGoDeps(dir), ['github.com/gin-gonic/gin', 'github.com/labstack/echo/v4']);
assert.deepEqual(detectProjectType(dir).frameworks, ['gin', 'echo']);
});
}
}
});
test('rejects malformed quoted escapes without losing later requirements', () => {
for (const escaped of [String.raw`\q`, String.raw`\x6`, String.raw`\777`, String.raw`\uD800`, String.raw`\U00110000`]) {
withGoMod([
'module example.com/app',
`require "github.com/gin-gonic/${escaped}in" v1.10.0`,
'require github.com/labstack/echo/v4 v4.13.0',
].join('\n'), dir => {
assert.deepEqual(getGoDeps(dir), ['github.com/labstack/echo/v4']);
});
}
});
test('preserves raw quoted paths and decodes byte escapes as UTF-8', () => {
withGoMod([
'module example.com/app',
'require `github.com/gin-gonic/\\x67in` v1.10.0',
String.raw`require "example.com/\xc3\xa9" v1.0.0`,
String.raw`require "\ufeffgithub.com/gin-gonic/gin" v1.10.0`,
].join('\n'), dir => {
assert.deepEqual(getGoDeps(dir), [String.raw`github.com/gin-gonic/\x67in`, 'example.com/é', '\ufeffgithub.com/gin-gonic/gin']);
assert.deepEqual(detectProjectType(dir).frameworks, []);
});
});
test('accepts adjacent quoted module and version tokens', () => {
for (const newline of ['\n', '\r\n']) {
withGoMod([
'module example.com/app',
String.raw`require "github.com/gin-gonic/\x67in""v1.10.0"`,
'require(',
' "github.com/labstack/echo/v4""v4.13.0"',
')',
].join(newline), dir => {
assert.deepEqual(getGoDeps(dir), ['github.com/gin-gonic/gin', 'github.com/labstack/echo/v4']);
assert.deepEqual(detectProjectType(dir).frameworks, ['gin', 'echo']);
});
}
});