Repository navigation
Expand file tree
/
Copy pathplugin-native.test.ts
More file actions
107 lines (106 loc) · 3.17 KB
/
Copy pathplugin-native.test.ts
File metadata and controls
107 lines (106 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { it, expect } from "vitest";
import { validateNativeRequest } from "@smartdoca/plugin-sdk/native";
const request = {
version: 1,
id: "one",
pluginId: "example.mail",
operation: "attachment.save",
input: {
path: "/attachments/a?download=true",
name: "report.pdf",
mime: "application/pdf",
},
};
it("accepts same-plugin relative attachments and rejects namespace, traversal and redirect destinations", () => {
expect(validateNativeRequest(request, "example.mail")).toEqual(request);
expect(() => validateNativeRequest(request, "another.plugin")).toThrow();
for (const path of [
"//evil.example",
"https://evil.example/a",
"/../auth",
"/a/../../auth",
"/%2e%2e/auth",
"/a%2fa",
"/%252e%252e/auth",
"/a\\b",
"/a#b",
])
expect(() =>
validateNativeRequest(
{ ...request, input: { ...request.input, path } },
"example.mail",
),
).toThrow();
});
it("bounds cache values and rejects filesystem names", () => {
for (const name of ["../a", "a/b", "..", ".", "a\\b"])
expect(() =>
validateNativeRequest(
{ ...request, input: { ...request.input, name } },
"example.mail",
),
).toThrow();
expect(() =>
validateNativeRequest(
{
...request,
operation: "storage.set",
input: { key: "mail", value: "a".repeat(2_000_001) },
},
"example.mail",
),
).toThrow();
expect(() =>
validateNativeRequest(
{ ...request, operation: "storage.get", input: { key: "" } },
"example.mail",
),
).toThrow();
});
it("dispatches authorized native downloads internally without following plugin redirects", async () => {
const { default: Fastify } = await import("fastify");
const { registerPluginMobileSessions } =
await import("@server/plugins/mobile-session.js");
const app = Fastify();
const entries = [
{ id: "example.mail.inbox", pluginId: "example.mail", mobile: true },
] as any;
registerPluginMobileSessions(
app,
{} as any,
() => ({ id: "user", display_name: "User", admin: 0 }),
entries,
false,
);
app.get("/api/v1/plugins/example.mail/attachment", (req) => {
expect(req.headers.authorization).toBe(`Bearer ${"a".repeat(64)}`);
return Buffer.from("attachment contents");
});
app.get("/api/v1/plugins/example.mail/redirect", (_req, reply) =>
reply.redirect("https://evil.example"),
);
const download = (path: string, token = true) =>
app.inject({
method: "POST",
url: "/api/v1/plugins-mobile/attachment",
headers: token ? { authorization: `Bearer ${"a".repeat(64)}` } : {},
payload: {
pluginId: "example.mail",
path,
name: "a.txt",
mime: "text/plain",
},
});
try {
const success = await download("/attachment");
expect(success.statusCode).toBe(200);
expect(Buffer.from(success.json().base64, "base64").toString()).toBe(
"attachment contents",
);
expect((await download("/redirect")).statusCode).toBe(400);
expect((await download("/../auth")).statusCode).toBe(400);
expect((await download("/attachment", false)).statusCode).toBe(403);
} finally {
await app.close();
}
});