~james/core

core/web/src/lib/safeHref.test.ts -rw-r--r-- 988 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { describe, expect, it } from "vitest";
import { safeHref } from "#lib/safeHref.ts";

describe("safeHref", () => {
	it("returns links unchanged", () => {
		for (const value of [
			"https://tangled.org/about",
			"http://example.com",
			"example.com/blog",
			"mailto:me@example.com",
			"ssh://git@github.com/owner/repo.git",
			"git@github.com:owner/repo.git",
			"http://["
		]) {
			expect(safeHref(value), value).toBe(value);
		}
	});

	it("drops links the browser would run", () => {
		for (const value of [
			"javascript:alert(document.domain)",
			"JavaScript:alert(1)",
			" javascript:alert(1)",
			"java\tscript:alert(1)",
			"java\nscript:alert(1)",
			"\u0001javascript:alert(1)",
			"vbscript:msgbox(1)",
			"data:text/html,<script>alert(1)</script>"
		]) {
			expect(safeHref(value), JSON.stringify(value)).toBeUndefined();
		}
	});

	it("drops empty values", () => {
		expect(safeHref(undefined)).toBeUndefined();
		expect(safeHref("")).toBeUndefined();
	});
});