ai-secure-audit 0.0.13 → 0.0.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/coverage.js +86 -2
- package/src/report-simple.js +7 -1
- package/src/report.js +6 -0
- package/src/scan.js +13 -0
- package/src/scanners/gitleaks.js +1 -1
- package/src/scanners/osv.js +6 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
> Status: 0.0.x on npm runs the basic check with `npx ai-secure-audit`: secrets, libraries with known holes and, from
|
|
4
4
|
> 0.0.5, code rules for routes anyone can repeat without limit and, from 0.0.9, for session checks that a built-in
|
|
5
5
|
> name such as `constructor` passes. From 0.0.10 it also checks Dockerfiles with trivy (Compose files are not read yet,
|
|
6
|
-
> and the report says so), and from 0.0.11 it warns when a Docker build would copy these results into the image, and from 0.0.13 `--report simple` writes a short to-do list in plain words (`report-simple.md`) and `live` checks one host you list in `targets.yml`: its TLS with testssl.sh, and with `--zap` its web headers and cookies with OWASP ZAP in Docker. `live` has only been tried on servers on the same machine and a GitHub test runner, never on a real remote site, and it does not run on Windows (TLS part) or without Docker (ZAP part). v0.1 is not released yet.
|
|
6
|
+
> and the report says so), and from 0.0.11 it warns when a Docker build would copy these results into the image, and from 0.0.13 `--report simple` writes a short to-do list in plain words (`report-simple.md`) and `live` checks one host you list in `targets.yml`: its TLS with testssl.sh, and with `--zap` its web headers and cookies with OWASP ZAP in Docker. `live` has only been tried on servers on the same machine and a GitHub test runner, never on a real remote site, and it does not run on Windows (TLS part) or without Docker (ZAP part). From 0.0.14 a secret in a file that was moved with `git mv` is no longer called "not committed yet" (that wording told people they could skip rotating a key that is already in git). v0.1 is not released yet.
|
|
7
7
|
>
|
|
8
8
|
> **มือใหม่เริ่มตรงนี้:** [learn/](learn/) ควรตรวจอะไรก่อน เพราะอะไร และแก้ยังไง (ภาษาไทย)
|
|
9
9
|
>
|
package/package.json
CHANGED
package/src/coverage.js
CHANGED
|
@@ -57,6 +57,79 @@ export const LOCKFILES = {
|
|
|
57
57
|
'mix.lock': { ecosystem: 'Hex (Elixir)', tested: false },
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
+
// Files that list libraries without fixing their versions, and the lockfiles that do. A project with one of these and no
|
|
61
|
+
// lockfile gets no library check at all, so the report has to say it. `uses` is true when the file really asks for a library.
|
|
62
|
+
const matches = (re) => (text) => re.test(text);
|
|
63
|
+
const json = (read) => (text) => { try { return read(JSON.parse(text)); } catch { return true; } };
|
|
64
|
+
const anyKey = (...objects) => objects.some((o) => o && Object.keys(o).length > 0);
|
|
65
|
+
const MANIFESTS = [
|
|
66
|
+
{ is: (n) => n === 'package.json', locks: ['package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock', 'pnpm-lock.yaml', 'bun.lock'],
|
|
67
|
+
uses: json((p) => anyKey(p.dependencies, p.devDependencies, p.optionalDependencies, p.peerDependencies)) },
|
|
68
|
+
// "php" and "ext-*" are the platform, not libraries: library names contain a slash.
|
|
69
|
+
{ is: (n) => n === 'composer.json', locks: ['composer.lock'],
|
|
70
|
+
uses: json((p) => Object.keys({ ...p.require, ...p['require-dev'] }).some((k) => k.includes('/'))) },
|
|
71
|
+
{ is: (n) => n === 'Gemfile', locks: ['Gemfile.lock'], uses: matches(/^\s*gem\s/m) },
|
|
72
|
+
{ is: (n) => n === 'Cargo.toml', locks: ['Cargo.lock'], uses: matches(/^\[(?:[^\]\n]*\.)?(?:dev-|build-)?dependencies(?:\.[^\]\n]*)?\]/m) },
|
|
73
|
+
{ is: (n) => n === 'pyproject.toml', locks: ['poetry.lock', 'uv.lock', 'Pipfile.lock', 'requirements.txt'],
|
|
74
|
+
uses: matches(/^\s*dependencies\s*=\s*\[\s*[^\]\s]|^\[(?:tool\.poetry\.(?:[\w.-]+\.)?dependencies|project\.optional-dependencies|dependency-groups)\]/m) },
|
|
75
|
+
{ is: (n) => n === 'Pipfile', locks: ['Pipfile.lock'], uses: matches(/^\[(?:dev-)?packages\][ \t]*\r?\n(?:[ \t]*(?:#.*)?\r?\n)*[ \t]*[^\[\s#]/m) },
|
|
76
|
+
{ is: (n) => n === 'pubspec.yaml', locks: ['pubspec.lock'], uses: matches(/^(?:dev_)?dependencies:/m) },
|
|
77
|
+
{ is: (n) => n.endsWith('.csproj'), locks: ['packages.lock.json'], uses: matches(/<PackageReference\b/) },
|
|
78
|
+
{ is: (n) => n === 'build.gradle' || n === 'build.gradle.kts', locks: ['gradle.lockfile'],
|
|
79
|
+
uses: matches(/\b(?:implementation|api|compileOnly|runtimeOnly|testImplementation)\b/) },
|
|
80
|
+
{ is: (n) => n === 'mix.exs', locks: ['mix.lock'], uses: matches(/\bdefp?\s+deps\b/) },
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
// Output written by a deploy tool: its package.json files are copies, not something a person keeps a lockfile for.
|
|
84
|
+
const GENERATED = /(?:^|\/)\.(?:vercel|netlify|serverless|wrangler)\//;
|
|
85
|
+
|
|
86
|
+
const parent = (dir) => (dir.includes('/') ? dir.slice(0, dir.lastIndexOf('/')) : '');
|
|
87
|
+
const at = (dir, name) => (dir ? `${dir}/${name}` : name);
|
|
88
|
+
const globRe = (glob) => new RegExp(`^${glob.replace(/^\.\//, '').replace(/\/$/, '')
|
|
89
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&').replace(/\*\*|\*|\?/g, (m) => (m === '**' ? '.*' : m === '*' ? '[^/]*' : '[^/]'))}$`);
|
|
90
|
+
|
|
91
|
+
// The folders a workspace at `dir` says belong to it (npm, yarn, pnpm and Cargo keep one lockfile at the top).
|
|
92
|
+
function workspaceMembers(root, dir, files) {
|
|
93
|
+
const read = (name) => { try { return files.has(at(dir, name)) ? readFileSync(join(root, dir, name), 'utf8') : ''; } catch { return ''; } };
|
|
94
|
+
const globs = [];
|
|
95
|
+
try {
|
|
96
|
+
const w = JSON.parse(read('package.json') || '{}').workspaces;
|
|
97
|
+
globs.push(...(Array.isArray(w) ? w : w?.packages ?? []));
|
|
98
|
+
} catch { /* an unreadable package.json declares no workspace */ }
|
|
99
|
+
for (const m of read('pnpm-workspace.yaml').matchAll(/^\s*-\s*['"]?([^'"#\r\n]+?)['"]?\s*(?:#.*)?$/gm)) globs.push(m[1]);
|
|
100
|
+
const cargo = /^\[workspace\][^[]*?members\s*=\s*\[([^\]]*)\]/m.exec(read('Cargo.toml'));
|
|
101
|
+
if (cargo) globs.push(...[...cargo[1].matchAll(/['"]([^'"]+)['"]/g)].map((m) => m[1]));
|
|
102
|
+
const include = globs.filter((g) => typeof g === 'string' && !g.startsWith('!')).map(globRe);
|
|
103
|
+
const exclude = globs.filter((g) => typeof g === 'string' && g.startsWith('!')).map((g) => globRe(g.slice(1)));
|
|
104
|
+
return (sub) => include.some((re) => re.test(sub)) && !exclude.some((re) => re.test(sub));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function manifestsWithoutLockfile(root, list) {
|
|
108
|
+
const files = new Set(list);
|
|
109
|
+
const missing = [];
|
|
110
|
+
for (const f of list) {
|
|
111
|
+
const name = basename(f);
|
|
112
|
+
const kind = MANIFESTS.find((m) => m.is(name));
|
|
113
|
+
if (!kind || GENERATED.test(f)) continue;
|
|
114
|
+
const dir = f === name ? '' : f.slice(0, -name.length - 1);
|
|
115
|
+
if (kind.locks.some((l) => files.has(at(dir, l)))) continue;
|
|
116
|
+
let covered = false;
|
|
117
|
+
for (let up = dir; up && !covered;) {
|
|
118
|
+
up = parent(up);
|
|
119
|
+
if (kind.locks.some((l) => files.has(at(up, l)))) covered = workspaceMembers(root, up, files)(up ? dir.slice(up.length + 1) : dir);
|
|
120
|
+
}
|
|
121
|
+
if (covered) continue;
|
|
122
|
+
let text = '';
|
|
123
|
+
try { text = readFileSync(join(root, f), 'utf8'); } catch { missing.push(f); continue; }
|
|
124
|
+
if (kind.uses(text)) missing.push(f);
|
|
125
|
+
}
|
|
126
|
+
return missing;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const named = (files) => `${files.slice(0, 5).join(', ')}${files.length > 5 ? ` and ${files.length - 5} more` : ''}`;
|
|
130
|
+
// Gaps that start like this mean some libraries got no check; the reports repeat it at the top.
|
|
131
|
+
export const NO_LIBRARY_CHECK = 'Libraries not checked:';
|
|
132
|
+
|
|
60
133
|
// Languages the kit's own code rules read, taken from the rule files so the list cannot fall behind them.
|
|
61
134
|
export function ruleLanguages(rulesDir = fileURLToPath(new URL('../rules/', import.meta.url))) {
|
|
62
135
|
const langs = new Set();
|
|
@@ -96,7 +169,7 @@ export function projectCoverage(root, { rulesDir, limit = 200_000 } = {}) {
|
|
|
96
169
|
}
|
|
97
170
|
const languages = [...counts].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1))
|
|
98
171
|
.map(([language, n]) => ({ language, files: n, code_rules: covered.has(language) }));
|
|
99
|
-
return { languages, lockfiles, compose, truncated: files.length >= limit };
|
|
172
|
+
return { languages, lockfiles, compose, unlocked: manifestsWithoutLockfile(root, files), truncated: files.length >= limit };
|
|
100
173
|
}
|
|
101
174
|
|
|
102
175
|
// Plain sentences for the report and for the AI assistant. Empty when everything found is covered.
|
|
@@ -109,7 +182,18 @@ export function coverageGaps(cov) {
|
|
|
109
182
|
'AI assistant with the prompt in https://github.com/surapas3022/ai-secure-audit/blob/main/docs/ai-review-prompt.md ' +
|
|
110
183
|
'(its results are leads to check, not findings).');
|
|
111
184
|
}
|
|
112
|
-
|
|
185
|
+
if (cov.unlocked?.length) {
|
|
186
|
+
gaps.push(`${NO_LIBRARY_CHECK} no lockfile (the file that records exact library versions) was found for ${named(cov.unlocked)}, ` +
|
|
187
|
+
'so the libraries listed there were not looked at; "0 findings" says nothing about them. Create the lockfile with your ' +
|
|
188
|
+
'package manager (for npm: npm install --package-lock-only), keep it in git, and run the check again.');
|
|
189
|
+
}
|
|
190
|
+
const unread = new Set(cov.unread ?? []);
|
|
191
|
+
if (unread.size) {
|
|
192
|
+
gaps.push(`${NO_LIBRARY_CHECK} osv-scanner read no libraries from ${named([...unread])}. It skips files that a .gitignore ` +
|
|
193
|
+
'names, even committed ones; the other usual reason is a file that lists no libraries. "0 findings" says nothing about ' +
|
|
194
|
+
'them. If .gitignore names the lockfile or its folder, remove that line and run the check again.');
|
|
195
|
+
}
|
|
196
|
+
const untested = cov.lockfiles.filter((l) => !l.tested && !unread.has(l.file));
|
|
113
197
|
if (untested.length) {
|
|
114
198
|
gaps.push(`Libraries: ${untested.map((l) => `${l.file} (${l.ecosystem})`).join(', ')} ` +
|
|
115
199
|
'were read by osv-scanner, but this kit has not tested that ecosystem with known answers yet.');
|
package/src/report-simple.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { countBySeverity } from './findings.js';
|
|
2
|
+
import { NO_LIBRARY_CHECK } from './coverage.js';
|
|
2
3
|
|
|
3
4
|
// Plain report for someone who does not know security words.
|
|
4
5
|
// Rules: no tool names, no commit hashes, no CWE ids, no severity jargon.
|
|
@@ -65,9 +66,14 @@ export function reportSimple(result, date) {
|
|
|
65
66
|
lines.push('> **The check is incomplete:** some parts did not run. Do not treat "nothing found" as safe until they do.', '');
|
|
66
67
|
}
|
|
67
68
|
|
|
69
|
+
const noLibraries = result.coverage?.gaps?.some((g) => g.startsWith(NO_LIBRARY_CHECK));
|
|
70
|
+
if (noLibraries) {
|
|
71
|
+
lines.push('> **Some libraries were not checked.** See "What was not checked" below.', '');
|
|
72
|
+
}
|
|
73
|
+
|
|
68
74
|
const total = c.critical + c.high + c.medium + c.low;
|
|
69
75
|
if (total === 0) {
|
|
70
|
-
lines.push(failed.length ? 'Nothing found by the parts that ran.' : '**Nothing found by these tools.** That is good, but read "What was not checked" below.', '');
|
|
76
|
+
lines.push(failed.length || noLibraries ? 'Nothing found by the parts that ran.' : '**Nothing found by these tools.** That is good, but read "What was not checked" below.', '');
|
|
71
77
|
} else {
|
|
72
78
|
lines.push(`**${total} thing${total === 1 ? '' : 's'} to fix.** Start at number 1 and work down. After each fix, run \`npx ai-secure-audit retest\` to see it disappear from the list.`, '');
|
|
73
79
|
}
|
package/src/report.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { countBySeverity } from './findings.js';
|
|
2
|
+
import { NO_LIBRARY_CHECK } from './coverage.js';
|
|
2
3
|
|
|
3
4
|
// Plain-language explanations, by finding category.
|
|
4
5
|
const PLAIN = {
|
|
@@ -63,6 +64,11 @@ export function reportMarkdown(result, date) {
|
|
|
63
64
|
lines.push('');
|
|
64
65
|
}
|
|
65
66
|
|
|
67
|
+
if (result.coverage?.gaps?.some((g) => g.startsWith(NO_LIBRARY_CHECK))) {
|
|
68
|
+
lines.push('> **Some libraries were not checked.** See "What this check could not see" below.');
|
|
69
|
+
lines.push('');
|
|
70
|
+
}
|
|
71
|
+
|
|
66
72
|
const top = findings.filter((f) => ['critical', 'high'].includes(f.severity.normalized)).slice(0, 5);
|
|
67
73
|
if (top.length) {
|
|
68
74
|
lines.push('## Fix these first');
|
package/src/scan.js
CHANGED
|
@@ -19,6 +19,13 @@ function git(root, args) {
|
|
|
19
19
|
return r.status === 0 ? r.stdout.trim() : null;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
// True when `file` is tracked and identical to HEAD. --literal-pathspecs: a file name from the scanned project is
|
|
23
|
+
// never read as a pattern (a file called "*.js" or ":(exclude)x" must not match other files).
|
|
24
|
+
export function committedUnchanged(root, file) {
|
|
25
|
+
const g = (args) => spawnSync('git', ['--literal-pathspecs', '-C', root, ...args]).status === 0;
|
|
26
|
+
return g(['ls-files', '--error-unmatch', '--', file]) && g(['diff', '--quiet', 'HEAD', '--', file]);
|
|
27
|
+
}
|
|
28
|
+
|
|
22
29
|
export function outDir(root) {
|
|
23
30
|
return join(root, '.audit', 'out');
|
|
24
31
|
}
|
|
@@ -42,12 +49,16 @@ export async function scan(target, { log = () => {}, version }) {
|
|
|
42
49
|
commit: isGit ? git(root, ['rev-parse', '--short', 'HEAD']) : null,
|
|
43
50
|
dirty: isGit ? git(root, ['status', '--porcelain']) !== '' : null,
|
|
44
51
|
prefix: isGit ? git(root, ['rev-parse', '--show-prefix']) ?? '' : '',
|
|
52
|
+
// Tracked and identical to HEAD: the content is in git even when gitleaks saw no commit add it at this path
|
|
53
|
+
// (a file moved with `git mv` shows up in history as a rename with no added lines).
|
|
54
|
+
isCommitted: (file) => isGit && committedUnchanged(root, file),
|
|
45
55
|
isIgnored: (file) => isGit && spawnSync('git', ['-C', root, 'check-ignore', '-q', '--', file]).status === 0,
|
|
46
56
|
};
|
|
47
57
|
|
|
48
58
|
const tools = lockedTools();
|
|
49
59
|
const runs = [];
|
|
50
60
|
let findings = [];
|
|
61
|
+
let librariesRead = null; // stays null when the library scanner did not run: the report says that already
|
|
51
62
|
for (const s of BASIC_SCANNERS) {
|
|
52
63
|
const run = { id: s.id, version: tools[s.id].version, license: tools[s.id].license, status: 'ok' };
|
|
53
64
|
runs.push(run);
|
|
@@ -72,6 +83,7 @@ export async function scan(target, { log = () => {}, version }) {
|
|
|
72
83
|
const data = existsSync(pass.rawFile) ? JSON.parse(readFileSync(pass.rawFile, 'utf8') || 'null') : null;
|
|
73
84
|
json = pass.name ? [...(json ?? []), ...(data ?? []).map((item) => ({ ...item, pass: pass.name }))] : data;
|
|
74
85
|
}
|
|
86
|
+
if (s.filesRead) librariesRead = new Set(s.filesRead(json, ctx));
|
|
75
87
|
const found = s.parse(json, ctx).map((f) => ({ ...f, source: { ...f.source, tool_version: run.version } }));
|
|
76
88
|
run.findings = found.length;
|
|
77
89
|
findings = findings.concat(found);
|
|
@@ -82,6 +94,7 @@ export async function scan(target, { log = () => {}, version }) {
|
|
|
82
94
|
}
|
|
83
95
|
|
|
84
96
|
const coverage = projectCoverage(root);
|
|
97
|
+
coverage.unread = librariesRead ? coverage.lockfiles.map((l) => l.file).filter((f) => !librariesRead.has(f)) : [];
|
|
85
98
|
const result = {
|
|
86
99
|
generator: `ai-secure-audit ${version}`,
|
|
87
100
|
check: 'basic',
|
package/src/scanners/gitleaks.js
CHANGED
|
@@ -107,7 +107,7 @@ export default {
|
|
|
107
107
|
const historyOnly = ctx.isGit !== false && inHistory && !inFiles.has(`${leak.RuleID}:${file}`) &&
|
|
108
108
|
(filesPassRan || !fileExists(file));
|
|
109
109
|
const built = kind === 'built';
|
|
110
|
-
const notCommitted = Boolean(ctx.isGit) && !inHistory && !built;
|
|
110
|
+
const notCommitted = Boolean(ctx.isGit) && !inHistory && !built && !ctx.isCommitted?.(file);
|
|
111
111
|
const guess = built && leak.RuleID === 'generic-api-key';
|
|
112
112
|
const fix = built ? FIX.built : notCommitted ? FIX.notCommitted : FIX.committed;
|
|
113
113
|
return {
|
package/src/scanners/osv.js
CHANGED
|
@@ -48,12 +48,17 @@ export default {
|
|
|
48
48
|
},
|
|
49
49
|
|
|
50
50
|
args(ctx, out) {
|
|
51
|
-
return ['scan', 'source', '-r', '--offline-vulnerabilities', '--download-offline-databases', '--allow-no-lockfiles',
|
|
51
|
+
return ['scan', 'source', '-r', '--offline-vulnerabilities', '--download-offline-databases', '--allow-no-lockfiles', '--all-packages',
|
|
52
52
|
'--format', 'json', '--output-file', out, '--verbosity', 'error', ctx.root];
|
|
53
53
|
},
|
|
54
54
|
|
|
55
55
|
okExit: [0, 1],
|
|
56
56
|
|
|
57
|
+
// The files libraries were really read from. A lockfile on disk that is not here was skipped.
|
|
58
|
+
filesRead(json, ctx) {
|
|
59
|
+
return (json?.results ?? []).filter((r) => r.packages?.length).map((r) => rel(ctx.root, r.source?.path));
|
|
60
|
+
},
|
|
61
|
+
|
|
57
62
|
parse(json, ctx) {
|
|
58
63
|
const findings = [];
|
|
59
64
|
for (const result of json?.results ?? []) {
|