Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import { describe } from "vite-plus/test";
3
4import {
5 assetResponseHeaders,
6 downloadContentDisposition,
7 isLoopbackHostname,
8 resolveDevRedirectUrl,
9} from "./http.ts";
10
11describe("http dev routing", () => {
12 it("treats localhost and loopback addresses as local", () => {
13 expect(isLoopbackHostname("127.0.0.1")).toBe(true);
14 expect(isLoopbackHostname("localhost")).toBe(true);
15 expect(isLoopbackHostname("::1")).toBe(true);
16 expect(isLoopbackHostname("[::1]")).toBe(true);
17 });
18
19 it("does not treat LAN addresses as local", () => {
20 expect(isLoopbackHostname("192.168.86.35")).toBe(false);
21 expect(isLoopbackHostname("10.0.0.24")).toBe(false);
22 expect(isLoopbackHostname("example.local")).toBe(false);
23 });
24
25 it("preserves path and query when redirecting to the dev server", () => {
26 const devUrl = new URL("http://127.0.0.1:5173/");
27 const requestUrl = new URL("http://127.0.0.1:3774/pair?token=test-token");
28
29 expect(resolveDevRedirectUrl(devUrl, requestUrl)).toBe(
30 "http://127.0.0.1:5173/pair?token=test-token",
31 );
32 });
33});
34
35describe("assetResponseHeaders", () => {
36 it("sandboxes SVG assets", () => {
37 expect(assetResponseHeaders("/attachments/user-image.svg")).toMatchObject({
38 "Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'; sandbox",
39 "X-Content-Type-Options": "nosniff",
40 });
41 expect(assetResponseHeaders("/attachments/user-image.SVG")).toHaveProperty(
42 "Content-Security-Policy",
43 );
44 });
45
46 it("does not apply document policy to raster images", () => {
47 expect(assetResponseHeaders("/attachments/user-image.png")).toEqual({
48 "Cache-Control": "private, max-age=3600",
49 "X-Content-Type-Options": "nosniff",
50 });
51 });
52
53 it("declares utf-8 for HTML assets so non-ASCII content renders correctly", () => {
54 expect(assetResponseHeaders("/workspace/page.html")).toHaveProperty(
55 "Content-Type",
56 "text/html; charset=utf-8",
57 );
58 expect(assetResponseHeaders("/workspace/PAGE.HTM")).toHaveProperty(
59 "Content-Type",
60 "text/html; charset=utf-8",
61 );
62 });
63
64 it("downloads uploaded documents without executing their content", () => {
65 expect(assetResponseHeaders("/attachments/upload.html", { download: true })).toMatchObject({
66 "Content-Disposition": "attachment",
67 "Content-Security-Policy": "default-src 'none'; sandbox",
68 "Content-Type": "application/octet-stream",
69 });
70 });
71
72 it("serves the real filename and mime type when the claims carry them", () => {
73 expect(
74 assetResponseHeaders("/attachments/thread-1-abc-pdf.pdf", {
75 download: true,
76 fileName: "Q3 report.pdf",
77 mimeType: "application/pdf",
78 }),
79 ).toMatchObject({
80 "Content-Disposition": 'attachment; filename="Q3 report.pdf"',
81 "Content-Security-Policy": "default-src 'none'; sandbox",
82 "Content-Type": "application/pdf",
83 });
84 });
85
86 it("keeps renderable mime types as octet-stream downloads", () => {
87 for (const mimeType of [
88 "text/html",
89 "text/xml",
90 "image/svg+xml",
91 "application/xhtml+xml",
92 "application/rss+xml",
93 "APPLICATION/XML",
94 "IMAGE/SVG+XML",
95 "application/xml-dtd",
96 "application/xml-external-parsed-entity",
97 "not a mime",
98 ]) {
99 expect(
100 assetResponseHeaders("/attachments/upload.bin", { download: true, mimeType }),
101 ).toHaveProperty("Content-Type", "application/octet-stream");
102 }
103 });
104
105 it("preserves official Office Open XML mime types", () => {
106 for (const mimeType of [
107 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
108 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
109 "application/vnd.openxmlformats-officedocument.presentationml.presentation",
110 ]) {
111 expect(
112 assetResponseHeaders("/attachments/upload.bin", { download: true, mimeType }),
113 ).toHaveProperty("Content-Type", mimeType);
114 }
115 });
116});
117
118describe("downloadContentDisposition", () => {
119 it("quotes plain names and strips quotes and control characters", () => {
120 expect(downloadContentDisposition("report.pdf")).toBe('attachment; filename="report.pdf"');
121 expect(downloadContentDisposition('we"ird\n.pdf')).toBe('attachment; filename="we_ird_.pdf"');
122 });
123
124 it("adds an RFC 5987 encoded name for non-ASCII filenames", () => {
125 expect(downloadContentDisposition("répört.pdf")).toBe(
126 `attachment; filename="r_p_rt.pdf"; filename*=UTF-8''r%C3%A9p%C3%B6rt.pdf`,
127 );
128 expect(downloadContentDisposition("résumé'(*).pdf")).toBe(
129 `attachment; filename="r_sum_'(*).pdf"; filename*=UTF-8''r%C3%A9sum%C3%A9%27%28%2A%29.pdf`,
130 );
131 });
132
133 it("does not throw on unpaired surrogates in the filename", () => {
134 expect(downloadContentDisposition("bad\ud800name.pdf")).toBe(
135 `attachment; filename="bad_name.pdf"; filename*=UTF-8''bad%EF%BF%BDname.pdf`,
136 );
137 });
138});