Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
15 changes: 13 additions & 2 deletions src/runner/context.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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; }
Expand Down
16 changes: 14 additions & 2 deletions tests/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>) {
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);
Expand Down Expand Up @@ -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);
}
});