Open History to see the last commit
1import path from "node:path";
2import { describe, expect, it } from "vitest";
3import {
4 normalizeAttachmentRelativePath,
5 resolveAttachmentRelativePath,
6} from "./attachmentPaths.ts";
7
8describe("normalizeAttachmentRelativePath", () => {
9 it("returns normalized path for valid input", () => {
10 expect(normalizeAttachmentRelativePath("thread-1/message-1/file.png")).toBe(
11 "thread-1/message-1/file.png",
12 );
13 });
14
15 it("normalizes backslashes to forward slashes", () => {
16 expect(normalizeAttachmentRelativePath("thread-1\\message-1\\file.png")).toBe(
17 "thread-1/message-1/file.png",
18 );
19 });
20
21 it("strips leading slashes", () => {
22 expect(normalizeAttachmentRelativePath("/thread-1/message-1/file.png")).toBe(
23 "thread-1/message-1/file.png",
24 );
25 expect(normalizeAttachmentRelativePath("///thread-1/message-1/file.png")).toBe(
26 "thread-1/message-1/file.png",
27 );
28 });
29
30 it("strips leading backslashes", () => {
31 expect(normalizeAttachmentRelativePath("\\thread-1\\message-1\\file.png")).toBe(
32 "thread-1/message-1/file.png",
33 );
34 });
35
36 it("normalizes path traversal (../)", () => {
37 expect(normalizeAttachmentRelativePath("thread-1/../message-1/file.png")).toBe(
38 "message-1/file.png",
39 );
40 });
41
42 it("returns null for path traversal attempts", () => {
43 expect(normalizeAttachmentRelativePath("../etc/passwd")).toBeNull();
44 expect(normalizeAttachmentRelativePath("thread-1/../../etc/passwd")).toBeNull();
45 expect(normalizeAttachmentRelativePath("..%2f..%2fetc/passwd")).toBeNull();
46 });
47
48 it("returns null for empty path after normalization", () => {
49 expect(normalizeAttachmentRelativePath("")).toBe(".");
50 expect(normalizeAttachmentRelativePath("/")).toBeNull();
51 expect(normalizeAttachmentRelativePath("///")).toBeNull();
52 });
53
54 it("returns null for null bytes", () => {
55 expect(normalizeAttachmentRelativePath("thread-1\0message-1/file.png")).toBeNull();
56 expect(normalizeAttachmentRelativePath("thread-1/../../etc/passwd\0")).toBeNull();
57 });
58
59 it("handles URL-encoded forward slashes in filenames", () => {
60 expect(normalizeAttachmentRelativePath("thread-1/message-1/file%2Fname.png")).toBe(
61 "thread-1/message-1/file%2Fname.png",
62 );
63 });
64
65 it("handles spaces in path", () => {
66 expect(normalizeAttachmentRelativePath("thread-1/message-1/file name.png")).toBe(
67 "thread-1/message-1/file name.png",
68 );
69 });
70
71 it("handles unicode characters", () => {
72 expect(normalizeAttachmentRelativePath("thread-1/消息/file.png")).toBe(
73 "thread-1/消息/file.png",
74 );
75 expect(normalizeAttachmentRelativePath("thread-1/日本語/file.png")).toBe(
76 "thread-1/日本語/file.png",
77 );
78 });
79});
80
81describe("resolveAttachmentRelativePath", () => {
82 it("resolves valid path within attachments directory", () => {
83 const attachmentsDir = "/data/attachments";
84 const result = resolveAttachmentRelativePath({
85 attachmentsDir,
86 relativePath: "thread-1/message-1/file.png",
87 });
88 expect(result).toBe(path.join(attachmentsDir, "thread-1/message-1/file.png"));
89 });
90
91 it("returns null for path traversal attempt", () => {
92 const attachmentsDir = "/data/attachments";
93 expect(
94 resolveAttachmentRelativePath({
95 attachmentsDir,
96 relativePath: "../../../etc/passwd",
97 }),
98 ).toBeNull();
99 });
100
101 it("handles absolute path attempt (normalized to relative)", () => {
102 const attachmentsDir = "/data/attachments";
103 const result = resolveAttachmentRelativePath({
104 attachmentsDir,
105 relativePath: "/etc/passwd",
106 });
107 expect(result).toBe("/data/attachments/etc/passwd");
108 });
109
110 it("returns null for null byte injection", () => {
111 const attachmentsDir = "/data/attachments";
112 expect(
113 resolveAttachmentRelativePath({
114 attachmentsDir,
115 relativePath: "thread-1\0/../../etc/passwd",
116 }),
117 ).toBeNull();
118 });
119
120 it("returns null for URL-encoded traversal", () => {
121 const attachmentsDir = "/data/attachments";
122 expect(
123 resolveAttachmentRelativePath({
124 attachmentsDir,
125 relativePath: "..%2f..%2f..%2fetc%2fpasswd",
126 }),
127 ).toBeNull();
128 });
129
130 it("prevents directory traversal via backslash on POSIX", () => {
131 const attachmentsDir = "/data/attachments";
132 expect(
133 resolveAttachmentRelativePath({
134 attachmentsDir,
135 relativePath: "..\\..\\etc\\passwd",
136 }),
137 ).toBeNull();
138 });
139
140 it("normalizes and resolves complex valid paths", () => {
141 const attachmentsDir = "/data/attachments";
142 const result = resolveAttachmentRelativePath({
143 attachmentsDir,
144 relativePath: "thread-1//message-1///file.png",
145 });
146 expect(result).toBe(path.join(attachmentsDir, "thread-1/message-1/file.png"));
147 });
148
149 it("preserves spaces and special characters in filenames", () => {
150 const attachmentsDir = "/data/attachments";
151 const result = resolveAttachmentRelativePath({
152 attachmentsDir,
153 relativePath: "thread-1/folder with spaces/my file (1).png",
154 });
155 expect(result).toBe(path.join(attachmentsDir, "thread-1/folder with spaces/my file (1).png"));
156 });
157
158 it("handles UTF-8 multibyte characters", () => {
159 const attachmentsDir = "/data/attachments";
160 const result = resolveAttachmentRelativePath({
161 attachmentsDir,
162 relativePath: "thread-1/文档/文件.png",
163 });
164 expect(result).toBe(path.join(attachmentsDir, "thread-1/文档/文件.png"));
165 });
166});