feat: clean plugin READMEs before they reach the page
A README is third-party HTML that can change after review. It is sanitized against a closed allowlist on every build, and its relative links are pointed at the published version so images and docs resolve without knowing the author's branch. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
4 files changed
+261
-2
No files matched your search
Generated
+172
-1
@@ -9,7 +9,8 @@
|
||||
"version": "26.9.0",
|
||||
"dependencies": {
|
||||
"@astrojs/sitemap": "^3.7.3",
|
||||
"astro": "^7.1.6"
|
||||
"astro": "^7.1.6",
|
||||
"sanitize-html": "^2.17.7"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=22.12.0"
|
||||
@@ -2298,6 +2299,21 @@
|
||||
"integrity": "sha512-aylIc7Z9y4yzHYAJNuESG3hfhC+0Ibp/MAMiaOZgNv4pmEdFyfZhhhny4MNiAfWdBQ1RQ2mfDWmM1x8SvGyp8g==",
|
||||
"license": "CC0-1.0"
|
||||
},
|
||||
"node_modules/dayjs": {
|
||||
"version": "1.11.23",
|
||||
"resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.23.tgz",
|
||||
"integrity": "sha512-QDTCU0M0MxR3hQfnlDJfwekQiaanm1ubOD231u73WBckQ/fsamwRLiE2GBz6D3a/xF1NgfiDLJjXBa1hYOYTtQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/deepmerge": {
|
||||
"version": "4.3.1",
|
||||
"resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz",
|
||||
"integrity": "sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/defu": {
|
||||
"version": "6.1.7",
|
||||
"resolved": "https://registry.npmjs.org/defu/-/defu-6.1.7.tgz",
|
||||
@@ -2479,6 +2495,18 @@
|
||||
"@esbuild/win32-x64": "0.28.2"
|
||||
}
|
||||
},
|
||||
"node_modules/escape-string-regexp": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
|
||||
"integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/eventemitter3": {
|
||||
"version": "5.0.4",
|
||||
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.4.tgz",
|
||||
@@ -2675,6 +2703,107 @@
|
||||
"url": "https://github.com/sponsors/wooorm"
|
||||
}
|
||||
},
|
||||
"node_modules/htmlparser2": {
|
||||
"version": "12.0.0",
|
||||
"resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-12.0.0.tgz",
|
||||
"integrity": "sha512-Tz7u1i95/g2x2jz81+x0FBVhBhY5aRTvD3tXXdFaljuNdzDLJ8UGNRrTcj2cgQvAg3iW/h77Fz15nLW0L0CrZw==",
|
||||
"funding": [
|
||||
"https://github.com/fb55/htmlparser2?sponsor=1",
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/fb55"
|
||||
}
|
||||
],
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"domelementtype": "^3.0.0",
|
||||
"domhandler": "^6.0.0",
|
||||
"domutils": "^4.0.2",
|
||||
"entities": "^8.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20.19.0"
|
||||
}
|
||||
},
|
||||
"node_modules/htmlparser2/node_modules/dom-serializer": {
|
||||
"version": "3.1.1",
|
||||
"resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-3.1.1.tgz",
|
||||
"integrity": "sha512-4MEa38/QexBob6gFNwu+EGdWvhJ1OKuNwdYY3Y3NyeWDQfnGeDYQUDfIRzWu5B5gsv03so2Uxd28YC6zrsx3Lw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"domelementtype": "^3.0.0",
|
||||
"domhandler": "^6.0.0",
|
||||
"entities": "^8.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20.19.0"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/cheeriojs/dom-serializer?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/htmlparser2/node_modules/domelementtype": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-3.0.0.tgz",
|
||||
"integrity": "sha512-umCQid3jKbDmVjx8jGaW7uUykm4DEUeyV21hPxNMo2nV955DhUThwqyOIDtreepP31hl84X7G5U9ZfsWvIB3Pg==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "github",
|
||||
"url": "https://github.com/sponsors/fb55"
|
||||
}
|
||||
],
|
||||
"license": "BSD-2-Clause",
|
||||
"engines": {
|
||||
"node": ">=20.19.0"
|
||||
}
|
||||
},
|
||||
"node_modules/htmlparser2/node_modules/domhandler": {
|
||||
"version": "6.0.1",
|
||||
"resolved": "https://registry.npmjs.org/domhandler/-/domhandler-6.0.1.tgz",
|
||||
"integrity": "sha512-gYzvtM72ZtxQO0T048kd6HWSbbGCNOUwcnfQ01cqIJ4X2IYKFFHZ5mKvrQETcFXxsRObZulDaKmy//R7TPtsBg==",
|
||||
"license": "BSD-2-Clause",
|
||||
"dependencies": {
|
||||
"domelementtype": "^3.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20.19.0"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/fb55/domhandler?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/htmlparser2/node_modules/domutils": {
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/domutils/-/domutils-4.0.2.tgz",
|
||||
"integrity": "sha512-qI4JLRKnSzqFqr7hAlS5xQDusBCjKSEG4t4+7aNrIQMHBcsC2TGEhuyABJdYkgSewL57PNLYEiibY2iPKhKpaA==",
|
||||
"license": "BSD-2-Clause",
|
||||
"dependencies": {
|
||||
"dom-serializer": "^3.0.0",
|
||||
"domelementtype": "^3.0.0",
|
||||
"domhandler": "^6.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20.19.0"
|
||||
},
|
||||
"funding": {
|
||||
"type": "github",
|
||||
"url": "https://github.com/fb55/domutils?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/htmlparser2/node_modules/entities": {
|
||||
"version": "8.1.0",
|
||||
"resolved": "https://registry.npmjs.org/entities/-/entities-8.1.0.tgz",
|
||||
"integrity": "sha512-kxL7msIffSuh9aaFAMD7rxAIuTRMAHMeBtgHW2yUdWw732ZNh4MehkF2gdjvtdmikkaIP9bFDDJOPlsvm7avrA==",
|
||||
"license": "BSD-2-Clause",
|
||||
"engines": {
|
||||
"node": ">=20.19.0"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/fb55/entities?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/http-cache-semantics": {
|
||||
"version": "4.2.0",
|
||||
"resolved": "https://registry.npmjs.org/http-cache-semantics/-/http-cache-semantics-4.2.0.tgz",
|
||||
@@ -2717,6 +2846,15 @@
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/is-plain-object": {
|
||||
"version": "5.1.0",
|
||||
"resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-5.1.0.tgz",
|
||||
"integrity": "sha512-bUi/yjmtKYcRVUtWRGr0UA6xEFh2I6zWUwMrUXB3s7bmYCaZ8a+0ZsTRkrawh/mzlSD1Y0Ph8bp/U+TvBpWDNw==",
|
||||
"license": "MIT",
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/js-yaml": {
|
||||
"version": "4.3.2",
|
||||
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.3.2.tgz",
|
||||
@@ -2745,6 +2883,15 @@
|
||||
"integrity": "sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/launder": {
|
||||
"version": "1.7.1",
|
||||
"resolved": "https://registry.npmjs.org/launder/-/launder-1.7.1.tgz",
|
||||
"integrity": "sha512-mU6WRz5EusL9ZZuiZ5SO4Y6C0P9PAUR9iwdb6bzj4KDihm28DiHFw+/yk9DBH4f+Pv1wuzQ4e2jV3oQ7mkIqvw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"dayjs": "^1.11.7"
|
||||
}
|
||||
},
|
||||
"node_modules/lightningcss": {
|
||||
"version": "1.33.0",
|
||||
"resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.33.0.tgz",
|
||||
@@ -3317,6 +3464,12 @@
|
||||
"integrity": "sha512-yQA4H19AmPEoMUeavPMDIe1higySl/gH/yaQrkT/s07Qp+7pp2hYz30N3z2l5BkjVkF9Ow6o0wjJamm2y7Sn0A==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/parse-srcset": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/parse-srcset/-/parse-srcset-1.0.2.tgz",
|
||||
"integrity": "sha512-/2qh0lav6CmI15FzA3i/2Bzk2zCgQhGMkvhOhKNcBVQ1ldgpbfiNTVslmooUmWJcADi1f1kIeynbDRVzNlfR6Q==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/piccolore": {
|
||||
"version": "0.1.3",
|
||||
"resolved": "https://registry.npmjs.org/piccolore/-/piccolore-0.1.3.tgz",
|
||||
@@ -3497,6 +3650,24 @@
|
||||
"@rolldown/binding-win32-x64-msvc": "1.2.9"
|
||||
}
|
||||
},
|
||||
"node_modules/sanitize-html": {
|
||||
"version": "2.17.7",
|
||||
"resolved": "https://registry.npmjs.org/sanitize-html/-/sanitize-html-2.17.7.tgz",
|
||||
"integrity": "sha512-PGtEkc9cbnedU3s9TmzDbpsZ8w086g/0Q8k8/oIO1NLNU3i5k9yn835CrjJSajp1KMmkisbO1qPXxNKO3welAg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"deepmerge": "^4.2.2",
|
||||
"escape-string-regexp": "^4.0.0",
|
||||
"htmlparser2": "^12.0.0",
|
||||
"is-plain-object": "^5.0.0",
|
||||
"launder": "^1.7.1",
|
||||
"parse-srcset": "^1.0.2",
|
||||
"postcss": "^8.3.11"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=22.12.0"
|
||||
}
|
||||
},
|
||||
"node_modules/satteri": {
|
||||
"version": "0.10.5",
|
||||
"resolved": "https://registry.npmjs.org/satteri/-/satteri-0.10.5.tgz",
|
||||
|
||||
+2
-1
@@ -16,6 +16,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@astrojs/sitemap": "^3.7.3",
|
||||
"astro": "^7.1.6"
|
||||
"astro": "^7.1.6",
|
||||
"sanitize-html": "^2.17.7"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// A plugin's README is somebody else's HTML. Review reads the yaml once; the README can change on
|
||||
// any later publish and reach the site at the next Friday cut. So it is cleaned against a closed
|
||||
// allowlist every build, and never trusted because the entry was.
|
||||
import sanitizeHtml from 'sanitize-html';
|
||||
|
||||
const HEX = /^#[0-9a-f]{3,8}$/i;
|
||||
|
||||
/** Relative URLs resolve against the published package; anchors and absolute URLs stay. */
|
||||
function absolute(url, base) {
|
||||
if (!url || url.startsWith('#')) return url;
|
||||
try {
|
||||
return new URL(url, base).href;
|
||||
} catch {
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
/** Only touch the attribute when it is there — setting it to `undefined` renders a bare `href`. */
|
||||
function rebase(attribs, key, base) {
|
||||
return key in attribs ? { ...attribs, [key]: absolute(attribs[key], base) } : attribs;
|
||||
}
|
||||
|
||||
export function cleanReadme(html, base) {
|
||||
return sanitizeHtml(html, {
|
||||
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img', 'h1', 'h2', 'details', 'summary', 'picture', 'source']),
|
||||
allowedAttributes: {
|
||||
'*': ['id', 'align'],
|
||||
a: ['href', 'title'],
|
||||
img: ['src', 'alt', 'title', 'width', 'height'],
|
||||
source: ['srcset', 'media'],
|
||||
pre: ['class', 'style', 'tabindex', 'data-language'],
|
||||
span: ['class', 'style'],
|
||||
code: ['class'],
|
||||
},
|
||||
// Only what shiki writes. Anything else (position, inset, z-index…) would let a README draw
|
||||
// over the page around it.
|
||||
allowedStyles: {
|
||||
'*': { color: [HEX], 'background-color': [HEX], 'overflow-x': [/^auto$/] },
|
||||
},
|
||||
allowedSchemes: ['http', 'https', 'mailto'],
|
||||
transformTags: {
|
||||
a: (tagName, attribs) => ({ tagName, attribs: rebase(attribs, 'href', base) }),
|
||||
img: (tagName, attribs) => ({ tagName, attribs: rebase(attribs, 'src', base) }),
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
import { isEsmOnly, byNewest, npmReadme } from './registry.mjs';
|
||||
import { cleanReadme } from './readme.mjs';
|
||||
|
||||
test('isEsmOnly: type module with ESM exports passes', () => {
|
||||
assert.equal(isEsmOnly({ type: 'module', exports: { '.': { import: './dist/index.js', types: './dist/index.d.ts' } } }), true);
|
||||
@@ -36,3 +37,43 @@ test('npmReadme: npm placeholder means no README', () => {
|
||||
assert.equal(npmReadme({}), '');
|
||||
assert.equal(npmReadme({ readme: '# hi' }), '# hi');
|
||||
});
|
||||
|
||||
const BASE = 'https://jsr.io/@green-tea/jwt/26.9.0-beta.0/';
|
||||
|
||||
test('cleanReadme: strips scripts, handlers and javascript: links', () => {
|
||||
const html = cleanReadme('<script>alert(1)</script><img src="x.png" onerror="x()"><a href="javascript:x()">a</a>', BASE);
|
||||
assert.doesNotMatch(html, /script|onerror|javascript:/);
|
||||
});
|
||||
|
||||
test('cleanReadme: keeps align and width (our READMEs center the logo)', () => {
|
||||
const html = cleanReadme('<p align="center"><img src="https://x.io/l.png" width="96" alt="l"></p>', BASE);
|
||||
assert.match(html, /<p align="center">/);
|
||||
assert.match(html, /width="96"/);
|
||||
});
|
||||
|
||||
test('cleanReadme: relative links and images point at the published version', () => {
|
||||
const html = cleanReadme('<a href="./docs/a.md">a</a><img src="assets/l.png">', BASE);
|
||||
assert.match(html, /href="https:\/\/jsr\.io\/@green-tea\/jwt\/26\.9\.0-beta\.0\/docs\/a\.md"/);
|
||||
assert.match(html, /src="https:\/\/jsr\.io\/@green-tea\/jwt\/26\.9\.0-beta\.0\/assets\/l\.png"/);
|
||||
const npm = cleanReadme('<img src="./l.png">', 'https://cdn.jsdelivr.net/npm/green-tea-foo@1.0.0/');
|
||||
assert.match(npm, /src="https:\/\/cdn\.jsdelivr\.net\/npm\/green-tea-foo@1\.0\.0\/l\.png"/);
|
||||
});
|
||||
|
||||
test('cleanReadme: leaves anchors alone', () => {
|
||||
assert.match(cleanReadme('<a href="#install">i</a>', BASE), /href="#install"/);
|
||||
});
|
||||
|
||||
test('cleanReadme: styles — shiki colours stay, layout tricks go', () => {
|
||||
const html = cleanReadme(
|
||||
'<pre class="astro-code" style="background-color:#24292e;overflow-x:auto"><span style="color:#F97583">x</span></pre><div style="position:fixed;inset:0">y</div>',
|
||||
BASE,
|
||||
);
|
||||
assert.match(html, /color:#F97583/);
|
||||
assert.match(html, /background-color:#24292e/);
|
||||
assert.doesNotMatch(html, /position|inset/);
|
||||
});
|
||||
|
||||
test('cleanReadme: an <a> without href does not become a link', () => {
|
||||
const html = cleanReadme('<a name="x">t</a><img alt="n">', BASE);
|
||||
assert.doesNotMatch(html, /href|src/);
|
||||
});
|
||||
Reference in new issue
Block a user