wp-shakedown 0.0.0-stage → 0.2.0
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/LICENSE +340 -0
- package/README.md +74 -2
- package/dist/baseline.d.ts +6 -0
- package/dist/baseline.js +41 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +296 -0
- package/dist/diff.d.ts +8 -0
- package/dist/diff.js +26 -0
- package/dist/engines/config.d.ts +3 -0
- package/dist/engines/config.js +134 -0
- package/dist/engines/custom.d.ts +10 -0
- package/dist/engines/custom.js +286 -0
- package/dist/engines/eslint.d.ts +8 -0
- package/dist/engines/eslint.js +96 -0
- package/dist/engines/index.d.ts +2 -0
- package/dist/engines/index.js +14 -0
- package/dist/engines/integrity.d.ts +2 -0
- package/dist/engines/integrity.js +108 -0
- package/dist/engines/php-compat.d.ts +2 -0
- package/dist/engines/php-compat.js +26 -0
- package/dist/engines/php-lint.d.ts +6 -0
- package/dist/engines/php-lint.js +42 -0
- package/dist/engines/phpstan.d.ts +11 -0
- package/dist/engines/phpstan.js +103 -0
- package/dist/engines/stylelint.d.ts +4 -0
- package/dist/engines/stylelint.js +63 -0
- package/dist/engines/theme-check.d.ts +7 -0
- package/dist/engines/theme-check.js +109 -0
- package/dist/engines/theme-review.d.ts +24 -0
- package/dist/engines/theme-review.js +378 -0
- package/dist/engines/updates.d.ts +6 -0
- package/dist/engines/updates.js +78 -0
- package/dist/engines/vulnerabilities.d.ts +22 -0
- package/dist/engines/vulnerabilities.js +128 -0
- package/dist/engines/wp-json.d.ts +18 -0
- package/dist/engines/wp-json.js +134 -0
- package/dist/engines/wpcs.d.ts +7 -0
- package/dist/engines/wpcs.js +60 -0
- package/dist/exec.d.ts +12 -0
- package/dist/exec.js +19 -0
- package/dist/fingerprint.d.ts +10 -0
- package/dist/fingerprint.js +29 -0
- package/dist/health/install.d.ts +16 -0
- package/dist/health/install.js +68 -0
- package/dist/home.d.ts +2 -0
- package/dist/home.js +6 -0
- package/dist/hub.d.ts +32 -0
- package/dist/hub.js +99 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +36 -0
- package/dist/js/files.d.ts +4 -0
- package/dist/js/files.js +47 -0
- package/dist/js/tools.d.ts +3 -0
- package/dist/js/tools.js +67 -0
- package/dist/mcp/server.d.ts +9 -0
- package/dist/mcp/server.js +91 -0
- package/dist/mcp/tools.d.ts +26 -0
- package/dist/mcp/tools.js +163 -0
- package/dist/net.d.ts +18 -0
- package/dist/net.js +68 -0
- package/dist/php/files.d.ts +3 -0
- package/dist/php/files.js +53 -0
- package/dist/php/php.d.ts +4 -0
- package/dist/php/php.js +25 -0
- package/dist/php/phpcs.d.ts +25 -0
- package/dist/php/phpcs.js +55 -0
- package/dist/php/tools.d.ts +3 -0
- package/dist/php/tools.js +66 -0
- package/dist/profiles.d.ts +4 -0
- package/dist/profiles.js +41 -0
- package/dist/progress.d.ts +3 -0
- package/dist/progress.js +13 -0
- package/dist/projects.d.ts +36 -0
- package/dist/projects.js +91 -0
- package/dist/report/escape.d.ts +1 -0
- package/dist/report/escape.js +4 -0
- package/dist/report/html.d.ts +2 -0
- package/dist/report/html.js +100 -0
- package/dist/report/json.d.ts +2 -0
- package/dist/report/json.js +3 -0
- package/dist/report/markdown.d.ts +5 -0
- package/dist/report/markdown.js +92 -0
- package/dist/report/sarif.d.ts +2 -0
- package/dist/report/sarif.js +44 -0
- package/dist/report/write.d.ts +9 -0
- package/dist/report/write.js +48 -0
- package/dist/runner.d.ts +14 -0
- package/dist/runner.js +166 -0
- package/dist/session.d.ts +22 -0
- package/dist/session.js +39 -0
- package/dist/target.d.ts +5 -0
- package/dist/target.js +114 -0
- package/dist/types.d.ts +165 -0
- package/dist/types.js +6 -0
- package/dist/ui/server.d.ts +13 -0
- package/dist/ui/server.js +458 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +3 -0
- package/dist/versions.d.ts +9 -0
- package/dist/versions.js +35 -0
- package/js-tools/eslint.config.mjs +19 -0
- package/js-tools/package-lock.json +7004 -0
- package/js-tools/package.json +13 -0
- package/js-tools/stylelint.config.mjs +4 -0
- package/package.json +58 -4
- package/php-tools/composer.json +68 -0
- package/php-tools/composer.lock +889 -0
- package/php-tools/theme-check.php +239 -0
- package/ui/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/ui/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/ui/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/ui/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/ui/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/ui/dist/assets/index-DXwdz_Wu.css +2 -0
- package/ui/dist/assets/index-V8IOqxHa.js +54 -0
- package/ui/dist/index.html +14 -0
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { Ajv } from 'ajv';
|
|
5
|
+
import { home } from '../home.js';
|
|
6
|
+
import { DEPTHS, EngineSkipped } from '../types.js';
|
|
7
|
+
const esc = (s) => s.replace(/[.*+?^${}()|[\]\\\/]/g, '\\$&');
|
|
8
|
+
/** Turns a glob into a RegExp that matches a POSIX relative path. Supports **, *, ? and {a,b}. */
|
|
9
|
+
export function globToRegExp(glob) {
|
|
10
|
+
let out = '';
|
|
11
|
+
for (let i = 0; i < glob.length; i++) {
|
|
12
|
+
const c = glob[i];
|
|
13
|
+
if (c === '*') {
|
|
14
|
+
if (glob[i + 1] === '*') {
|
|
15
|
+
if (glob[i + 2] === '/') {
|
|
16
|
+
out += '(?:.*/)?';
|
|
17
|
+
i += 2;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
out += '.*';
|
|
21
|
+
i += 1;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else
|
|
25
|
+
out += '[^/]*';
|
|
26
|
+
}
|
|
27
|
+
else if (c === '?')
|
|
28
|
+
out += '[^/]';
|
|
29
|
+
else if (c === '{' && glob.indexOf('}', i) > i) {
|
|
30
|
+
const end = glob.indexOf('}', i);
|
|
31
|
+
out += `(?:${glob.slice(i + 1, end).split(',').map(esc).join('|')})`;
|
|
32
|
+
i = end;
|
|
33
|
+
}
|
|
34
|
+
else
|
|
35
|
+
out += esc(c);
|
|
36
|
+
}
|
|
37
|
+
return new RegExp(`^${out}$`);
|
|
38
|
+
}
|
|
39
|
+
const CATEGORIES = ['security', 'compatibility', 'standards', 'correctness', 'performance', 'accessibility', 'i18n', 'health', 'other'];
|
|
40
|
+
const strings = (extra = {}) => ({ type: 'array', items: { type: 'string' }, ...extra });
|
|
41
|
+
const globs = { oneOf: [{ type: 'string' }, strings({ minItems: 1 })] };
|
|
42
|
+
const common = {
|
|
43
|
+
title: { type: 'string' },
|
|
44
|
+
kinds: { type: 'array', items: { enum: ['theme', 'plugin', 'install'] }, minItems: 1 },
|
|
45
|
+
paths: strings({ minItems: 1 }),
|
|
46
|
+
depth: { enum: [...DEPTHS] },
|
|
47
|
+
severity: { enum: ['error', 'warning', 'info'] },
|
|
48
|
+
category: { enum: CATEGORIES },
|
|
49
|
+
message: { type: 'string' },
|
|
50
|
+
hint: { type: 'string' },
|
|
51
|
+
onError: { enum: ['fail', 'skip', 'warn'] },
|
|
52
|
+
};
|
|
53
|
+
const schemaFor = (type, properties, extra = {}) => ({
|
|
54
|
+
type: 'object',
|
|
55
|
+
properties: { type: { const: type }, ...common, ...properties },
|
|
56
|
+
required: ['type'],
|
|
57
|
+
additionalProperties: false,
|
|
58
|
+
...extra,
|
|
59
|
+
});
|
|
60
|
+
const ajv = new Ajv({ strict: false });
|
|
61
|
+
const validators = {
|
|
62
|
+
pattern: ajv.compile(schemaFor('pattern', { files: globs, forbid: { type: 'string' }, require: { type: 'string' }, flags: { type: 'string' } }, { required: ['type', 'files', 'message'], oneOf: [{ required: ['forbid'], not: { required: ['require'] } }, { required: ['require'], not: { required: ['forbid'] } }] })),
|
|
63
|
+
files: ajv.compile(schemaFor('files', { require: strings(), forbid: strings() }, { anyOf: [{ required: ['require'] }, { required: ['forbid'] }] })),
|
|
64
|
+
command: ajv.compile(schemaFor('command', { run: { type: 'string' }, expectExit: { type: 'integer' }, timeoutSeconds: { type: 'number', exclusiveMinimum: 0 } }, { required: ['type', 'run'] })),
|
|
65
|
+
none: ajv.compile({ type: 'object', properties: { type: { enum: ['pattern', 'files', 'command'] } }, required: ['type'] }),
|
|
66
|
+
};
|
|
67
|
+
/** The check itself cannot run. This is not a finding about the target. */
|
|
68
|
+
class CheckError extends Error {
|
|
69
|
+
}
|
|
70
|
+
const posix = (p) => p.split(path.sep).join('/');
|
|
71
|
+
const list = (v) => (v === undefined ? [] : Array.isArray(v) ? v : [v]);
|
|
72
|
+
const MAX_FILE = 2 * 1024 * 1024;
|
|
73
|
+
/** The files of the target as POSIX paths relative to its folder. */
|
|
74
|
+
export function walk(target) {
|
|
75
|
+
if (statSync(target.path).isFile())
|
|
76
|
+
return { root: path.dirname(target.path), files: [path.basename(target.path)] };
|
|
77
|
+
const files = [];
|
|
78
|
+
const visit = (dir) => {
|
|
79
|
+
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
|
80
|
+
if (e.name.startsWith('.'))
|
|
81
|
+
continue;
|
|
82
|
+
const full = path.join(dir, e.name);
|
|
83
|
+
if (e.isDirectory()) {
|
|
84
|
+
if (e.name !== 'node_modules' && e.name !== 'vendor')
|
|
85
|
+
visit(full);
|
|
86
|
+
}
|
|
87
|
+
else if (e.isFile())
|
|
88
|
+
files.push(posix(path.relative(target.path, full)));
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
visit(target.path);
|
|
92
|
+
return { root: target.path, files: files.sort() };
|
|
93
|
+
}
|
|
94
|
+
function runCommand(check, target, cwd, signal) {
|
|
95
|
+
const seconds = check.timeoutSeconds ?? 120;
|
|
96
|
+
return new Promise((resolve, reject) => {
|
|
97
|
+
let output = '';
|
|
98
|
+
let timedOut = false;
|
|
99
|
+
// The shell gets its own process group, so a stop reaches the commands the shell started and not only the shell.
|
|
100
|
+
const group = process.platform !== 'win32';
|
|
101
|
+
const child = spawn(check.run, { shell: true, cwd, env: { ...process.env, WP_SHAKEDOWN_TARGET: target.path }, detached: group });
|
|
102
|
+
const stop = () => {
|
|
103
|
+
try {
|
|
104
|
+
if (group && child.pid)
|
|
105
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
106
|
+
else
|
|
107
|
+
child.kill();
|
|
108
|
+
}
|
|
109
|
+
catch {
|
|
110
|
+
// It already ended.
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
const onAbort = () => {
|
|
114
|
+
stop();
|
|
115
|
+
reject(signal.reason instanceof Error ? signal.reason : new Error('The run was stopped.'));
|
|
116
|
+
};
|
|
117
|
+
if (signal?.aborted)
|
|
118
|
+
onAbort();
|
|
119
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
120
|
+
// Reject at the timeout without waiting for the pipes to close. A grandchild can keep them open.
|
|
121
|
+
const timer = setTimeout(() => {
|
|
122
|
+
timedOut = true;
|
|
123
|
+
stop();
|
|
124
|
+
reject(new CheckError(`The command did not finish in ${seconds} seconds.`));
|
|
125
|
+
}, seconds * 1000);
|
|
126
|
+
child.stdout.setEncoding('utf8').on('data', (d) => (output += d));
|
|
127
|
+
child.stderr.setEncoding('utf8').on('data', (d) => (output += d));
|
|
128
|
+
const done = () => {
|
|
129
|
+
clearTimeout(timer);
|
|
130
|
+
signal?.removeEventListener('abort', onAbort);
|
|
131
|
+
};
|
|
132
|
+
child.on('error', (err) => {
|
|
133
|
+
done();
|
|
134
|
+
reject(new CheckError(`The command could not start: ${err.message}.`));
|
|
135
|
+
});
|
|
136
|
+
child.on('close', (code) => {
|
|
137
|
+
done();
|
|
138
|
+
if (timedOut || signal?.aborted)
|
|
139
|
+
return;
|
|
140
|
+
const n = code ?? 1;
|
|
141
|
+
if (n === 126 || n === 127)
|
|
142
|
+
return reject(new CheckError(`The command was not found or could not run (exit code ${n}).`));
|
|
143
|
+
resolve({ code: n, output });
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
async function runCheck(name, check, target, signal) {
|
|
148
|
+
const title = check.title ?? name;
|
|
149
|
+
const make = (message, location, hint = check.hint) => ({
|
|
150
|
+
rule: name,
|
|
151
|
+
severity: check.severity ?? 'error',
|
|
152
|
+
category: check.category ?? 'other',
|
|
153
|
+
message,
|
|
154
|
+
location,
|
|
155
|
+
...(hint ? { hint } : {}),
|
|
156
|
+
});
|
|
157
|
+
if (check.type === 'command') {
|
|
158
|
+
const cwd = statSync(target.path).isFile() ? path.dirname(target.path) : target.path;
|
|
159
|
+
const { code, output } = await runCommand(check, target, cwd, signal);
|
|
160
|
+
if (code === (check.expectExit ?? 0))
|
|
161
|
+
return [];
|
|
162
|
+
const tail = output.split(/\r?\n/).filter((l) => l.trim() !== '').slice(-10).join('\n').slice(-1000);
|
|
163
|
+
const hint = check.hint ?? (tail || undefined);
|
|
164
|
+
return [make(check.message ?? `${title} failed with exit code ${code}.`, {}, hint)];
|
|
165
|
+
}
|
|
166
|
+
const { root, files } = walk(target);
|
|
167
|
+
if (check.type === 'files') {
|
|
168
|
+
const out = [];
|
|
169
|
+
for (const g of list(check.require)) {
|
|
170
|
+
const re = globToRegExp(g);
|
|
171
|
+
if (!files.some((f) => re.test(f)))
|
|
172
|
+
out.push(make(check.message ?? `Nothing matches "${g}".`, {}));
|
|
173
|
+
}
|
|
174
|
+
for (const g of list(check.forbid)) {
|
|
175
|
+
const re = globToRegExp(g);
|
|
176
|
+
for (const f of files)
|
|
177
|
+
if (re.test(f))
|
|
178
|
+
out.push(make(check.message ?? 'This file must not be here.', { file: f }));
|
|
179
|
+
}
|
|
180
|
+
return out;
|
|
181
|
+
}
|
|
182
|
+
let re;
|
|
183
|
+
try {
|
|
184
|
+
re = new RegExp((check.forbid ?? check.require), check.flags ?? '');
|
|
185
|
+
}
|
|
186
|
+
catch (err) {
|
|
187
|
+
throw new CheckError(`The pattern is not a valid regular expression: ${err instanceof Error ? err.message : String(err)}.`);
|
|
188
|
+
}
|
|
189
|
+
const wanted = list(check.files).map(globToRegExp);
|
|
190
|
+
const out = [];
|
|
191
|
+
let matched = false;
|
|
192
|
+
for (const f of files) {
|
|
193
|
+
if (!wanted.some((w) => w.test(f)))
|
|
194
|
+
continue;
|
|
195
|
+
const full = path.join(root, f);
|
|
196
|
+
if (statSync(full).size > MAX_FILE)
|
|
197
|
+
continue;
|
|
198
|
+
const text = readFileSync(full, 'utf8');
|
|
199
|
+
if (check.require !== undefined) {
|
|
200
|
+
re.lastIndex = 0;
|
|
201
|
+
if (re.test(text))
|
|
202
|
+
matched = true;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
text.split(/\r?\n/).forEach((line, i) => {
|
|
206
|
+
re.lastIndex = 0;
|
|
207
|
+
if (re.test(line))
|
|
208
|
+
out.push(make(check.message, { file: f, line: i + 1 }));
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
if (check.require !== undefined && !matched)
|
|
212
|
+
out.push(make(check.message, {}));
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
function failing(name, reason) {
|
|
216
|
+
return {
|
|
217
|
+
id: `custom:${name}`,
|
|
218
|
+
title: name,
|
|
219
|
+
kinds: ['theme', 'plugin', 'install'],
|
|
220
|
+
depth: 'quick',
|
|
221
|
+
async run() {
|
|
222
|
+
throw new Error(`The check file ${name}.json is not valid: ${reason}.`);
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
function toEngine(name, check) {
|
|
227
|
+
return {
|
|
228
|
+
id: `custom:${name}`,
|
|
229
|
+
title: check.title ?? name,
|
|
230
|
+
kinds: check.kinds ?? ['theme', 'plugin'],
|
|
231
|
+
depth: check.depth ?? 'standard',
|
|
232
|
+
async run({ target, signal }) {
|
|
233
|
+
try {
|
|
234
|
+
return await runCheck(name, check, target, signal);
|
|
235
|
+
}
|
|
236
|
+
catch (err) {
|
|
237
|
+
if (!(err instanceof CheckError))
|
|
238
|
+
throw err;
|
|
239
|
+
const how = check.onError ?? 'fail';
|
|
240
|
+
if (how === 'skip')
|
|
241
|
+
throw new EngineSkipped(err.message);
|
|
242
|
+
if (how === 'warn') {
|
|
243
|
+
return [{ rule: name, severity: 'warning', category: 'other', message: `The check could not run: ${err.message}`, location: {} }];
|
|
244
|
+
}
|
|
245
|
+
throw new Error(err.message);
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
/** The checks in <home>/checks as engines. With a target, only the checks whose `paths` fit it. */
|
|
251
|
+
export function customEngines(target) {
|
|
252
|
+
const dir = path.join(home(), 'checks');
|
|
253
|
+
if (!existsSync(dir))
|
|
254
|
+
return [];
|
|
255
|
+
const names = readdirSync(dir)
|
|
256
|
+
.filter((n) => /^[a-z0-9][a-z0-9-]*\.json$/.test(n))
|
|
257
|
+
.sort();
|
|
258
|
+
const out = [];
|
|
259
|
+
for (const file of names) {
|
|
260
|
+
const name = file.slice(0, -'.json'.length);
|
|
261
|
+
let data;
|
|
262
|
+
try {
|
|
263
|
+
data = JSON.parse(readFileSync(path.join(dir, file), 'utf8'));
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
out.push(failing(name, 'it is not JSON'));
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
const type = data?.type;
|
|
270
|
+
const validate = validators[typeof type === 'string' && type in validators && type !== 'none' ? type : 'none'];
|
|
271
|
+
if (!validate(data)) {
|
|
272
|
+
const e = validate.errors[0];
|
|
273
|
+
const unknown = e.keyword === 'additionalProperties' ? e.params.additionalProperty : undefined;
|
|
274
|
+
out.push(failing(name, unknown ? `it has a field the format does not know, "${unknown}"` : `${e.instancePath || 'the file'} ${e.message}`));
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
const check = data;
|
|
278
|
+
if (target && check.paths) {
|
|
279
|
+
const p = posix(target.path);
|
|
280
|
+
if (!check.paths.some((g) => globToRegExp(g).test(p)))
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
out.push(toEngine(name, check));
|
|
284
|
+
}
|
|
285
|
+
return out;
|
|
286
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Category, Engine, RawFinding } from '../types.ts';
|
|
2
|
+
export declare function categoriseJs(ruleId: string): Category;
|
|
3
|
+
/** The tools print real paths, so a base behind a symlink (macOS /var) is resolved first. */
|
|
4
|
+
export declare function posixRelative(base: string, file: string): string;
|
|
5
|
+
export declare function parseEslint(json: string, base: string): RawFinding[];
|
|
6
|
+
export declare const firstLine: (s: string) => string;
|
|
7
|
+
export declare function chunks<T>(list: T[], size: number): T[][];
|
|
8
|
+
export declare const eslint: Engine;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { realpathSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { exec } from '../exec.js';
|
|
4
|
+
import { isGenerated, sourceFiles } from '../js/files.js';
|
|
5
|
+
import { jsTools } from '../js/tools.js';
|
|
6
|
+
export function categoriseJs(ruleId) {
|
|
7
|
+
if (/^jsx-a11y\//.test(ruleId))
|
|
8
|
+
return 'accessibility';
|
|
9
|
+
if (/^@wordpress\/i18n-/.test(ruleId))
|
|
10
|
+
return 'i18n';
|
|
11
|
+
if (/^(prettier\/|jsdoc\/|import\/order$|@wordpress\/dependency-group$|no-var$|prefer-|object-shorthand$|camelcase$|curly$|dot-notation$|quote-props$|one-var$|no-else-return$|no-lonely-if$|no-nested-ternary$|no-unneeded-ternary$|no-useless-|spaced-comment$|yoda$|no-console$|no-multi-str$)/.test(ruleId))
|
|
12
|
+
return 'standards';
|
|
13
|
+
return 'correctness';
|
|
14
|
+
}
|
|
15
|
+
/** The tools print real paths, so a base behind a symlink (macOS /var) is resolved first. */
|
|
16
|
+
export function posixRelative(base, file) {
|
|
17
|
+
let rel = path.relative(base, file);
|
|
18
|
+
if (rel.startsWith('..')) {
|
|
19
|
+
try {
|
|
20
|
+
rel = path.relative(realpathSync(base), file);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// Keep the first answer.
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return rel.split(path.sep).join('/');
|
|
27
|
+
}
|
|
28
|
+
export function parseEslint(json, base) {
|
|
29
|
+
let data;
|
|
30
|
+
try {
|
|
31
|
+
data = JSON.parse(json);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
data = null;
|
|
35
|
+
}
|
|
36
|
+
if (!Array.isArray(data))
|
|
37
|
+
throw new Error('ESLint returned output that is not JSON.');
|
|
38
|
+
const out = [];
|
|
39
|
+
for (const f of data) {
|
|
40
|
+
for (const m of f.messages ?? []) {
|
|
41
|
+
const location = {
|
|
42
|
+
file: posixRelative(base, f.filePath),
|
|
43
|
+
...(m.line ? { line: m.line } : {}),
|
|
44
|
+
...(m.column ? { column: m.column } : {}),
|
|
45
|
+
};
|
|
46
|
+
if (m.fatal) {
|
|
47
|
+
out.push({ rule: 'syntax', severity: 'error', category: 'correctness', message: m.message, location });
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const rule = m.ruleId ?? 'eslint';
|
|
51
|
+
const category = categoriseJs(rule);
|
|
52
|
+
out.push({
|
|
53
|
+
rule,
|
|
54
|
+
severity: category === 'standards' ? 'info' : 'warning',
|
|
55
|
+
category,
|
|
56
|
+
message: m.message,
|
|
57
|
+
location,
|
|
58
|
+
...(m.fix ? { hint: 'eslint --fix can fix this.' } : {}),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
export const firstLine = (s) => s.split('\n').map((l) => l.trim()).find(Boolean) ?? '';
|
|
65
|
+
export function chunks(list, size) {
|
|
66
|
+
const out = [];
|
|
67
|
+
for (let i = 0; i < list.length; i += size)
|
|
68
|
+
out.push(list.slice(i, i + size));
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
export const eslint = {
|
|
72
|
+
id: 'eslint',
|
|
73
|
+
title: 'JavaScript',
|
|
74
|
+
kinds: ['theme', 'plugin'],
|
|
75
|
+
async run({ target, signal, note }) {
|
|
76
|
+
const files = sourceFiles(target.path, ['.js', '.jsx', '.mjs', '.cjs']).filter((f) => !isGenerated(f));
|
|
77
|
+
if (files.length === 0)
|
|
78
|
+
return [];
|
|
79
|
+
const tools = await jsTools(note);
|
|
80
|
+
const out = [];
|
|
81
|
+
for (const chunk of chunks(files, 200)) {
|
|
82
|
+
if (signal?.aborted)
|
|
83
|
+
break;
|
|
84
|
+
const r = await exec(process.execPath, [
|
|
85
|
+
path.join(tools, 'node_modules', 'eslint', 'bin', 'eslint.js'),
|
|
86
|
+
'--no-config-lookup', '-c', path.join(tools, 'eslint.config.mjs'),
|
|
87
|
+
'--format', 'json', '--no-error-on-unmatched-pattern', '--no-warn-ignored',
|
|
88
|
+
...chunk.map((f) => posixRelative(target.path, f)),
|
|
89
|
+
], { cwd: target.path, ...(signal ? { signal } : {}) });
|
|
90
|
+
if (r.code !== 0 && r.code !== 1)
|
|
91
|
+
throw new Error(`ESLint stopped: ${firstLine(r.stderr) || firstLine(r.stdout)}`);
|
|
92
|
+
out.push(...parseEslint(r.stdout, target.path));
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
},
|
|
96
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { config } from './config.js';
|
|
2
|
+
import { eslint } from './eslint.js';
|
|
3
|
+
import { integrity } from './integrity.js';
|
|
4
|
+
import { phpCompat } from './php-compat.js';
|
|
5
|
+
import { phpLint } from './php-lint.js';
|
|
6
|
+
import { phpstan } from './phpstan.js';
|
|
7
|
+
import { stylelint } from './stylelint.js';
|
|
8
|
+
import { themeCheck } from './theme-check.js';
|
|
9
|
+
import { themeReview } from './theme-review.js';
|
|
10
|
+
import { updates } from './updates.js';
|
|
11
|
+
import { vulnerabilities } from './vulnerabilities.js';
|
|
12
|
+
import { wpJson } from './wp-json.js';
|
|
13
|
+
import { wpcs } from './wpcs.js';
|
|
14
|
+
export const engines = [phpLint, themeCheck, themeReview, wpcs, phpCompat, phpstan, wpJson, eslint, stylelint, integrity, updates, vulnerabilities, config];
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { coreVersion, packages, skipOnNetwork } from '../health/install.js';
|
|
5
|
+
import { getJson } from '../net.js';
|
|
6
|
+
import { EngineSkipped } from '../types.js';
|
|
7
|
+
const MONTH = 30 * 24 * 3600 * 1000;
|
|
8
|
+
const md5 = (file) => createHash('md5').update(readFileSync(file)).digest('hex');
|
|
9
|
+
const rec = (v) => (v && typeof v === 'object' ? v : undefined);
|
|
10
|
+
function walk(root, dir, out) {
|
|
11
|
+
let entries;
|
|
12
|
+
try {
|
|
13
|
+
entries = readdirSync(path.join(root, dir), { withFileTypes: true });
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
for (const e of entries.sort((a, b) => (a.name < b.name ? -1 : 1))) {
|
|
19
|
+
const rel = `${dir}/${e.name}`;
|
|
20
|
+
if (e.isDirectory())
|
|
21
|
+
walk(root, rel, out);
|
|
22
|
+
else if (e.isFile() && e.name !== '.DS_Store')
|
|
23
|
+
out.push(rel);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export const integrity = {
|
|
27
|
+
id: 'integrity',
|
|
28
|
+
title: 'File integrity',
|
|
29
|
+
kinds: ['install'],
|
|
30
|
+
run({ target, signal, note }) {
|
|
31
|
+
return skipOnNetwork(async () => {
|
|
32
|
+
const root = target.path;
|
|
33
|
+
const out = [];
|
|
34
|
+
const cv = coreVersion(root);
|
|
35
|
+
if (!cv)
|
|
36
|
+
throw new EngineSkipped('Cannot read the WordPress version from wp-includes/version.php.');
|
|
37
|
+
const url = (locale) => `https://api.wordpress.org/core/checksums/1.0/?version=${cv.version}&locale=${locale}`;
|
|
38
|
+
let r = await getJson(url(cv.locale), { ttlMs: MONTH, ...(signal ? { signal } : {}) });
|
|
39
|
+
let sums = rec(rec(r.data)?.checksums);
|
|
40
|
+
if (!sums && cv.locale !== 'en_US') {
|
|
41
|
+
r = await getJson(url('en_US'), { ttlMs: MONTH, ...(signal ? { signal } : {}) });
|
|
42
|
+
sums = rec(rec(r.data)?.checksums);
|
|
43
|
+
}
|
|
44
|
+
if (!sums) {
|
|
45
|
+
note(`wordpress.org has no checksums for WordPress ${cv.version}. Core files were not compared.`);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
for (const [file, sum] of Object.entries(sums)) {
|
|
49
|
+
if (file.startsWith('wp-content/'))
|
|
50
|
+
continue;
|
|
51
|
+
const abs = path.join(root, file);
|
|
52
|
+
if (!existsSync(abs)) {
|
|
53
|
+
out.push({ rule: 'core.missing', severity: 'warning', category: 'health', message: 'A WordPress core file is missing.', location: { file } });
|
|
54
|
+
}
|
|
55
|
+
else if (md5(abs) !== sum) {
|
|
56
|
+
out.push({
|
|
57
|
+
rule: 'core.modified', severity: 'error', category: 'security',
|
|
58
|
+
message: 'This WordPress core file differs from the official release.',
|
|
59
|
+
hint: 'Reinstall WordPress core, or compare the file with the official release.', location: { file },
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const found = [];
|
|
64
|
+
walk(root, 'wp-admin', found);
|
|
65
|
+
walk(root, 'wp-includes', found);
|
|
66
|
+
for (const file of found) {
|
|
67
|
+
if (Object.hasOwn(sums, file))
|
|
68
|
+
continue;
|
|
69
|
+
out.push({
|
|
70
|
+
rule: 'core.unexpected', severity: 'warning', category: 'security', message: 'This file is not part of WordPress core.',
|
|
71
|
+
hint: 'The core folders hold only core files. Find out what put it there.', location: { file },
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
let skipped = 0;
|
|
76
|
+
for (const pkg of packages(target)) {
|
|
77
|
+
if (signal?.aborted)
|
|
78
|
+
break;
|
|
79
|
+
if (pkg.kind !== 'plugin' || !pkg.version)
|
|
80
|
+
continue;
|
|
81
|
+
if (statSync(pkg.path).isFile())
|
|
82
|
+
continue;
|
|
83
|
+
const pr = await getJson(`https://downloads.wordpress.org/plugin-checksums/${pkg.slug}/${pkg.version}.json`, { ttlMs: MONTH, ...(signal ? { signal } : {}) });
|
|
84
|
+
const files = pr.status === 200 ? rec(rec(pr.data)?.files) : undefined;
|
|
85
|
+
if (!files) {
|
|
86
|
+
skipped++;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
for (const [rel, info] of Object.entries(files)) {
|
|
90
|
+
const abs = path.join(pkg.path, rel);
|
|
91
|
+
const file = path.relative(root, abs).split(path.sep).join('/');
|
|
92
|
+
if (!existsSync(abs)) {
|
|
93
|
+
out.push({ rule: 'plugin.missing', severity: 'warning', category: 'health', message: `A file of ${pkg.name} ${pkg.version} is missing.`, location: { file } });
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
const want = rec(info)?.md5;
|
|
97
|
+
const list = Array.isArray(want) ? want : [want];
|
|
98
|
+
if (!list.includes(md5(abs))) {
|
|
99
|
+
out.push({ rule: 'plugin.modified', severity: 'error', category: 'security', message: `This file differs from the wordpress.org release of ${pkg.name} ${pkg.version}.`, location: { file } });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (skipped)
|
|
104
|
+
note(`${skipped} ${skipped === 1 ? 'plugin has' : 'plugins have'} no checksums on wordpress.org and ${skipped === 1 ? 'was' : 'were'} not compared.`);
|
|
105
|
+
return out;
|
|
106
|
+
});
|
|
107
|
+
},
|
|
108
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { requiresPhp } from '../php/files.js';
|
|
2
|
+
import { runPhpcs } from '../php/phpcs.js';
|
|
3
|
+
export const phpCompat = {
|
|
4
|
+
id: 'php-compat',
|
|
5
|
+
title: 'PHP compatibility',
|
|
6
|
+
kinds: ['theme', 'plugin'],
|
|
7
|
+
async run({ target, signal, note }) {
|
|
8
|
+
const declared = requiresPhp(target.path);
|
|
9
|
+
const min = declared ?? '7.4';
|
|
10
|
+
note(declared ? `Checking against PHP ${min} and newer.` : 'No "Requires PHP" header found. Checking against PHP 7.4 and newer.');
|
|
11
|
+
const messages = await runPhpcs({
|
|
12
|
+
target,
|
|
13
|
+
standard: 'PHPCompatibilityWP',
|
|
14
|
+
extra: ['--runtime-set', 'testVersion', `${min}-`],
|
|
15
|
+
...(signal ? { signal } : {}),
|
|
16
|
+
note,
|
|
17
|
+
});
|
|
18
|
+
return messages.map((m) => ({
|
|
19
|
+
rule: m.source,
|
|
20
|
+
severity: m.type === 'ERROR' ? 'error' : 'warning',
|
|
21
|
+
category: 'compatibility',
|
|
22
|
+
message: m.message,
|
|
23
|
+
location: { file: m.file, line: m.line, column: m.column },
|
|
24
|
+
}));
|
|
25
|
+
},
|
|
26
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { statSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { exec } from '../exec.js';
|
|
4
|
+
import { phpFiles } from '../php/files.js';
|
|
5
|
+
import { phpBinary, phpVersion } from '../php/php.js';
|
|
6
|
+
export function parseLint(output) {
|
|
7
|
+
const m = /(?:PHP )?(?:Parse|Fatal) error:\s+(.+?) in .+ on line (\d+)/.exec(output);
|
|
8
|
+
return m ? { message: m[1], line: Number(m[2]) } : undefined;
|
|
9
|
+
}
|
|
10
|
+
export const phpLint = {
|
|
11
|
+
id: 'php-lint',
|
|
12
|
+
title: 'PHP syntax',
|
|
13
|
+
kinds: ['theme', 'plugin'],
|
|
14
|
+
depth: 'quick',
|
|
15
|
+
async run({ target, signal, note }) {
|
|
16
|
+
note(`Checking syntax with PHP ${await phpVersion()}.`);
|
|
17
|
+
const base = statSync(target.path).isFile() ? path.dirname(target.path) : target.path;
|
|
18
|
+
const files = phpFiles(target.path);
|
|
19
|
+
const findings = [];
|
|
20
|
+
let next = 0;
|
|
21
|
+
const worker = async () => {
|
|
22
|
+
while (next < files.length && !signal?.aborted) {
|
|
23
|
+
const file = files[next++];
|
|
24
|
+
const r = await exec(phpBinary(), ['-d', 'display_errors=1', '-d', 'log_errors=0', '-l', file], { ...(signal ? { signal } : {}) });
|
|
25
|
+
if (r.code === 0)
|
|
26
|
+
continue;
|
|
27
|
+
const out = `${r.stdout}\n${r.stderr}`;
|
|
28
|
+
const parsed = parseLint(out);
|
|
29
|
+
const first = out.split('\n').map((l) => l.trim()).find(Boolean);
|
|
30
|
+
findings.push({
|
|
31
|
+
rule: 'syntax',
|
|
32
|
+
severity: 'error',
|
|
33
|
+
category: 'correctness',
|
|
34
|
+
message: parsed?.message ?? first ?? 'PHP could not parse this file.',
|
|
35
|
+
location: { file: path.relative(base, file).split(path.sep).join('/'), ...(parsed?.line ? { line: parsed.line } : {}) },
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
await Promise.all(Array.from({ length: Math.min(8, files.length) }, worker));
|
|
40
|
+
return findings.sort((a, b) => (a.location.file < b.location.file ? -1 : a.location.file > b.location.file ? 1 : 0));
|
|
41
|
+
},
|
|
42
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Engine, RawFinding } from '../types.ts';
|
|
2
|
+
export declare function neon(opts: {
|
|
3
|
+
tools: string;
|
|
4
|
+
target: string;
|
|
5
|
+
isFile: boolean;
|
|
6
|
+
tmpDir: string;
|
|
7
|
+
vendor?: string;
|
|
8
|
+
exclude: string[];
|
|
9
|
+
}): string;
|
|
10
|
+
export declare function parsePhpstan(json: string, base: string): RawFinding[];
|
|
11
|
+
export declare const phpstan: Engine;
|