diff --git a/scripts/lib/project-detect.js b/scripts/lib/project-detect.js index 36697a9e0..e2cf0b141 100644 --- a/scripts/lib/project-detect.js +++ b/scripts/lib/project-detect.js @@ -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 { diff --git a/tests/lib/project-detect-go-requires.test.js b/tests/lib/project-detect-go-requires.test.js index aac81d56e..a5f3e4f54 100644 --- a/tests/lib/project-detect-go-requires.test.js +++ b/tests/lib/project-detect-go-requires.test.js @@ -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']); + }); + } +});