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("serves inline videos with their declared mime type", () => {
54 expect(
55 assetResponseHeaders("/attachments/demo.bin", {
56 mimeType: 'video/mp4; codecs="avc1.42E01E"',
57 }),
58 ).toEqual({
59 "Cache-Control": "private, max-age=3600",
60 "Content-Type": "video/mp4",
61 "X-Content-Type-Options": "nosniff",
62 });
63 });
64 it("declares utf-8 for HTML assets so non-ASCII content renders correctly", () => {
65 expect(assetResponseHeaders("/workspace/page.html")).toHaveProperty(
66 "Content-Type",
67 "text/html; charset=utf-8",
68 );
69 expect(assetResponseHeaders("/workspace/PAGE.HTM")).toHaveProperty(
70 "Content-Type",
71 "text/html; charset=utf-8",
72 );
73 });
74
75 it("downloads uploaded documents without executing their content", () => {
76 expect(assetResponseHeaders("/attachments/upload.html", { download: true })).toMatchObject({
77 "Content-Disposition": "attachment",
78 "Content-Security-Policy": "default-src 'none'; sandbox",
79 "Content-Type": "application/octet-stream",
80 });
81 });
82
83 it("serves the real filename and mime type when the claims carry them", () => {
84 expect(
85 assetResponseHeaders("/attachments/thread-1-abc-pdf.pdf", {
86 download: true,
87 fileName: "Q3 report.pdf",
88 mimeType: "application/pdf",
89 }),
90 ).toMatchObject({
91 "Content-Disposition": 'attachment; filename="Q3 report.pdf"',
92 "Content-Security-Policy": "default-src 'none'; sandbox",
93 "Content-Type": "application/pdf",
94 });
95 });
96
97 it("keeps renderable mime types as octet-stream downloads", () => {
98 for (const mimeType of [
99 "text/html",
100 "text/xml",
101 "image/svg+xml",
102 "application/xhtml+xml",
103 "application/rss+xml",
104 "APPLICATION/XML",
105 "IMAGE/SVG+XML",
106 "application/xml-dtd",
107 "application/xml-external-parsed-entity",
108 "not a mime",
109 ]) {
110 expect(
111 assetResponseHeaders("/attachments/upload.bin", { download: true, mimeType }),
112 ).toHaveProperty("Content-Type", "application/octet-stream");
113 }
114 });
115
116 it("preserves official Office Open XML mime types", () => {
117 for (const mimeType of [
118 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
119 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
120 "application/vnd.openxmlformats-officedocument.presentationml.presentation",
121 ]) {
122 expect(
123 assetResponseHeaders("/attachments/upload.bin", { download: true, mimeType }),
124 ).toHaveProperty("Content-Type", mimeType);
125 }
126 });
127});
128
129describe("downloadContentDisposition", () => {
130 it("quotes plain names and strips quotes and control characters", () => {
131 expect(downloadContentDisposition("report.pdf")).toBe('attachment; filename="report.pdf"');
132 expect(downloadContentDisposition('we"ird\n.pdf')).toBe('attachment; filename="we_ird_.pdf"');
133 });
134
135 it("adds an RFC 5987 encoded name for non-ASCII filenames", () => {
136 expect(downloadContentDisposition("répört.pdf")).toBe(
137 `attachment; filename="r_p_rt.pdf"; filename*=UTF-8''r%C3%A9p%C3%B6rt.pdf`,
138 );
139 expect(downloadContentDisposition("résumé'(*).pdf")).toBe(
140 `attachment; filename="r_sum_'(*).pdf"; filename*=UTF-8''r%C3%A9sum%C3%A9%27%28%2A%29.pdf`,
141 );
142 });
143
144 it("does not throw on unpaired surrogates in the filename", () => {
145 expect(downloadContentDisposition("bad\ud800name.pdf")).toBe(
146 `attachment; filename="bad_name.pdf"; filename*=UTF-8''bad%EF%BF%BDname.pdf`,
147 );
148 });
149});