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: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- [x] Python ecosystem support (`requirements.txt`, `pyproject.toml`)
- [x] Severity filtering (`--min-severity`)
- [x] File/path exclusion patterns
- [ ] Performance optimization for large repositories
- [x] Performance optimization for large repositories
- [ ] More comprehensive test fixtures

## Future (not committed)
Expand Down
44 changes: 26 additions & 18 deletions src/rules/opk-001-ai-credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,37 @@ const rule: Rule = {

const absolutePath = path.join(context.rootDir, relativePath);

let stat: fs.Stats;
try {
stat = fs.statSync(absolutePath);
} catch {
continue;
}
let content: string | undefined;
if (context.getFileContent) {
content = await context.getFileContent(relativePath);
if (content === undefined) {
continue;
}
} else {
let stat: fs.Stats;
try {
stat = fs.statSync(absolutePath);
} catch {
continue;
}

if (stat.size > MAX_FILE_SIZE || stat.size === 0) {
continue;
}
if (stat.size > MAX_FILE_SIZE || stat.size === 0) {
continue;
}

let buffer: Buffer;
try {
buffer = fs.readFileSync(absolutePath);
} catch {
continue;
}
let buffer: Buffer;
try {
buffer = fs.readFileSync(absolutePath);
} catch {
continue;
}

if (isBinaryContent(buffer)) {
continue;
if (isBinaryContent(buffer)) {
continue;
}
content = buffer.toString('utf-8');
}

const content = buffer.toString('utf-8');
const lines = content.split('\n');

for (let i = 0; i < lines.length; i++) {
Expand Down
43 changes: 25 additions & 18 deletions src/rules/opk-002-prompt-artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,36 @@ const rule: Rule = {

const absolutePath = path.join(context.rootDir, relativePath);

let stat: fs.Stats;
try {
stat = fs.statSync(absolutePath);
} catch {
continue;
}
let content: string | undefined;
if (context.getFileContent) {
content = await context.getFileContent(relativePath);
if (content === undefined) continue;
} else {
let stat: fs.Stats;
try {
stat = fs.statSync(absolutePath);
} catch {
continue;
}

if (stat.size > MAX_FILE_SIZE || stat.size === 0) {
continue;
}
if (stat.size > MAX_FILE_SIZE || stat.size === 0) {
continue;
}

let buffer: Buffer;
try {
buffer = fs.readFileSync(absolutePath);
} catch {
continue;
}
let buffer: Buffer;
try {
buffer = fs.readFileSync(absolutePath);
} catch {
continue;
}

if (isBinaryContent(buffer)) {
continue;
if (isBinaryContent(buffer)) {
continue;
}

content = buffer.toString('utf-8');
}

const content = buffer.toString('utf-8');
const lines = content.split('\n');

for (let i = 0; i < lines.length; i++) {
Expand Down
43 changes: 25 additions & 18 deletions src/rules/opk-003-placeholder-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,36 @@ const rule: Rule = {

const absolutePath = path.join(context.rootDir, relativePath);

let stat: fs.Stats;
try {
stat = fs.statSync(absolutePath);
} catch {
continue;
}
let content: string | undefined;
if (context.getFileContent) {
content = await context.getFileContent(relativePath);
if (content === undefined) continue;
} else {
let stat: fs.Stats;
try {
stat = fs.statSync(absolutePath);
} catch {
continue;
}

if (stat.size > MAX_FILE_SIZE || stat.size === 0) {
continue;
}
if (stat.size > MAX_FILE_SIZE || stat.size === 0) {
continue;
}

let buffer: Buffer;
try {
buffer = fs.readFileSync(absolutePath);
} catch {
continue;
}
let buffer: Buffer;
try {
buffer = fs.readFileSync(absolutePath);
} catch {
continue;
}

if (isBinaryContent(buffer)) {
continue;
if (isBinaryContent(buffer)) {
continue;
}

content = buffer.toString('utf-8');
}

const content = buffer.toString('utf-8');
const lines = content.split('\n');

for (let i = 0; i < lines.length; i++) {
Expand Down
76 changes: 59 additions & 17 deletions src/scanner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ export async function scan(
const excludePatterns = config?.exclude || [];

const files = await discoverFiles(absoluteRoot, excludePatterns);

const context: ScanContext = {
rootDir: absoluteRoot,
files,
};
const disabledRules = new Set<string>();

if (config?.rules) {
Expand All @@ -106,19 +101,66 @@ export async function scan(
const allFindings: ScanResult['findings'] = [];
let rulesRun = 0;

for (const rule of rules) {
if (disabledRules.has(rule.id)) {
continue;
}
const activeRules = rules.filter(r => !disabledRules.has(r.id));
rulesRun = activeRules.length;

try {
const findings = await rule.check(context);
allFindings.push(...findings);
rulesRun++;
} catch (err: unknown) {
const message = err instanceof Error ? err.message : String(err);
process.stderr.write(`Warning: Rule ${rule.id} failed: ${message}\n`);
rulesRun++;
const CHUNK_SIZE = 100;
for (let i = 0; i < files.length; i += CHUNK_SIZE) {
const chunk = files.slice(i, i + CHUNK_SIZE);
const fileCache = new Map<string, string | null>();

const getFileContent = async (relativePath: string): Promise<string | undefined> => {
if (fileCache.has(relativePath)) {
const cached = fileCache.get(relativePath);
return cached === null ? undefined : cached;
}
try {
const absolutePath = path.join(absoluteRoot, relativePath);
const stat = await fs.promises.stat(absolutePath);
if (stat.size === 0 || stat.size > 1_000_000) {
fileCache.set(relativePath, null);
return undefined;
}
const buffer = await fs.promises.readFile(absolutePath);

// binary check
let isBinary = false;
const checkLength = Math.min(buffer.length, 8000);
for (let j = 0; j < checkLength; j++) {
if (buffer[j] === 0) {
isBinary = true;
break;
}
}

if (isBinary) {
fileCache.set(relativePath, null);
return undefined;
}

const content = buffer.toString('utf8');
fileCache.set(relativePath, content);
return content;
} catch {
fileCache.set(relativePath, null);
return undefined;
}
};

const chunkContext: ScanContext = {
rootDir: absoluteRoot,
files: chunk,
getFileContent,
};

for (const rule of activeRules) {
try {
const findings = await rule.check(chunkContext);
allFindings.push(...findings);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : String(err);
process.stderr.write(`Warning: Rule ${rule.id} failed: ${message}\n`);
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface ScanContext {
rootDir: string;
/** List of file paths relative to rootDir */
files: string[];
/** Optional file cache accessor to improve I/O performance */
getFileContent?: (relativePath: string) => Promise<string | undefined>;
}

export interface Rule {
Expand Down
Loading