From 79114290bfce61b26ba3fd0e945d951f3d470fd1 Mon Sep 17 00:00:00 2001 From: AppSolves Date: Sun, 15 Nov 2026 04:21:40 +0100 Subject: [PATCH] fix: distinguish real module imports from generated code strings --- CHANGELOG.md | 2 ++ src/runner/context.ts | 15 +++++++++++++-- tests/context.test.ts | 16 ++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4669cea..4d1605e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Security and behavior changes +- Parse real JS/TS import syntax with the existing pinned TypeScript compiler when selecting essential context. Generated-code strings/comments no longer cause false missing-dependency failures; actual missing imports/exports/require calls remain fail-closed. + - Honor public read access for authorized non-collaborator PR events only when explicitly assigned to a consenting repository admin. Private repositories, manual commands, tenant identity checks and separate merge authorization are unchanged. - Respect the provider's earliest refresh timestamp before runtime token renewal. Bound the credential wait and workflow lifetime, preserve exclusive leases/CAS and persist rotation before inference; classify premature/unavailable refresh windows without raw credential diagnostics. diff --git a/src/runner/context.ts b/src/runner/context.ts index 326d44e..261c4f5 100644 --- a/src/runner/context.ts +++ b/src/runner/context.ts @@ -1,3 +1,4 @@ +import ts from 'typescript'; import type { Octokit } from '@octokit/rest'; import { mkdir, writeFile } from 'node:fs/promises'; import { createHash } from 'node:crypto'; @@ -115,8 +116,18 @@ export async function collectContext(github: Octokit, snapshot: RequestSnapshot, for (const file of changed) for (const [label, commit, filename] of [['base', snapshot.base, file.previous_filename ?? file.filename], ['head', snapshot.head, file.filename]] as const) { if (!/\.[cm]?[jt]sx?$/.test(filename)) continue; const text = written.get(`${label}/${filename}`)?.toString('utf8') ?? ''; - for (const match of text.matchAll(/(?:from\s*|import\s*\(?|require\()\s*['"](\.{1,2}\/[^'"]{1,200})['"]/g)) { - const path = posix.normalize(posix.join(posix.dirname(filename!), match[1]!)); + const imports: string[] = []; + const visit = (node: ts.Node): void => { + const specifier = (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) ? node.moduleSpecifier + : ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference) ? node.moduleReference.expression + : ts.isCallExpression(node) && (node.expression.kind === ts.SyntaxKind.ImportKeyword || (ts.isIdentifier(node.expression) && node.expression.text === 'require')) ? node.arguments[0] : undefined; + if (specifier && ts.isStringLiteralLike(specifier) && /^\.{1,2}\//.test(specifier.text)) imports.push(specifier.text); + ts.forEachChild(node, visit); + }; + // Parse syntax: generated code strings and comments are not actual module dependencies. + visit(ts.createSourceFile(filename, text, ts.ScriptTarget.Latest, true)); + for (const specifier of imports) { + const path = posix.normalize(posix.join(posix.dirname(filename!), specifier)); if (posix.extname(path) && !/\.(?:[cm]?[jt]sx?|json)$/.test(path)) continue; if (dependencies.has(`${label}/${path}`)) continue; if (!safePath(path) || dependencies.size >= 24) { limited(true); continue; } diff --git a/tests/context.test.ts b/tests/context.test.ts index 6afce50..c6586ba 100644 --- a/tests/context.test.ts +++ b/tests/context.test.ts @@ -8,10 +8,10 @@ import { join } from 'node:path'; import { collectContext, type ContextLookup } from '../src/runner/context.js'; import { snapshot, HEAD, BASE, WORKFLOW, files } from './fixtures.js'; -type Options = { language?: 'py' | 'rs'; mode?: string; truncated?: boolean; binary?: boolean; corrupt?: boolean; status?: string; patch?: string; apiStatus?: number; missingFiles?: boolean; dependency?: 'present' | 'missing' }; +type Options = { source?: string; language?: 'py' | 'rs'; mode?: string; truncated?: boolean; binary?: boolean; corrupt?: boolean; status?: string; patch?: string; apiStatus?: number; missingFiles?: boolean; dependency?: 'present' | 'missing' }; async function fixture(options: Options = {}, exercise?: (lookup: ContextLookup, directory: string) => Promise) { const directory = await mkdtemp(join(tmpdir(), 'codexbot-context-')); - const requested: string[] = [], blob = Buffer.from(options.binary ? 'binary\0data' : options.language === 'py' ? 'from .dep import check\nfixture context text' : options.language === 'rs' ? 'mod dep;\nfixture context text' : options.dependency ? 'import { check } from "./dep.js";\nfixture context text' : 'fixture context text'); + const requested: string[] = [], blob = Buffer.from(options.source ?? (options.binary ? 'binary\0data' : options.language === 'py' ? 'from .dep import check\nfixture context text' : options.language === 'rs' ? 'mod dep;\nfixture context text' : options.dependency ? 'import { check } from "./dep.js";\nfixture context text' : 'fixture context text')); const blobSha = createHash('sha1').update(`blob ${blob.length}\0`).update(blob).digest('hex'); const dependency = Buffer.from('export const check = () => true;'), dependencySha = createHash('sha1').update(`blob ${dependency.length}\0`).update(dependency).digest('hex'); const root = 'e'.repeat(40), src = 'f'.repeat(40); @@ -100,3 +100,15 @@ await test('missing or oversized diffs are explicitly incomplete even with compl await test('404 and rate-limit commit resolution cannot yield a complete context', async () => { for (const apiStatus of [404, 403, 429]) assert.equal((await fixture({ apiStatus })).result.complete, false); }); + +await test('literal imports inside generated code, templates and comments are not essential dependencies',async()=>{ + const source=`const generated = "import {check} from './missing.js';"; +const template = \`require('./missing.cjs')\`; +// export {check} from './missing.ts'; +/* import('./missing.mjs'); */ +export const fixture = {generated,template};`; + const result=await fixture({source});assert.equal(result.result.complete,true);assert.equal(result.result.requiredContextComplete,true); + for(const source of ["export {check} from './missing.js';","const x = import('./missing.js');","const x = require('./missing.js');","import x = require('./missing.js');"]){ + const missing=await fixture({source});assert.equal(missing.result.complete,false,source);assert.equal(missing.result.requiredContextComplete,false,source); + } +});