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 .github/workflows/codeguardian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:
pull_request:
branches: [ main ]

workflow_dispatch:

jobs:
scan:
name: Run CodeGuardian
Expand Down
46 changes: 46 additions & 0 deletions src/safeRegex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Worker, isMainThread, parentPort, workerData } from "node:worker_threads";

if (!isMainThread) {
// runs inside the isolated worker thread
const { line, pattern, flags } = workerData;
try {
const regex = new RegExp(pattern, flags || "g");
const matched = regex.test(line);
parentPort.postMessage({ success: true, matched });
} catch (error) {
parentPort.postMessage({ success: false, error: error.message });
}
}

/*
* runs on main thread
*/
export function matchWithTimeout(line, rule, timeoutMs = 50) {
return new Promise((resolve) => {
const worker = new Worker(new URL(import.meta.url), {
workerData: { line, pattern: rule.pattern, flags: rule.flags },
});

// Kill the thread if it takes too long
const timeout = setTimeout(() => {
worker.terminate();
resolve(false);
}, timeoutMs);

worker.on("message", (msg) => {
clearTimeout(timeout);
worker.terminate();
if (msg.success) {
resolve(msg.matched);
} else {
resolve(false);
}
});

worker.on("error", () => {
clearTimeout(timeout);
worker.terminate();
resolve(false);
});
});
}
17 changes: 5 additions & 12 deletions src/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ignore from 'ignore';
import { execSync } from 'node:child_process';
import { styleText } from 'node:util';
import { findUnusedModules } from './unusedModuleDetector.js';
import { matchWithTimeout } from './safeRegex.js';

const DEFAULT_CONFIG_FILES = ['.codeguardianrc.json', 'codeguardian.config.json'];

Expand Down Expand Up @@ -70,22 +71,14 @@ function listFiles({ staged, ignoreFiles } = {}) {
return entries.filter(e => !ig.ignores(e));
}

function findMatchesInFile(content, rules) {
async function findMatchesInFile(content, rules) {
const lines = content.split(/\r?\n/);
const findings = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
for (const rule of rules) {
let flags = 'g';
if (rule.flags) flags = rule.flags;
let regex;
try {
regex = new RegExp(rule.pattern, flags);
} catch (err) {
// invalid regex, skip
continue;
}
if (regex.test(line)) {
const isMatch = await matchWithTimeout(line, rule, 50);
if (isMatch) {
findings.push({ rule: rule.name || 'unnamed', lineNumber: i + 1, line: line.trim(), pattern: rule.pattern });
}
}
Expand Down Expand Up @@ -121,7 +114,7 @@ async function run({ configPath = null, staged = false, verbose = false } = {})
continue;
}
filesScanned++;
const fileFindings = findMatchesInFile(content, rules);
const fileFindings = await findMatchesInFile(content, rules);
if (fileFindings.length > 0) {
findings.push({ file, matches: fileFindings });
}
Expand Down