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
@@ -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