red-design-mcp 0.1.0 → 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/README.md +45 -11
- package/dist/a11y.js +20 -7
- package/dist/agents.js +24 -0
- package/dist/cli.js +108 -28
- package/dist/colorparse.js +277 -0
- package/dist/csp.js +242 -0
- package/dist/cssparse.js +163 -50
- package/dist/gateway.js +7 -0
- package/dist/htmlscan.js +159 -0
- package/dist/playbooks.js +9 -0
- package/dist/secrets.js +113 -0
- package/dist/security.js +271 -0
- package/dist/server.js +166 -44
- package/dist/toolsets.js +4 -3
- package/dist/vision.js +70 -13
- package/dist/web.js +213 -3
- package/package.json +9 -1
package/dist/cssparse.js
CHANGED
|
@@ -1,59 +1,170 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parseColor } from "./colorparse.js";
|
|
2
|
+
import { rgbToHex, rgbToHsl } from "./color.js";
|
|
2
3
|
export const stripComments = (css) => css.replace(/\/\*[\s\S]*?\*\//g, "");
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
const MAX_DEPTH = 64;
|
|
5
|
+
/**
|
|
6
|
+
* A small CSS tokenizer-parser. It understands strings, url(), comments, nested rules (CSS nesting),
|
|
7
|
+
* at-rule blocks and custom properties whose values contain braces, which a flat regex cannot.
|
|
8
|
+
*/
|
|
9
|
+
function parseBlock(css, start, depth) {
|
|
10
|
+
if (depth > MAX_DEPTH)
|
|
11
|
+
throw new Error("CSS is nested too deeply to analyze.");
|
|
12
|
+
const decls = [];
|
|
13
|
+
const children = [];
|
|
14
|
+
const n = css.length;
|
|
15
|
+
let i = start;
|
|
16
|
+
let buf = "";
|
|
17
|
+
const pushDecl = () => {
|
|
18
|
+
const t = buf.trim();
|
|
19
|
+
buf = "";
|
|
20
|
+
if (!t || t.startsWith("@"))
|
|
21
|
+
return;
|
|
22
|
+
const c = t.indexOf(":");
|
|
23
|
+
if (c < 1)
|
|
24
|
+
return;
|
|
25
|
+
const prop = t.slice(0, c).trim().toLowerCase();
|
|
26
|
+
if (!/^(--[\w-]+|-?[a-z][\w-]*)$/.test(prop))
|
|
27
|
+
return;
|
|
28
|
+
decls.push([prop, t.slice(c + 1).replace(/!important/gi, "").trim()]);
|
|
29
|
+
};
|
|
30
|
+
while (i < n) {
|
|
31
|
+
const ch = css[i];
|
|
32
|
+
if (ch === "/" && css[i + 1] === "*") {
|
|
33
|
+
const e = css.indexOf("*/", i + 2);
|
|
34
|
+
i = e === -1 ? n : e + 2;
|
|
11
35
|
continue;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
36
|
+
}
|
|
37
|
+
if (ch === '"' || ch === "'") {
|
|
38
|
+
let j = i + 1;
|
|
39
|
+
while (j < n && css[j] !== ch) {
|
|
40
|
+
if (css[j] === "\\")
|
|
41
|
+
j++;
|
|
42
|
+
j++;
|
|
43
|
+
}
|
|
44
|
+
buf += css.slice(i, j + 1);
|
|
45
|
+
i = j + 1;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if ((ch === "u" || ch === "U") && css.slice(i, i + 4).toLowerCase() === "url(" && (i === 0 || !/[\w-]/.test(css[i - 1]))) {
|
|
49
|
+
let j = i + 4;
|
|
50
|
+
while (j < n && /\s/.test(css[j]))
|
|
51
|
+
j++;
|
|
52
|
+
if (css[j] === '"' || css[j] === "'") {
|
|
53
|
+
const q = css[j++];
|
|
54
|
+
while (j < n && css[j] !== q) {
|
|
55
|
+
if (css[j] === "\\")
|
|
56
|
+
j++;
|
|
57
|
+
j++;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
while (j < n && css[j] !== ")")
|
|
61
|
+
j++;
|
|
62
|
+
buf += "url()"; // contents (data URLs, paths) are irrelevant and may contain braces or colors
|
|
63
|
+
i = j + 1;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (ch === "{") {
|
|
67
|
+
const pre = buf.trim();
|
|
68
|
+
if (/^--[\w-]+\s*:$/.test(pre)) {
|
|
69
|
+
let d = 1;
|
|
70
|
+
let j = i + 1;
|
|
71
|
+
while (j < n && d > 0) {
|
|
72
|
+
if (css[j] === "{")
|
|
73
|
+
d++;
|
|
74
|
+
else if (css[j] === "}")
|
|
75
|
+
d--;
|
|
76
|
+
j++;
|
|
77
|
+
}
|
|
78
|
+
decls.push([pre.slice(0, pre.indexOf(":")).trim().toLowerCase(), css.slice(i, j).trim()]);
|
|
79
|
+
buf = "";
|
|
80
|
+
i = j;
|
|
17
81
|
continue;
|
|
18
|
-
|
|
82
|
+
}
|
|
83
|
+
const inner = parseBlock(css, i + 1, depth + 1);
|
|
84
|
+
children.push({ prelude: pre, decls: inner.decls, children: inner.children });
|
|
85
|
+
buf = "";
|
|
86
|
+
i = inner.end + 1;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (ch === "}") {
|
|
90
|
+
pushDecl();
|
|
91
|
+
return { decls, children, end: i };
|
|
19
92
|
}
|
|
20
|
-
|
|
93
|
+
if (ch === ";") {
|
|
94
|
+
pushDecl();
|
|
95
|
+
i++;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
buf += ch;
|
|
99
|
+
i++;
|
|
21
100
|
}
|
|
22
|
-
|
|
101
|
+
pushDecl();
|
|
102
|
+
return { decls, children, end: n };
|
|
23
103
|
}
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
const HSL = /hsla?\(\s*([\d.]+)(?:deg)?\s*[, ]\s*([\d.]+)%\s*[, ]\s*([\d.]+)%\s*(?:[,/]\s*([\d.]+%?)\s*)?\)/gi;
|
|
27
|
-
const opaque = (a) => {
|
|
28
|
-
if (a === undefined)
|
|
29
|
-
return true;
|
|
30
|
-
const v = a.endsWith("%") ? parseFloat(a) / 100 : parseFloat(a);
|
|
31
|
-
return v >= 1;
|
|
32
|
-
};
|
|
33
|
-
/** Opaque color literals in a CSS value, as normalized #rrggbb. Translucent colors are skipped. */
|
|
34
|
-
export function colorsIn(value) {
|
|
104
|
+
const GROUPING_AT_RULES = new Set(["media", "supports", "layer", "container", "scope", "document", "starting-style"]);
|
|
105
|
+
function splitTopLevel(s) {
|
|
35
106
|
const out = [];
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
107
|
+
let depth = 0;
|
|
108
|
+
let cur = "";
|
|
109
|
+
for (const ch of s) {
|
|
110
|
+
if (ch === "(" || ch === "[")
|
|
111
|
+
depth++;
|
|
112
|
+
else if (ch === ")" || ch === "]")
|
|
113
|
+
depth--;
|
|
114
|
+
if (ch === "," && depth === 0) {
|
|
115
|
+
out.push(cur.trim());
|
|
116
|
+
cur = "";
|
|
43
117
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
out.push(hex);
|
|
118
|
+
else
|
|
119
|
+
cur += ch;
|
|
47
120
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
121
|
+
if (cur.trim())
|
|
122
|
+
out.push(cur.trim());
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
function flatten(nodes, parent, ctx, out) {
|
|
126
|
+
for (const node of nodes) {
|
|
127
|
+
if (node.prelude.startsWith("@")) {
|
|
128
|
+
const name = node.prelude.slice(1).split(/[\s(]/)[0].toLowerCase();
|
|
129
|
+
if (!GROUPING_AT_RULES.has(name))
|
|
130
|
+
continue; // @keyframes, @font-face, @property ... are not style rules
|
|
131
|
+
const next = [...ctx, node.prelude];
|
|
132
|
+
if (node.decls.length && parent)
|
|
133
|
+
out.push({ selector: parent.join(", "), decls: node.decls, context: next });
|
|
134
|
+
flatten(node.children, parent, next, out);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
const own = splitTopLevel(node.prelude);
|
|
138
|
+
const sels = parent ? parent.flatMap((p) => own.map((c) => (c.includes("&") ? c.replace(/&/g, p) : `${p} ${c}`))) : own;
|
|
139
|
+
if (node.decls.length)
|
|
140
|
+
out.push({ selector: sels.join(", "), decls: node.decls, context: ctx });
|
|
141
|
+
flatten(node.children, sels, ctx, out);
|
|
142
|
+
}
|
|
52
143
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
144
|
+
}
|
|
145
|
+
export function parseRules(css) {
|
|
146
|
+
const nodes = [];
|
|
147
|
+
let i = 0;
|
|
148
|
+
while (i < css.length) {
|
|
149
|
+
const r = parseBlock(css, i, 0);
|
|
150
|
+
nodes.push(...r.children);
|
|
151
|
+
i = r.end + 1; // continue past a stray closing brace
|
|
152
|
+
}
|
|
153
|
+
const out = [];
|
|
154
|
+
flatten(nodes, null, [], out);
|
|
155
|
+
return out;
|
|
156
|
+
}
|
|
157
|
+
const HEX_RE = /#([0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{4}|[0-9a-f]{3})(?![0-9a-z_-])/gi;
|
|
158
|
+
const FUNC_RE = /(?<![\w-])(?:rgba?|hsla?|hwb|oklch|oklab)\([^()]*\)/gi;
|
|
159
|
+
/** Opaque color literals in a CSS value, as normalized #rrggbb. Translucent colors are skipped. */
|
|
160
|
+
export function colorsIn(value) {
|
|
161
|
+
const out = [];
|
|
162
|
+
for (const re of [HEX_RE, FUNC_RE]) {
|
|
163
|
+
for (const m of value.matchAll(re)) {
|
|
164
|
+
const c = parseColor(m[0]);
|
|
165
|
+
if (c && c.alpha >= 1)
|
|
166
|
+
out.push(rgbToHex(c.rgb));
|
|
167
|
+
}
|
|
57
168
|
}
|
|
58
169
|
return out;
|
|
59
170
|
}
|
|
@@ -89,16 +200,18 @@ export function extractCssTokens(css) {
|
|
|
89
200
|
const sels = r.selector.split(",").map((s) => s.trim());
|
|
90
201
|
const isRoot = sels.some((s) => [":root", "html", "body", "*"].includes(s));
|
|
91
202
|
const isHeading = sels.some((s) => /^h[1-6]$/i.test(s));
|
|
203
|
+
const dark = (r.context ?? []).some((c) => /prefers-color-scheme\s*:\s*dark/i.test(c));
|
|
92
204
|
for (const [prop, val] of r.decls) {
|
|
93
205
|
const cs = colorsIn(val);
|
|
94
|
-
|
|
95
|
-
|
|
206
|
+
// Dark-mode overrides still count as used colors, but must not replace the light-mode tokens.
|
|
207
|
+
if (!dark && prop.startsWith("--") && cs.length === 1 && /^(#|rgb|hsl|hwb|oklch|oklab)/i.test(val)) {
|
|
208
|
+
named[prop.slice(2).toLowerCase().replace(/[^a-z0-9-]+/g, "-")] ??= cs[0];
|
|
96
209
|
}
|
|
97
210
|
for (const c of cs)
|
|
98
211
|
bump(colorCount, c);
|
|
99
|
-
if (isRoot && (prop === "background" || prop === "background-color"))
|
|
212
|
+
if (!dark && isRoot && (prop === "background" || prop === "background-color"))
|
|
100
213
|
bodyBg ??= cs[0];
|
|
101
|
-
if (isRoot && prop === "color")
|
|
214
|
+
if (!dark && isRoot && prop === "color")
|
|
102
215
|
bodyText ??= cs[0];
|
|
103
216
|
if (prop === "font-family") {
|
|
104
217
|
const f = firstFamily(val);
|
package/dist/gateway.js
CHANGED
|
@@ -97,6 +97,13 @@ const SYNONYMS = {
|
|
|
97
97
|
redesign: ["reimagine"], restyle: ["reimagine"], reimagine: ["reimagine", "style"], style: ["reimagine"], remix: ["reimagine"], variation: ["reimagine"],
|
|
98
98
|
analyze: ["analyze", "spec"], inspect: ["analyze"], describe: ["analyze"],
|
|
99
99
|
system: ["design", "system"], starter: ["system"], scale: ["scale"], ratio: ["type", "scale"],
|
|
100
|
+
check: ["audit"], verify: ["audit"], validate: ["audit"], grade: ["audit"], write: ["generate"], create: ["generate"], build: ["generate"], config: ["generate"], configure: ["generate"], snippet: ["generate"],
|
|
101
|
+
security: ["security"], secure: ["security", "audit"], safe: ["security"], vulnerability: ["security"], harden: ["security", "header"], hardening: ["security", "header"],
|
|
102
|
+
csp: ["csp", "policy", "security"], policy: ["csp"], header: ["header", "security"], hsts: ["header", "security"], cookie: ["security", "cookie"],
|
|
103
|
+
xss: ["csp", "security"], clickjacking: ["csp", "header", "security"], owasp: ["security"], cors: ["security", "header"], sri: ["security"], integrity: ["security"],
|
|
104
|
+
secret: ["secret", "scan"], leak: ["secret", "scan"], leaked: ["secret", "scan"], credential: ["secret", "scan"], password: ["secret", "scan"], apikey: ["secret", "scan"],
|
|
105
|
+
privacy: ["privacy", "tracker", "security"], tracker: ["privacy", "security"], gdpr: ["privacy", "tracker", "security"], mixed: ["security"], nginx: ["security", "header"], apache: ["security", "header"], netlify: ["security", "header"], vercel: ["security", "header"], caddy: ["security", "header"], helmet: ["security", "header"],
|
|
106
|
+
seo: ["html", "audit"], markup: ["html", "audit"], semantic: ["html", "audit"], landmark: ["html", "audit"], alt: ["html", "audit", "accessibility"],
|
|
100
107
|
};
|
|
101
108
|
function expand(tokens) {
|
|
102
109
|
const out = new Set(tokens);
|
package/dist/htmlscan.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/** A small, forgiving HTML tokenizer plus an accessibility and quality audit built on it. */
|
|
2
|
+
const TAG_RE = /<([a-zA-Z][a-zA-Z0-9:-]*)((?:\s+[^\s"'<>\/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'=<>`]+))?)*)\s*\/?>/g;
|
|
3
|
+
const ATTR_RE = /([^\s"'<>\/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
|
|
4
|
+
export const decodeEntities = (s) => s.replace(/&/gi, "&").replace(/"/gi, '"').replace(/�*39;|'/gi, "'").replace(/</gi, "<").replace(/>/gi, ">").replace(/ /gi, " ");
|
|
5
|
+
function parseAttrs(raw) {
|
|
6
|
+
const attrs = {};
|
|
7
|
+
for (const m of raw.matchAll(ATTR_RE)) {
|
|
8
|
+
const name = m[1].toLowerCase();
|
|
9
|
+
if (!(name in attrs))
|
|
10
|
+
attrs[name] = decodeEntities(m[2] ?? m[3] ?? m[4] ?? "");
|
|
11
|
+
}
|
|
12
|
+
return attrs;
|
|
13
|
+
}
|
|
14
|
+
export function parseHtml(html) {
|
|
15
|
+
let masked = html.replace(/<!--[\s\S]*?-->/g, (m) => " ".repeat(m.length));
|
|
16
|
+
const scripts = [];
|
|
17
|
+
const styles = [];
|
|
18
|
+
masked = masked.replace(/<(script|style)\b((?:[^>"']|"[^"]*"|'[^']*')*)>([\s\S]*?)<\/\1\s*>/gi, (m, name, rawAttrs, content) => {
|
|
19
|
+
(name.toLowerCase() === "script" ? scripts : styles).push({ attrs: parseAttrs(rawAttrs), content });
|
|
20
|
+
return `<${name}${rawAttrs}>${" ".repeat(content.length)}</${name}>`;
|
|
21
|
+
});
|
|
22
|
+
const tags = [];
|
|
23
|
+
for (const m of masked.matchAll(TAG_RE)) {
|
|
24
|
+
tags.push({ name: m[1].toLowerCase(), attrs: parseAttrs(m[2] ?? ""), index: m.index ?? 0, end: (m.index ?? 0) + m[0].length });
|
|
25
|
+
}
|
|
26
|
+
return { masked, tags, scripts, styles };
|
|
27
|
+
}
|
|
28
|
+
/** Inner HTML of the element that starts at `tag` (up to its first closing tag). */
|
|
29
|
+
export function innerHtml(p, tag) {
|
|
30
|
+
const re = new RegExp(`</${tag.name}\\s*>`, "gi");
|
|
31
|
+
re.lastIndex = tag.end;
|
|
32
|
+
const m = re.exec(p.masked);
|
|
33
|
+
return m ? p.masked.slice(tag.end, m.index) : "";
|
|
34
|
+
}
|
|
35
|
+
export const stripTags = (s) => decodeEntities(s.replace(/<[^>]*>/g, " ")).replace(/\s+/g, " ").trim();
|
|
36
|
+
const GENERIC_LINK = /^(click here|here|read more|more|learn more|link|this|details|continue)$/i;
|
|
37
|
+
const SKIP_LABEL_TYPES = new Set(["hidden", "submit", "button", "reset", "image"]);
|
|
38
|
+
export function auditHtml(html) {
|
|
39
|
+
const p = parseHtml(html);
|
|
40
|
+
const findings = [];
|
|
41
|
+
const add = (sev, area, msg, fix) => findings.push({ sev, area, msg, fix });
|
|
42
|
+
const named = (n) => p.tags.filter((t) => t.name === n);
|
|
43
|
+
const fragment = !named("html").length && !named("head").length;
|
|
44
|
+
if (!fragment) {
|
|
45
|
+
const htmlTag = named("html")[0];
|
|
46
|
+
if (!htmlTag?.attrs.lang?.trim())
|
|
47
|
+
add("high", "accessibility", "The <html> element has no lang attribute, so screen readers guess the language.", 'Add lang, for example <html lang="en"> (WCAG 3.1.1).');
|
|
48
|
+
const title = named("title")[0];
|
|
49
|
+
if (!title || !stripTags(innerHtml(p, title)))
|
|
50
|
+
add("medium", "semantics", "The page has no <title>, or it is empty.", "Add a short, unique <title> that says what the page is.");
|
|
51
|
+
const viewport = named("meta").find((t) => t.attrs.name?.toLowerCase() === "viewport");
|
|
52
|
+
if (!viewport)
|
|
53
|
+
add("medium", "semantics", "No viewport meta tag, so phones render the page at desktop width.", 'Add <meta name="viewport" content="width=device-width, initial-scale=1">.');
|
|
54
|
+
else {
|
|
55
|
+
const c = (viewport.attrs.content ?? "").toLowerCase();
|
|
56
|
+
const maxScale = /maximum-scale\s*=\s*([\d.]+)/.exec(c);
|
|
57
|
+
if (/user-scalable\s*=\s*(no|0)/.test(c) || (maxScale && parseFloat(maxScale[1]) < 2)) {
|
|
58
|
+
add("high", "accessibility", "The viewport blocks pinch-zoom, which low-vision users rely on.", "Remove user-scalable=no and any maximum-scale below 2 (WCAG 1.4.4).");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (!named("main").length && !p.tags.some((t) => t.attrs.role === "main"))
|
|
62
|
+
add("low", "semantics", "No <main> landmark, so keyboard and screen-reader users cannot jump to the content.", "Wrap the primary content in <main>.");
|
|
63
|
+
}
|
|
64
|
+
const images = named("img");
|
|
65
|
+
const noAlt = images.filter((t) => !("alt" in t.attrs) && t.attrs.role !== "presentation");
|
|
66
|
+
if (noAlt.length)
|
|
67
|
+
add("high", "accessibility", `${noAlt.length} image${noAlt.length > 1 ? "s have" : " has"} no alt attribute.`, 'Describe meaningful images in alt, and use alt="" for decorative ones (WCAG 1.1.1).');
|
|
68
|
+
const noSize = images.filter((t) => !t.attrs.width || !t.attrs.height);
|
|
69
|
+
if (noSize.length)
|
|
70
|
+
add("low", "performance", `${noSize.length} image${noSize.length > 1 ? "s lack" : " lacks"} width and height, which causes layout shift while loading.`, "Set width and height attributes (or CSS aspect-ratio) on images.");
|
|
71
|
+
// Form controls need a name: a <label for>, a wrapping <label>, or an aria attribute.
|
|
72
|
+
const labels = named("label");
|
|
73
|
+
const labelFor = new Set(labels.map((l) => l.attrs.for).filter(Boolean));
|
|
74
|
+
const labelRanges = labels.map((l) => [l.end, l.end + innerHtml(p, l).length]);
|
|
75
|
+
const unlabeled = p.tags.filter((t) => {
|
|
76
|
+
if (!["input", "select", "textarea"].includes(t.name))
|
|
77
|
+
return false;
|
|
78
|
+
if (t.name === "input" && SKIP_LABEL_TYPES.has((t.attrs.type ?? "text").toLowerCase()))
|
|
79
|
+
return false;
|
|
80
|
+
if (t.attrs["aria-label"]?.trim() || t.attrs["aria-labelledby"] || t.attrs.title?.trim())
|
|
81
|
+
return false;
|
|
82
|
+
if (t.attrs.id && labelFor.has(t.attrs.id))
|
|
83
|
+
return false;
|
|
84
|
+
return !labelRanges.some(([s, e]) => t.index >= s && t.index <= e);
|
|
85
|
+
});
|
|
86
|
+
if (unlabeled.length)
|
|
87
|
+
add("high", "accessibility", `${unlabeled.length} form field${unlabeled.length > 1 ? "s have" : " has"} no label.`, "Connect each field to a visible <label for> (placeholders are not labels). WCAG 1.3.1 and 3.3.2.");
|
|
88
|
+
const emptyButtons = named("button").filter((t) => {
|
|
89
|
+
const inner = innerHtml(p, t);
|
|
90
|
+
return !stripTags(inner) && !/<img[^>]+alt\s*=\s*["'][^"']+["']/i.test(inner) && !/<title>[^<]+<\/title>/i.test(inner) && !t.attrs["aria-label"]?.trim() && !t.attrs["aria-labelledby"] && !t.attrs.title?.trim();
|
|
91
|
+
});
|
|
92
|
+
if (emptyButtons.length)
|
|
93
|
+
add("high", "accessibility", `${emptyButtons.length} button${emptyButtons.length > 1 ? "s have" : " has"} no accessible name (for example an icon-only button).`, "Add visible text or an aria-label that says what the button does (WCAG 4.1.2).");
|
|
94
|
+
const links = named("a").filter((t) => "href" in t.attrs);
|
|
95
|
+
const emptyLinks = links.filter((t) => {
|
|
96
|
+
const inner = innerHtml(p, t);
|
|
97
|
+
return !stripTags(inner) && !/<img[^>]+alt\s*=\s*["'][^"']+["']/i.test(inner) && !t.attrs["aria-label"]?.trim() && !t.attrs["aria-labelledby"] && !t.attrs.title?.trim();
|
|
98
|
+
});
|
|
99
|
+
if (emptyLinks.length)
|
|
100
|
+
add("high", "accessibility", `${emptyLinks.length} link${emptyLinks.length > 1 ? "s have" : " has"} no text or accessible name.`, "Give every link descriptive text or an aria-label (WCAG 2.4.4).");
|
|
101
|
+
const vague = links.filter((t) => GENERIC_LINK.test(stripTags(innerHtml(p, t))));
|
|
102
|
+
if (vague.length)
|
|
103
|
+
add("low", "accessibility", `${vague.length} link${vague.length > 1 ? "s use" : " uses"} vague text such as "click here" or "read more".`, "Make link text meaningful on its own, for example \"Read the pricing guide\".");
|
|
104
|
+
const headings = p.tags.filter((t) => /^h[1-6]$/.test(t.name)).map((t) => ({ level: Number(t.name[1]) }));
|
|
105
|
+
if (!fragment && headings.length && !headings.some((h) => h.level === 1))
|
|
106
|
+
add("low", "semantics", "The page has no <h1>.", "Use one <h1> that names the page.");
|
|
107
|
+
if (headings.filter((h) => h.level === 1).length > 1)
|
|
108
|
+
add("low", "semantics", "The page has more than one <h1>.", "Use a single <h1> and <h2> to <h6> for sections.");
|
|
109
|
+
for (let i = 1; i < headings.length; i++) {
|
|
110
|
+
if (headings[i].level > headings[i - 1].level + 1) {
|
|
111
|
+
add("medium", "accessibility", `Heading levels skip from h${headings[i - 1].level} to h${headings[i].level}.`, "Do not skip heading levels; they form the page outline screen-reader users navigate by.");
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const ids = p.tags.map((t) => t.attrs.id).filter(Boolean);
|
|
116
|
+
const dupes = [...new Set(ids.filter((id, i) => ids.indexOf(id) !== i))];
|
|
117
|
+
if (dupes.length)
|
|
118
|
+
add("medium", "accessibility", `Duplicate id values: ${dupes.slice(0, 5).join(", ")}.`, "ids must be unique, or labels and aria references point at the wrong element.");
|
|
119
|
+
const posTab = p.tags.filter((t) => Number(t.attrs.tabindex) > 0);
|
|
120
|
+
if (posTab.length)
|
|
121
|
+
add("medium", "accessibility", `${posTab.length} element${posTab.length > 1 ? "s use" : " uses"} a positive tabindex, which breaks the natural focus order.`, "Use tabindex=\"0\" or \"-1\" only, and fix the DOM order instead.");
|
|
122
|
+
const frames = named("iframe").filter((t) => !t.attrs.title?.trim());
|
|
123
|
+
if (frames.length)
|
|
124
|
+
add("medium", "accessibility", `${frames.length} iframe${frames.length > 1 ? "s have" : " has"} no title.`, "Add a title that describes the embedded content.");
|
|
125
|
+
const autoplay = p.tags.filter((t) => (t.name === "video" || t.name === "audio") && "autoplay" in t.attrs);
|
|
126
|
+
if (autoplay.length)
|
|
127
|
+
add("medium", "accessibility", `${autoplay.length} media element${autoplay.length > 1 ? "s autoplay" : " autoplays"}.`, "Do not autoplay audio. If video must autoplay, keep it muted and give a pause control (WCAG 1.4.2, 2.2.2).");
|
|
128
|
+
const tablesNoHeaders = named("table").filter((t) => !/<th[\s>]/i.test(innerHtml(p, t)));
|
|
129
|
+
if (tablesNoHeaders.length)
|
|
130
|
+
add("low", "accessibility", `${tablesNoHeaders.length} table${tablesNoHeaders.length > 1 ? "s have" : " has"} no header cells.`, "Use <th> for header cells so screen readers can read rows and columns.");
|
|
131
|
+
const blocking = p.tags.filter((t) => t.name === "script" && t.attrs.src && !("async" in t.attrs) && !("defer" in t.attrs) && (t.attrs.type ?? "").toLowerCase() !== "module");
|
|
132
|
+
if (blocking.length > 2)
|
|
133
|
+
add("low", "performance", `${blocking.length} scripts block rendering (no async, defer or type=module).`, "Add defer to scripts that do not need to run before the page paints.");
|
|
134
|
+
const count = (s) => findings.filter((f) => f.sev === s).length;
|
|
135
|
+
const score = Math.max(0, 100 - count("high") * 10 - count("medium") * 5 - count("low") * 2);
|
|
136
|
+
return {
|
|
137
|
+
score,
|
|
138
|
+
findings,
|
|
139
|
+
fragment,
|
|
140
|
+
stats: { tags: p.tags.length, images: images.length, links: links.length, forms: named("form").length, headings: headings.length },
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
export function formatHtmlAudit(a, label = "the page") {
|
|
144
|
+
const count = (s) => a.findings.filter((f) => f.sev === s).length;
|
|
145
|
+
const lines = [
|
|
146
|
+
"## HTML audit",
|
|
147
|
+
`Score: **${a.score}/100** for ${label} · ${count("high")} high, ${count("medium")} medium, ${count("low")} low`,
|
|
148
|
+
`${a.stats.tags} elements · ${a.stats.images} images · ${a.stats.links} links · ${a.stats.forms} forms · ${a.stats.headings} headings${a.fragment ? " · fragment, so page-level checks (lang, title, viewport, main) were skipped" : ""}`,
|
|
149
|
+
];
|
|
150
|
+
for (const sev of ["high", "medium", "low"]) {
|
|
151
|
+
const list = a.findings.filter((f) => f.sev === sev);
|
|
152
|
+
if (list.length)
|
|
153
|
+
lines.push("", `### ${sev[0].toUpperCase() + sev.slice(1)}`, ...list.map((f) => `- ${f.msg} Fix: ${f.fix}`));
|
|
154
|
+
}
|
|
155
|
+
if (!a.findings.length)
|
|
156
|
+
lines.push("", "No issues found.");
|
|
157
|
+
lines.push("", "_This is a static check of the markup. It cannot judge color contrast (use `audit_accessibility` or `lint_css`) or behavior that scripts add later._");
|
|
158
|
+
return lines.join("\n");
|
|
159
|
+
}
|
package/dist/playbooks.js
CHANGED
|
@@ -64,6 +64,15 @@ export const PLAYBOOKS = [
|
|
|
64
64
|
? "1. Call `capture_url` and `audit_site`, then run `critique_design` and `audit_accessibility` on the screenshot."
|
|
65
65
|
: "1. Call `critique_design` and `audit_accessibility` on the image.", "2. Combine the results into a verdict (two sentences) and the top 5 fixes in priority order, each with the concrete change.", "3. End with what is already working well."),
|
|
66
66
|
},
|
|
67
|
+
{
|
|
68
|
+
name: "security-check",
|
|
69
|
+
title: "Security and privacy check",
|
|
70
|
+
description: "Review a site or front-end code for security and privacy problems, then fix what you can.",
|
|
71
|
+
args: [{ name: "target", description: "A page URL you own, or a path or description of the front-end code", required: true }],
|
|
72
|
+
build: (a) => L("Run a security and privacy check on " + a.target + " and fix what you can.", RULE, "Only check sites and code the user owns or has permission to review. Never repeat a secret you find; quote only its redacted preview.", "", isUrl(a.target)
|
|
73
|
+
? "1. Call `audit_security` for the page and note the grade."
|
|
74
|
+
: "1. Call `audit_html` on the markup, and call `scan_secrets` on config files, .env examples and front-end bundles (pass their contents).", "2. List the high and medium findings in priority order, each with the exact fix.", "3. Call `generate_csp` (report-only first) and `generate_security_headers` for the user's host, and add them to the project's config.", "4. If any secret was found, say it must be revoked and rotated, and that it remains in git history.", isUrl(a.target) ? "5. After the fix is deployed, call `audit_security` again and report the grade before and after." : "5. Re-run the same checks after the changes and report the results.", "6. Finish with what this review cannot cover, because it is a passive check and not a penetration test."),
|
|
75
|
+
},
|
|
67
76
|
{
|
|
68
77
|
name: "polish-css",
|
|
69
78
|
title: "Polish CSS to a high score",
|
package/dist/secrets.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds credentials that should never be committed or served: API keys, tokens, private keys and
|
|
3
|
+
* passwords in connection strings. Findings are always redacted; a full secret is never returned.
|
|
4
|
+
*/
|
|
5
|
+
const ROTATE = "Revoke and replace this credential now, then remove it from the code and from git history.";
|
|
6
|
+
export const shannonEntropy = (s) => {
|
|
7
|
+
if (!s)
|
|
8
|
+
return 0;
|
|
9
|
+
const counts = new Map();
|
|
10
|
+
for (const ch of s)
|
|
11
|
+
counts.set(ch, (counts.get(ch) ?? 0) + 1);
|
|
12
|
+
let h = 0;
|
|
13
|
+
for (const n of counts.values()) {
|
|
14
|
+
const p = n / s.length;
|
|
15
|
+
h -= p * Math.log2(p);
|
|
16
|
+
}
|
|
17
|
+
return h;
|
|
18
|
+
};
|
|
19
|
+
const PLACEHOLDER = /(example|sample|placeholder|your[_-]?|changeme|change[_-]me|xxxx|\*{4,}|<[^>]+>|\{\{|\$\{|process\.env|dummy|redacted|todo|fake|test[_-]?key)/i;
|
|
20
|
+
const RULES = [
|
|
21
|
+
{ id: "Private key block", severity: "high", re: /-----BEGIN (?:RSA |EC |DSA |OPENSSH |PGP |ENCRYPTED )?PRIVATE KEY(?: BLOCK)?-----/g, advice: ROTATE },
|
|
22
|
+
{ id: "AWS access key ID", severity: "high", re: /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g, advice: ROTATE },
|
|
23
|
+
{ id: "AWS secret access key", severity: "high", re: /aws.{0,20}?secret.{0,20}?[:=]\s*["']?([A-Za-z0-9/+=]{40})["']?/gi, group: 1, advice: ROTATE },
|
|
24
|
+
{ id: "GitHub token", severity: "high", re: /\bgh[pousr]_[A-Za-z0-9]{36,}\b/g, advice: ROTATE },
|
|
25
|
+
{ id: "GitHub fine-grained token", severity: "high", re: /\bgithub_pat_[A-Za-z0-9_]{22,}\b/g, advice: ROTATE },
|
|
26
|
+
{ id: "Slack token", severity: "high", re: /\bxox[abprs]-[A-Za-z0-9-]{10,}\b/g, advice: ROTATE },
|
|
27
|
+
{ id: "Stripe secret key", severity: "high", re: /\b[sr]k_live_[0-9a-zA-Z]{16,}\b/g, advice: ROTATE },
|
|
28
|
+
{ id: "Stripe test secret key", severity: "medium", re: /\b[sr]k_test_[0-9a-zA-Z]{16,}\b/g, advice: "Test keys are lower risk, but still should not be committed. Rotate if it was ever shared." },
|
|
29
|
+
{ id: "Anthropic API key", severity: "high", re: /\bsk-ant-[A-Za-z0-9_-]{32,}\b/g, advice: ROTATE },
|
|
30
|
+
{ id: "OpenRouter API key", severity: "high", re: /\bsk-or-v1-[a-f0-9]{48,}\b/g, advice: ROTATE },
|
|
31
|
+
{ id: "OpenAI API key", severity: "high", re: /\bsk-(?:proj-|svcacct-|admin-)?[A-Za-z0-9_-]{32,}\b/g, advice: ROTATE, accept: (s) => !s.startsWith("sk-ant-") && !s.startsWith("sk-or-") },
|
|
32
|
+
{ id: "Groq API key", severity: "high", re: /\bgsk_[A-Za-z0-9]{40,}\b/g, advice: ROTATE },
|
|
33
|
+
{ id: "Google API key", severity: "medium", re: /\bAIza[0-9A-Za-z_-]{35}\b/g, advice: "Google API keys used in browsers are acceptable only if restricted by HTTP referrer and API. Check the key's restrictions, and rotate if it is unrestricted." },
|
|
34
|
+
{ id: "Google OAuth client secret", severity: "high", re: /\bGOCSPX-[A-Za-z0-9_-]{24,}\b/g, advice: ROTATE },
|
|
35
|
+
{ id: "npm token", severity: "high", re: /\bnpm_[A-Za-z0-9]{36}\b/g, advice: ROTATE },
|
|
36
|
+
{ id: "SendGrid API key", severity: "high", re: /\bSG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}\b/g, advice: ROTATE },
|
|
37
|
+
{ id: "Twilio API key", severity: "high", re: /\bSK[0-9a-fA-F]{32}\b/g, advice: ROTATE },
|
|
38
|
+
{ id: "JSON Web Token", severity: "medium", re: /\beyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b/g, advice: "A JWT may be a live session or access token. If it is real, revoke the session and never ship tokens in front-end code." },
|
|
39
|
+
{
|
|
40
|
+
id: "Credentials in a connection string",
|
|
41
|
+
severity: "high",
|
|
42
|
+
re: /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:/@"'<>]+:([^\s@/"'<>]{4,})@[^\s/"'<>]+/gi,
|
|
43
|
+
group: 1,
|
|
44
|
+
advice: ROTATE,
|
|
45
|
+
accept: (s) => !PLACEHOLDER.test(s) && !/^(password|pass|pwd|secret|user|root|admin|\*+)$/i.test(s),
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
id: "Hard-coded secret assignment",
|
|
49
|
+
severity: "medium",
|
|
50
|
+
re: /\b[\w.-]{0,30}(?:api[_-]?key|secret|token|passwd|password|private[_-]?key|client[_-]?secret|auth[_-]?key)[\w.-]{0,20}["']?\s*[:=]\s*["']([^"'\s]{12,})["']/gi,
|
|
51
|
+
group: 1,
|
|
52
|
+
advice: "This looks like a hard-coded credential. Move it to an environment variable or secret manager and rotate it if it is real.",
|
|
53
|
+
accept: (s) => !PLACEHOLDER.test(s) && !/^https?:/i.test(s) && shannonEntropy(s) >= 3.0
|
|
54
|
+
},
|
|
55
|
+
];
|
|
56
|
+
export const preview = (secret) => secret.length <= 8 ? `${secret.slice(0, 2)}…(${secret.length} chars)` : `${secret.slice(0, 4)}…${secret.slice(-2)} (${secret.length} chars)`;
|
|
57
|
+
const MAX_BYTES = 1_000_000;
|
|
58
|
+
export function scanSecrets(input) {
|
|
59
|
+
const text = input.length > MAX_BYTES ? input.slice(0, MAX_BYTES) : input;
|
|
60
|
+
const lineStarts = [0];
|
|
61
|
+
for (let i = 0; i < text.length; i++)
|
|
62
|
+
if (text[i] === "\n")
|
|
63
|
+
lineStarts.push(i + 1);
|
|
64
|
+
const locate = (idx) => {
|
|
65
|
+
let lo = 0, hi = lineStarts.length - 1;
|
|
66
|
+
while (lo < hi) {
|
|
67
|
+
const mid = (lo + hi + 1) >> 1;
|
|
68
|
+
if (lineStarts[mid] <= idx)
|
|
69
|
+
lo = mid;
|
|
70
|
+
else
|
|
71
|
+
hi = mid - 1;
|
|
72
|
+
}
|
|
73
|
+
return { line: lo + 1, column: idx - lineStarts[lo] + 1 };
|
|
74
|
+
};
|
|
75
|
+
const found = [];
|
|
76
|
+
for (const rule of RULES) {
|
|
77
|
+
rule.re.lastIndex = 0;
|
|
78
|
+
for (const m of text.matchAll(rule.re)) {
|
|
79
|
+
const secret = m[rule.group ?? 0];
|
|
80
|
+
if (!secret)
|
|
81
|
+
continue;
|
|
82
|
+
if (rule.accept && !rule.accept(secret))
|
|
83
|
+
continue;
|
|
84
|
+
const start = (m.index ?? 0) + (rule.group ? m[0].indexOf(secret) : 0);
|
|
85
|
+
const end = start + secret.length;
|
|
86
|
+
// A more specific rule already claimed this span (for example an Anthropic key is not also an OpenAI key).
|
|
87
|
+
if (found.some((f) => start < f.end && end > f.start))
|
|
88
|
+
continue;
|
|
89
|
+
const { line, column } = locate(start);
|
|
90
|
+
found.push({ rule: rule.id, severity: rule.severity, line, column, preview: preview(secret), advice: rule.advice, start, end });
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return found.sort((a, b) => a.line - b.line || a.column - b.column).map(({ start: _s, end: _e, ...f }) => f);
|
|
94
|
+
}
|
|
95
|
+
export function formatSecretsReport(findings, label = "the text") {
|
|
96
|
+
if (!findings.length) {
|
|
97
|
+
return `## Secret scan\nNo credentials found in ${label}.\n\nThis checks for known key formats and obvious hard-coded passwords. It cannot prove a file is clean, so also keep secrets out of git by using environment variables and a \`.gitignore\` for \`.env\` files.`;
|
|
98
|
+
}
|
|
99
|
+
const high = findings.filter((f) => f.severity === "high").length;
|
|
100
|
+
const lines = [
|
|
101
|
+
"## Secret scan",
|
|
102
|
+
`Found **${findings.length}** possible credential${findings.length > 1 ? "s" : ""} in ${label} (${high} high severity). Values are redacted below.`,
|
|
103
|
+
"",
|
|
104
|
+
"| Where | What | Severity | Preview |",
|
|
105
|
+
"|---|---|---|---|",
|
|
106
|
+
...findings.map((f) => `| line ${f.line}, col ${f.column} | ${f.rule} | ${f.severity} | \`${f.preview}\` |`),
|
|
107
|
+
"",
|
|
108
|
+
"### What to do",
|
|
109
|
+
...[...new Set(findings.map((f) => f.advice))].map((a) => `- ${a}`),
|
|
110
|
+
"- Do not paste these values into chats, tickets or logs. Share only the redacted previews above.",
|
|
111
|
+
];
|
|
112
|
+
return lines.join("\n");
|
|
113
|
+
}
|