@sigil-dev/grimoire 0.9.6 → 0.9.10
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/.grimoire/_routes.ts +4 -0
- package/README.md +174 -174
- package/package.json +3 -3
- package/public/__grimoire__/client.js +55 -0
- package/public/__grimoire__/hydrate.js +63 -0
- package/src/client/csrf.ts +47 -0
- package/src/client/enhance.ts +5 -0
- package/src/client/router.ts +622 -619
- package/src/rendering/hydrate.ts +159 -159
- package/src/rendering/index.ts +599 -599
- package/src/routing/scanner.ts +1 -1
- package/src/server/coordinator.ts +376 -364
- package/src/server/index.ts +723 -718
- package/test/client-csrf.test.ts +61 -0
- package/test/client-router.test.ts +366 -366
- package/test/streaming.test.ts +281 -281
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, test } from "bun:test";
|
|
2
|
+
import { ensureCsrf, onSubmitEnsureCsrf } from "../src/client/csrf.ts";
|
|
3
|
+
|
|
4
|
+
// The root test/preload.ts installs happy-dom globals; this package's
|
|
5
|
+
// compiler options have no DOM lib, so reach them untyped.
|
|
6
|
+
const { window, document, Event } = globalThis as any;
|
|
7
|
+
|
|
8
|
+
window.location.href = "http://localhost/";
|
|
9
|
+
|
|
10
|
+
const token = (form: any) =>
|
|
11
|
+
form.querySelector('input[name="_csrf"]')?.value ?? null;
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
document.cookie = "_csrf=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/";
|
|
15
|
+
document.body.innerHTML = "";
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("client CSRF", () => {
|
|
19
|
+
test("a client-rendered form gets the cookie's token", () => {
|
|
20
|
+
document.cookie = "_csrf=abc123; path=/";
|
|
21
|
+
const form = document.createElement("form") as any;
|
|
22
|
+
form.setAttribute("method", "post");
|
|
23
|
+
ensureCsrf(form);
|
|
24
|
+
expect(token(form)).toBe("abc123");
|
|
25
|
+
expect(form.querySelectorAll('input[name="_csrf"]').length).toBe(1);
|
|
26
|
+
expect(form.firstElementChild.hasAttribute("data-grimoire-csrf")).toBe(
|
|
27
|
+
true,
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("a stale SSR token is replaced, not duplicated", () => {
|
|
32
|
+
document.cookie = "_csrf=fresh; path=/";
|
|
33
|
+
document.body.innerHTML = `<form method="post"><input type="hidden" name="_csrf" value="stale" data-grimoire-csrf></form>`;
|
|
34
|
+
const form = document.querySelector("form") as any;
|
|
35
|
+
ensureCsrf(form);
|
|
36
|
+
expect(token(form)).toBe("fresh");
|
|
37
|
+
expect(form.querySelectorAll('input[name="_csrf"]').length).toBe(1);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("no cookie, no change", () => {
|
|
41
|
+
const form = document.createElement("form") as any;
|
|
42
|
+
ensureCsrf(form);
|
|
43
|
+
expect(token(form)).toBeNull();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("the submit listener only touches POST forms", () => {
|
|
47
|
+
document.cookie = "_csrf=tok; path=/";
|
|
48
|
+
document.body.innerHTML = `<form method="post" id="p"></form><form id="g"></form>`;
|
|
49
|
+
document.addEventListener("submit", onSubmitEnsureCsrf as any, true);
|
|
50
|
+
for (const id of ["p", "g"]) {
|
|
51
|
+
document
|
|
52
|
+
.getElementById(id)!
|
|
53
|
+
.dispatchEvent(
|
|
54
|
+
new Event("submit", { bubbles: true, cancelable: true }),
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
document.removeEventListener("submit", onSubmitEnsureCsrf as any, true);
|
|
58
|
+
expect(token(document.getElementById("p"))).toBe("tok");
|
|
59
|
+
expect(token(document.getElementById("g"))).toBeNull();
|
|
60
|
+
});
|
|
61
|
+
});
|