@smartcrab/server 0.8.0 → 0.9.0-next-20261203092527
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/dist/http.d.ts +3 -0
- package/dist/http.js +6 -1
- package/dist/oidc-client.js +39 -15
- package/dist/security.d.ts +6 -5
- package/dist/security.js +6 -5
- package/dist/server-auth.d.ts +2 -2
- package/dist/server-auth.js +2 -3
- package/dist/webhook.d.ts +4 -0
- package/dist/webhook.js +32 -6
- package/package.json +4 -3
- package/src/http.ts +9 -1
- package/src/oidc-client.ts +56 -17
- package/src/security.ts +6 -5
- package/src/server-auth.ts +4 -7
- package/src/webhook.ts +38 -8
- package/src/sealing.test.ts +0 -57
- package/src/security.test.ts +0 -134
- package/src/server-auth.test.ts +0 -1151
- package/src/webhook.test.ts +0 -96
package/src/sealing.test.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import { Result } from "./result.js";
|
|
3
|
-
import { createSealer } from "./sealing.js";
|
|
4
|
-
|
|
5
|
-
const SECRET = "0123456789abcdef0123456789abcdef";
|
|
6
|
-
|
|
7
|
-
describe("createSealer", () => {
|
|
8
|
-
test("seal/unseal round-trips arbitrary JSON payloads", async () => {
|
|
9
|
-
const sealer = createSealer(SECRET);
|
|
10
|
-
const payload = { v: 1, sub: "usr_x", nested: { list: [1, 2, 3] } };
|
|
11
|
-
const sealed = await sealer.seal(payload);
|
|
12
|
-
expect(Result.isSuccess(sealed)).toBe(true);
|
|
13
|
-
if (Result.isFailure(sealed)) return;
|
|
14
|
-
expect(sealed.value).toMatch(/^v1\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$/);
|
|
15
|
-
|
|
16
|
-
const unsealed = await sealer.unseal(sealed.value);
|
|
17
|
-
expect(Result.isSuccess(unsealed)).toBe(true);
|
|
18
|
-
if (Result.isSuccess(unsealed)) {
|
|
19
|
-
expect(unsealed.value).toEqual(payload);
|
|
20
|
-
}
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test("a tampered ciphertext fails closed (GCM auth tag)", async () => {
|
|
24
|
-
const sealer = createSealer(SECRET);
|
|
25
|
-
const sealed = await sealer.seal({ sub: "usr_x" });
|
|
26
|
-
if (Result.isFailure(sealed)) throw new Error("seal failed");
|
|
27
|
-
const parts = sealed.value.split(".");
|
|
28
|
-
const ciphertext = parts[2] ?? "";
|
|
29
|
-
const midpoint = Math.floor(ciphertext.length / 2);
|
|
30
|
-
const replacement = ciphertext[midpoint] === "A" ? "B" : "A";
|
|
31
|
-
const tampered = `${parts[0]}.${parts[1]}.${ciphertext.slice(0, midpoint)}${replacement}${ciphertext.slice(midpoint + 1)}`;
|
|
32
|
-
|
|
33
|
-
const result = await sealer.unseal(tampered);
|
|
34
|
-
expect(Result.isFailure(result)).toBe(true);
|
|
35
|
-
if (Result.isFailure(result)) {
|
|
36
|
-
expect(result.error.type).toBe("seal_verification_failed");
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test("a value sealed with a different secret fails closed", async () => {
|
|
41
|
-
const sealed = await createSealer("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").seal({ a: 1 });
|
|
42
|
-
if (Result.isFailure(sealed)) throw new Error("seal failed");
|
|
43
|
-
const result = await createSealer(SECRET).unseal(sealed.value);
|
|
44
|
-
expect(Result.isFailure(result)).toBe(true);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test("malformed values are rejected without throwing", async () => {
|
|
48
|
-
const sealer = createSealer(SECRET);
|
|
49
|
-
for (const value of ["", "garbage", "v1.onlytwo", "v2.aaaa.bbbb", "v1.!!!.bbbb"]) {
|
|
50
|
-
const result = await sealer.unseal(value);
|
|
51
|
-
expect(Result.isFailure(result)).toBe(true);
|
|
52
|
-
if (Result.isFailure(result)) {
|
|
53
|
-
expect(result.error.type).toBe("seal_verification_failed");
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
});
|
package/src/security.test.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from "vitest";
|
|
2
|
-
import {
|
|
3
|
-
hasInvalidNavigationOrigin,
|
|
4
|
-
isRequestOriginValid,
|
|
5
|
-
sanitizeRedirectTarget,
|
|
6
|
-
timingSafeEqual,
|
|
7
|
-
} from "./security.js";
|
|
8
|
-
|
|
9
|
-
const APP = "https://app.example.com";
|
|
10
|
-
|
|
11
|
-
describe("sanitizeRedirectTarget (design.md §32.2 open redirect禁止)", () => {
|
|
12
|
-
test.each([
|
|
13
|
-
[null, "/"],
|
|
14
|
-
["", "/"],
|
|
15
|
-
["/dashboard", "/dashboard"],
|
|
16
|
-
["/settings?tab=security", "/settings?tab=security"],
|
|
17
|
-
["//evil.example.com", "/"],
|
|
18
|
-
["https://evil.example.com", "/"],
|
|
19
|
-
["http://localhost:3000/x", "/"],
|
|
20
|
-
["/\\evil.example.com", "/"],
|
|
21
|
-
["/path\r\ninjected", "/"],
|
|
22
|
-
["javascript:alert(1)", "/"],
|
|
23
|
-
["/%2f%2fevil.example.com", "/%2f%2fevil.example.com"],
|
|
24
|
-
])("sanitizeRedirectTarget(%j) → %j", (target, expected) => {
|
|
25
|
-
expect(sanitizeRedirectTarget(target, [], "/")).toBe(expected);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
test("overly long targets fall back", () => {
|
|
29
|
-
expect(sanitizeRedirectTarget(`/${"a".repeat(3000)}`, [], "/")).toBe("/");
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("byte-for-byte allowlisted absolute targets pass", () => {
|
|
33
|
-
expect(
|
|
34
|
-
sanitizeRedirectTarget(
|
|
35
|
-
"https://docs.example.com/welcome",
|
|
36
|
-
["https://docs.example.com/welcome"],
|
|
37
|
-
"/",
|
|
38
|
-
),
|
|
39
|
-
).toBe("https://docs.example.com/welcome");
|
|
40
|
-
// Not byte-for-byte identical → rejected.
|
|
41
|
-
expect(
|
|
42
|
-
sanitizeRedirectTarget(
|
|
43
|
-
"https://docs.example.com/welcome/",
|
|
44
|
-
["https://docs.example.com/welcome"],
|
|
45
|
-
"/",
|
|
46
|
-
),
|
|
47
|
-
).toBe("/");
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
describe("timingSafeEqual", () => {
|
|
52
|
-
test("equal strings match", () => {
|
|
53
|
-
expect(timingSafeEqual("abc123", "abc123")).toBe(true);
|
|
54
|
-
});
|
|
55
|
-
test("different strings of the same length do not match", () => {
|
|
56
|
-
expect(timingSafeEqual("abc123", "abc124")).toBe(false);
|
|
57
|
-
});
|
|
58
|
-
test("different lengths do not match", () => {
|
|
59
|
-
expect(timingSafeEqual("short", "muchlongerstring")).toBe(false);
|
|
60
|
-
});
|
|
61
|
-
test("unicode is compared by UTF-8 bytes", () => {
|
|
62
|
-
expect(timingSafeEqual("日本語", "日本語")).toBe(true);
|
|
63
|
-
expect(timingSafeEqual("日本語", "日本")).toBe(false);
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
describe("isRequestOriginValid (CSRF — design.md §32.2)", () => {
|
|
68
|
-
test("safe methods pass without an Origin", () => {
|
|
69
|
-
expect(isRequestOriginValid(new Request(`${APP}/auth/login`), [])).toBe(true);
|
|
70
|
-
});
|
|
71
|
-
test("POST with matching Origin passes", () => {
|
|
72
|
-
const request = new Request(`${APP}/auth/login`, {
|
|
73
|
-
method: "POST",
|
|
74
|
-
headers: { origin: APP },
|
|
75
|
-
});
|
|
76
|
-
expect(isRequestOriginValid(request, [])).toBe(true);
|
|
77
|
-
});
|
|
78
|
-
test("POST with a foreign Origin is rejected", () => {
|
|
79
|
-
const request = new Request(`${APP}/auth/login`, {
|
|
80
|
-
method: "POST",
|
|
81
|
-
headers: { origin: "https://evil.example.com" },
|
|
82
|
-
});
|
|
83
|
-
expect(isRequestOriginValid(request, [])).toBe(false);
|
|
84
|
-
});
|
|
85
|
-
test("POST with an allowlisted Origin passes", () => {
|
|
86
|
-
const request = new Request(`${APP}/auth/login`, {
|
|
87
|
-
method: "POST",
|
|
88
|
-
headers: { origin: "https://admin.example.com" },
|
|
89
|
-
});
|
|
90
|
-
expect(isRequestOriginValid(request, ["https://admin.example.com"])).toBe(true);
|
|
91
|
-
});
|
|
92
|
-
test('POST with Origin "null" is rejected', () => {
|
|
93
|
-
const request = new Request(`${APP}/auth/login`, {
|
|
94
|
-
method: "POST",
|
|
95
|
-
headers: { origin: "null" },
|
|
96
|
-
});
|
|
97
|
-
expect(isRequestOriginValid(request, [])).toBe(false);
|
|
98
|
-
});
|
|
99
|
-
test("POST falls back to a same-origin Referer", () => {
|
|
100
|
-
const request = new Request(`${APP}/auth/login`, {
|
|
101
|
-
method: "POST",
|
|
102
|
-
headers: { referer: `${APP}/page` },
|
|
103
|
-
});
|
|
104
|
-
expect(isRequestOriginValid(request, [])).toBe(true);
|
|
105
|
-
});
|
|
106
|
-
test("POST with a foreign Referer is rejected", () => {
|
|
107
|
-
const request = new Request(`${APP}/auth/login`, {
|
|
108
|
-
method: "POST",
|
|
109
|
-
headers: { referer: "https://evil.example.com/page" },
|
|
110
|
-
});
|
|
111
|
-
expect(isRequestOriginValid(request, [])).toBe(false);
|
|
112
|
-
});
|
|
113
|
-
test("POST without Origin and Referer fails closed", () => {
|
|
114
|
-
expect(isRequestOriginValid(new Request(`${APP}/auth/login`, { method: "POST" }), [])).toBe(
|
|
115
|
-
false,
|
|
116
|
-
);
|
|
117
|
-
});
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
describe("hasInvalidNavigationOrigin", () => {
|
|
121
|
-
test("GET without Origin is a normal navigation", () => {
|
|
122
|
-
expect(hasInvalidNavigationOrigin(new Request(`${APP}/auth/callback`), [])).toBe(false);
|
|
123
|
-
});
|
|
124
|
-
test("GET with a foreign Origin is rejected", () => {
|
|
125
|
-
const request = new Request(`${APP}/auth/callback`, {
|
|
126
|
-
headers: { origin: "https://evil.example.com" },
|
|
127
|
-
});
|
|
128
|
-
expect(hasInvalidNavigationOrigin(request, [])).toBe(true);
|
|
129
|
-
});
|
|
130
|
-
test("GET with its own Origin passes", () => {
|
|
131
|
-
const request = new Request(`${APP}/auth/callback`, { headers: { origin: APP } });
|
|
132
|
-
expect(hasInvalidNavigationOrigin(request, [])).toBe(false);
|
|
133
|
-
});
|
|
134
|
-
});
|