Repository navigation
Expand file tree
/
Copy pathstorage.test.ts
More file actions
157 lines (157 loc) · 5.47 KB
/
Copy pathstorage.test.ts
File metadata and controls
157 lines (157 loc) · 5.47 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { storageRuntime } from "@server/adapters/storage.js";
import { afterEach, describe, expect, it, vi } from "vitest";
import { generateKeyPairSync } from "node:crypto";
import { Readable } from "node:stream";
import { objectKey } from "../apps/server/src/services/storage-policy.js";
import { S3Client } from "@aws-sdk/client-s3";
import {
createStorage,
storageDefaults,
validateStorage,
type StorageRuntime,
} from "../apps/server/src/adapters/storage.js";
const runtime: StorageRuntime = {
configuration: storageRuntime().configuration, root: "/unused",
credentials: {
test: { accessKeyId: "test-key", secretAccessKey: "test-secret" },
},
endpointHosts: ["storage.example.com"],
cdnKeyPairId: undefined,
cdnPrivateKey: undefined,
};
const config = {
...storageDefaults,
provider: "s3" as const,
bucket: "doca-files",
credentialRef: "test",
};
const key = objectKey("6c59c39f-3a84-4656-8513-2bfe88336591", "application/octet-stream");
afterEach(() => vi.restoreAllMocks());
describe("storage adapters", () => {
it("sends S3 uploads to the configured bucket and consumes downloads before closing", async () => {
const commands: any[] = [];
vi.spyOn(S3Client.prototype, "send").mockImplementation((async (
command: any,
) => {
commands.push(command);
return {
Body: Readable.from([Buffer.from("downloaded")]),
};
}) as any);
const store = createStorage(runtime);
await store.put(
config,
key,
Buffer.from("uploaded"),
"application/octet-stream",
"文档.pdf",
);
expect(commands[0].input).toMatchObject({
Bucket: "doca-files",
Key: key,
ContentType: "application/octet-stream",
});
expect(commands[0].input.Body.toString()).toBe("uploaded");
expect(commands[0].input.ACL).toBeUndefined();
expect((await store.read(config, key)).toString()).toBe("downloaded");
await store.remove(config, key);
expect(commands.map((c) => c.constructor.name)).toEqual([
"PutObjectCommand",
"GetObjectCommand",
"DeleteObjectCommand",
]);
});
it("requires configured credentials and explicit HTTP(S) endpoint allowlisting", () => {
expect(() =>
validateStorage({ ...config, credentialRef: "unknown" }, runtime),
).toThrow();
for (const endpoint of [
"ftp://storage.example.com",
"http://127.0.0.1",
"http://storage.example.com/path",
"http://user:pw@storage.example.com",
"http://storage.example.com/?secret=token",
"http://storage.example.com/#token",
"https://127.0.0.1",
"https://storage.example.com/path",
"https://user:pw@storage.example.com",
])
expect(() => validateStorage({ ...config, endpoint }, runtime)).toThrow();
for (const endpoint of [
"http://storage.example.com",
"https://storage.example.com",
])
expect(() => validateStorage({ ...config, endpoint }, runtime)).not.toThrow();
});
it("never falls back to an unsigned public CDN URL", () => {
expect(() =>
validateStorage(
{ ...config, cdnDomain: "https://files.example.com" },
runtime,
),
).toThrow();
expect(() =>
createStorage(runtime).cdnUrl(
{ ...config, cdnDomain: "https://files.example.com" },
key,
),
).toThrow();
expect(createStorage(runtime).cdnUrl(config, key)).toBeNull();
const { privateKey } = generateKeyPairSync("rsa", {
modulusLength: 2048,
privateKeyEncoding: { type: "pkcs8", format: "pem" },
publicKeyEncoding: { type: "spki", format: "pem" },
});
expect(() =>
validateStorage(
{
...config,
cdnDomain: "ftp://files.example.com",
cdnPrivateKey: privateKey,
cdnKeyPairId: "test-pair",
},
runtime,
),
).toThrow("CDN 请填写 HTTP(S) 域名");
const internalUrl = new URL(
createStorage({
...runtime,
cdnPrivateKey: privateKey,
cdnKeyPairId: "test-pair",
}).cdnUrl({ ...config, cdnDomain: "http://files.internal" }, key)!,
);
expect(internalUrl.protocol).toBe("http:");
expect(internalUrl.searchParams.get("Signature")).toBeTruthy();
const url = new URL(
createStorage({
...runtime,
cdnPrivateKey: privateKey,
cdnKeyPairId: "test-pair",
}).cdnUrl(
{ ...config, cdnDomain: "https://files.example.com" },
key,
)!,
);
expect(url.hostname).toBe("files.example.com");
expect(url.searchParams.get("Key-Pair-Id")).toBe("test-pair");
expect(url.searchParams.get("Signature")).toBeTruthy();
expect(
Number(url.searchParams.get("Expires")) - Date.now() / 1000,
).toBeLessThanOrEqual(60);
});
it("rejects non-generated local object paths", async () => {
await expect(
createStorage(runtime).read(storageDefaults, "../../etc/passwd"),
).rejects.toThrow("Invalid object key");
});
it("keeps an S3 client alive until its download stream is consumed or cancelled", async () => {
const destroy = vi.spyOn(S3Client.prototype, "destroy").mockImplementation(() => {});
const body = new Readable({ read() {} });
vi.spyOn(S3Client.prototype, "send").mockResolvedValue({ Body: body } as never);
const stream = await createStorage(runtime).readStream(config, key, { start: 2, end: 5 });
expect(destroy).not.toHaveBeenCalled();
stream.destroy();
await new Promise<void>(resolve => stream.once("close", resolve));
expect(destroy).toHaveBeenCalled();
});
});