@celestia-island/hikari 0.55.388 → 0.55.391
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/package.json
CHANGED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { createApp, h, nextTick, ref } from "vue";
|
|
3
|
+
|
|
4
|
+
import HkMarkdownRenderer from "./HkMarkdownRenderer";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* SECURITY (perfsec follow-up, 2026-11-15): the renderer's link override
|
|
8
|
+
* neutralizes script-scheme hrefs at RENDER time — no `href` attribute is
|
|
9
|
+
* emitted at all, so the guard holds even where the DOMPurify build
|
|
10
|
+
* misbehaves (happy-dom strips odd shapes; real browsers rely on it as the
|
|
11
|
+
* primary pass). Middle-click / "open in new tab" reads the raw attribute:
|
|
12
|
+
* this is the face that must stay clean. Legit links — https, mailto,
|
|
13
|
+
* relative, and custom protocols the coordinator adjudicates — keep their
|
|
14
|
+
* attribute and their delegated-click path.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const mounts: { app: ReturnType<typeof createApp>; container: HTMLElement }[] = [];
|
|
18
|
+
|
|
19
|
+
function mount(content: string): { container: HTMLElement; set: (v: string) => void } {
|
|
20
|
+
const holder = ref(content);
|
|
21
|
+
const container = document.createElement("div");
|
|
22
|
+
document.body.appendChild(container);
|
|
23
|
+
const app = createApp({
|
|
24
|
+
render: () => h(HkMarkdownRenderer, { content: holder.value }),
|
|
25
|
+
});
|
|
26
|
+
app.mount(container);
|
|
27
|
+
mounts.push({ app, container });
|
|
28
|
+
return { container, set: (v: string) => void (holder.value = v) };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function settleFor(root: HTMLElement, selector: string, tries = 120): Promise<void> {
|
|
32
|
+
for (let i = 0; i < tries; i += 1) {
|
|
33
|
+
if (root.querySelector(selector)) return;
|
|
34
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
35
|
+
await nextTick();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
afterEach(() => {
|
|
40
|
+
for (const { app, container } of mounts.splice(0)) {
|
|
41
|
+
app.unmount();
|
|
42
|
+
container.remove();
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("HkMarkdownRenderer link scheme guard", () => {
|
|
47
|
+
it("renders a javascript: link as an inert span with no href at all", async () => {
|
|
48
|
+
const { container } = mount("click [me](javascript:alert(1)) now");
|
|
49
|
+
await settleFor(container, ".hk-md-link-neutralized");
|
|
50
|
+
const neutral = container.querySelector(".hk-md-link-neutralized");
|
|
51
|
+
expect(neutral?.textContent).toBe("me");
|
|
52
|
+
// The face that matters: NO anchor carrying a script scheme exists —
|
|
53
|
+
// middle-click and "open in new tab" have nothing to read.
|
|
54
|
+
expect(container.querySelector("a")).toBeNull();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("also neutralizes data:, vbscript: and blob: in one document", async () => {
|
|
58
|
+
const doc = [
|
|
59
|
+
"[a](data:text/html,<b>x</b>)",
|
|
60
|
+
"[b](vbscript:msgbox)",
|
|
61
|
+
"[c](blob:https://x/uuid)",
|
|
62
|
+
].join(" and ");
|
|
63
|
+
const { container } = mount(doc);
|
|
64
|
+
await settleFor(container, ".hk-md-link-neutralized");
|
|
65
|
+
expect(container.querySelectorAll(".hk-md-link-neutralized").length).toBe(3);
|
|
66
|
+
expect(container.querySelector("a")).toBeNull();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("renders the tab-obfuscated javascript: shape as plain text, not a link", async () => {
|
|
70
|
+
// marked's own lexer refuses `java<TAB>script:` as a URL at all, so the
|
|
71
|
+
// construct never becomes an anchor — stronger than neutralizing: there
|
|
72
|
+
// is nothing to click, copy or open. Pinned so a future marked upgrade
|
|
73
|
+
// that starts accepting the shape trips THIS test (and then the guard
|
|
74
|
+
// above catches it).
|
|
75
|
+
const { container } = mount("[d](java\tscript:alert(1))");
|
|
76
|
+
await settleFor(container, ".hk-markdown-body span, .hk-markdown-body a, .hk-markdown-body p");
|
|
77
|
+
expect(container.querySelector("a")).toBeNull();
|
|
78
|
+
expect(container.querySelector(".hk-md-link-neutralized")).toBeNull();
|
|
79
|
+
expect(container.textContent).toContain("[d]");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("keeps legit links intact — https, mailto, relative, custom protocol", async () => {
|
|
83
|
+
const doc = [
|
|
84
|
+
"[docs](https://docs.celestia.world/meta/cla.html)",
|
|
85
|
+
"[mail](mailto:security@celestia.world)",
|
|
86
|
+
"[rel](cla.md)",
|
|
87
|
+
"[frag](#section)",
|
|
88
|
+
"[vscode](vscode://file/x)",
|
|
89
|
+
].join(" · ");
|
|
90
|
+
const { container } = mount(doc);
|
|
91
|
+
await settleFor(container, "a[href]");
|
|
92
|
+
const hrefs = [...container.querySelectorAll("a[href]")].map((a) => a.getAttribute("href"));
|
|
93
|
+
expect(hrefs).toContain("https://docs.celestia.world/meta/cla.html");
|
|
94
|
+
expect(hrefs).toContain("mailto:security@celestia.world");
|
|
95
|
+
expect(hrefs).toContain("cla.md");
|
|
96
|
+
expect(hrefs).toContain("#section");
|
|
97
|
+
// Custom protocols keep their attribute: the coordinator (not the
|
|
98
|
+
// renderer) adjudicates them on click — #1321's contract.
|
|
99
|
+
expect(hrefs).toContain("vscode://file/x");
|
|
100
|
+
expect(container.querySelector(".hk-md-link-neutralized")).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("re-render swaps a neutralized link for a good one and back", async () => {
|
|
104
|
+
const { container, set } = mount("[x](javascript:alert(1))");
|
|
105
|
+
await settleFor(container, ".hk-md-link-neutralized");
|
|
106
|
+
expect(container.querySelector("a")).toBeNull();
|
|
107
|
+
set("[x](https://ok.example/)");
|
|
108
|
+
await settleFor(container, "a[href='https://ok.example/']");
|
|
109
|
+
expect(container.querySelector(".hk-md-link-neutralized")).toBeNull();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe("HkMarkdownRenderer raw-HTML and image sweep (review S1/S2)", () => {
|
|
114
|
+
it("strips the href of a raw-HTML anchor with a script scheme", async () => {
|
|
115
|
+
// renderer.link never sees raw HTML — it rides renderer.html into the
|
|
116
|
+
// sanitize pass, which is unreliable in some environments. The
|
|
117
|
+
// post-sanitize DOM sweep is what must catch this shape.
|
|
118
|
+
const { container } = mount('<a href="javascript:alert(1)">raw</a>');
|
|
119
|
+
await settleFor(container, ".hk-markdown-body a, .hk-markdown-body span");
|
|
120
|
+
const anchors = [...container.querySelectorAll("a")];
|
|
121
|
+
expect(anchors.length).toBe(1);
|
|
122
|
+
expect(anchors[0].getAttribute("href")).toBeNull();
|
|
123
|
+
expect(anchors[0].textContent).toBe("raw");
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("strips raw-HTML anchors case-insensitively and data: variants", async () => {
|
|
127
|
+
const doc = [
|
|
128
|
+
'<A HREF="JavaScript:alert(1)">up</A>',
|
|
129
|
+
'<a href="data:text/html,<b>x</b>">data</a>',
|
|
130
|
+
"<a href='vbscript:msgbox'>vb</a>",
|
|
131
|
+
].join("\n");
|
|
132
|
+
const { container } = mount(doc);
|
|
133
|
+
await settleFor(container, ".hk-markdown-body a, .hk-markdown-body span");
|
|
134
|
+
for (const a of [...container.querySelectorAll("a")]) {
|
|
135
|
+
expect(a.getAttribute("href")).toBeNull();
|
|
136
|
+
}
|
|
137
|
+
expect(container.querySelectorAll("a").length).toBe(3);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("strips a script-scheme img src (image token AND raw HTML)", async () => {
|
|
141
|
+
const doc = [
|
|
142
|
+
")",
|
|
143
|
+
'<img src="javascript:alert(1)" alt="y">',
|
|
144
|
+
"",
|
|
145
|
+
].join("\n");
|
|
146
|
+
const { container } = mount(doc);
|
|
147
|
+
await settleFor(container, ".hk-markdown-body img");
|
|
148
|
+
const srcs = [...container.querySelectorAll("img")]
|
|
149
|
+
.map((i) => i.getAttribute("src"))
|
|
150
|
+
.filter(Boolean);
|
|
151
|
+
expect(srcs).toEqual(["https://example.com/i.png"]);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("strips a script-scheme image-map area href (review follow-up)", async () => {
|
|
155
|
+
// <area href> is the same navigation sink as <a href>, and it is the
|
|
156
|
+
// one shape DOMPurify's unreliable builds let through alongside it.
|
|
157
|
+
const doc = [
|
|
158
|
+
'<map name="m">',
|
|
159
|
+
'<area href="javascript:alert(1)" alt="evil">',
|
|
160
|
+
'<area href="https://ok.example/x" alt="ok">',
|
|
161
|
+
"</map>",
|
|
162
|
+
].join("");
|
|
163
|
+
const { container } = mount(doc);
|
|
164
|
+
await settleFor(container, ".hk-markdown-body area");
|
|
165
|
+
const hrefs = [...container.querySelectorAll("area")]
|
|
166
|
+
.map((el) => el.getAttribute("href"))
|
|
167
|
+
.filter(Boolean);
|
|
168
|
+
expect(hrefs).toEqual(["https://ok.example/x"]);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("keeps a legit raw-HTML anchor untouched", async () => {
|
|
172
|
+
const { container } = mount('<a href="https://ok.example/x">raw-ok</a>');
|
|
173
|
+
await settleFor(container, "a[href]");
|
|
174
|
+
expect(container.querySelector("a[href]")?.getAttribute("href")).toBe(
|
|
175
|
+
"https://ok.example/x",
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
@@ -5,7 +5,7 @@ import HkMermaidDiagram from "./HkMermaidDiagram";
|
|
|
5
5
|
import { applyInlineChipsToBody } from "./inlineChips";
|
|
6
6
|
import { attachOverlayScrollbars, type OverlayScrollbarHandle } from "../composables/useOverlayScrollbar";
|
|
7
7
|
import { getMermaidEngine } from "../mermaid/engine";
|
|
8
|
-
import { openHyperlink } from "../runtime/hyperlink";
|
|
8
|
+
import { isDangerousHref, openHyperlink } from "../runtime/hyperlink";
|
|
9
9
|
import { asIndexable } from "../utils/typing";
|
|
10
10
|
import "./HkMarkdownRenderer.scss";
|
|
11
11
|
|
|
@@ -113,6 +113,24 @@ async function renderMarkdown(content: string): Promise<string> {
|
|
|
113
113
|
return `<div class="hk-md-code-wrap"><div class="hk-md-code">${langLabel}<pre><code class="hljs${langAttr}">${highlighted}</code></pre></div></div>`;
|
|
114
114
|
};
|
|
115
115
|
|
|
116
|
+
// SECURITY (perfsec follow-up, 2026-11-15): the anchor's HREF attribute
|
|
117
|
+
// must never carry a script scheme — DOMPurify is the primary guard in
|
|
118
|
+
// real browsers, but the delegated click only covers plain activation;
|
|
119
|
+
// middle-click / "open in new tab" reads the attribute directly, and the
|
|
120
|
+
// sanitizer is unreliable under some environments (happy-dom strips odd
|
|
121
|
+
// things — see the mermaid tests). A dangerous href renders as an inert
|
|
122
|
+
// span with NO href at all: nothing to intercept, nothing to open, no
|
|
123
|
+
// dependency on the sanitize pass. Legit custom protocols (vscode:, …)
|
|
124
|
+
// keep their attribute — the coordinator decides on click, same as
|
|
125
|
+
// #1321.
|
|
126
|
+
renderer.link = function (this: Renderer, token: Tokens.Link): string {
|
|
127
|
+
if (isDangerousHref(token.href)) {
|
|
128
|
+
const text = this.parser.parseInline(token.tokens);
|
|
129
|
+
return `<span class="hk-md-link-neutralized" title="link removed: unsafe scheme">${text}</span>`;
|
|
130
|
+
}
|
|
131
|
+
return marked.Renderer.prototype.link.call(this, token);
|
|
132
|
+
};
|
|
133
|
+
|
|
116
134
|
renderer.table = function (this: Renderer, token: Tokens.Table): string {
|
|
117
135
|
// Tables size to their content: a wide one (many columns, or a single
|
|
118
136
|
// unbreakable cell) pushes every ancestor's min-content width past its
|
|
@@ -130,7 +148,42 @@ async function renderMarkdown(content: string): Promise<string> {
|
|
|
130
148
|
|
|
131
149
|
const raw = marked.parse(content, { renderer, async: false });
|
|
132
150
|
const html = typeof raw === "string" ? raw : "";
|
|
133
|
-
return DOMPurify.sanitize(html);
|
|
151
|
+
return sweepDangerousUrlAttributes(DOMPurify.sanitize(html));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* SECURITY (perfsec follow-up, 2026-11-15, review S1): the renderer.link
|
|
156
|
+
* override only sees marked's LINK tokens — raw HTML written straight into
|
|
157
|
+
* the markdown (`<a href="javascript:…">`) rides renderer.html into the
|
|
158
|
+
* sanitize pass, which is exactly the environment-unreliable half this
|
|
159
|
+
* guard exists for. This post-sanitize DOM sweep is the braces: every
|
|
160
|
+
* anchor href and image src in the FINAL html is checked, whatever path
|
|
161
|
+
* produced it (link token, autolink, raw HTML, image token). Dangerous
|
|
162
|
+
* values drop the attribute entirely — the element stays (text/link
|
|
163
|
+
* label visible), the navigation face does not.
|
|
164
|
+
*/
|
|
165
|
+
function sweepDangerousUrlAttributes(html: string): string {
|
|
166
|
+
if (typeof window === "undefined" || typeof DOMParser === "undefined") return html;
|
|
167
|
+
try {
|
|
168
|
+
const doc = new DOMParser().parseFromString(html, "text/html");
|
|
169
|
+
let swept = false;
|
|
170
|
+
for (const a of [...doc.querySelectorAll("a[href], area[href]")]) {
|
|
171
|
+
if (isDangerousHref(a.getAttribute("href") ?? "")) {
|
|
172
|
+
a.removeAttribute("href");
|
|
173
|
+
swept = true;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
for (const img of [...doc.querySelectorAll("img[src]")]) {
|
|
177
|
+
if (isDangerousHref(img.getAttribute("src") ?? "")) {
|
|
178
|
+
img.removeAttribute("src");
|
|
179
|
+
swept = true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
if (!swept) return html;
|
|
183
|
+
return doc.body.innerHTML;
|
|
184
|
+
} catch {
|
|
185
|
+
return html;
|
|
186
|
+
}
|
|
134
187
|
}
|
|
135
188
|
|
|
136
189
|
export default defineComponent({
|