@antelopejs/dms-media 0.8.2 → 0.8.3-next.167.g8bd8349
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/test/antelope.test.js +1 -0
- package/dist/test/antelope.test.js.map +1 -1
- package/dist/test/helpers/config.d.ts +2 -0
- package/dist/test/helpers/config.js +16 -0
- package/dist/test/helpers/config.js.map +1 -0
- package/dist/test/helpers/delivery.d.ts +9 -0
- package/dist/test/helpers/delivery.js +47 -0
- package/dist/test/helpers/delivery.js.map +1 -0
- package/dist/test/helpers/derivative-storage.d.ts +3 -0
- package/dist/test/helpers/derivative-storage.js +22 -0
- package/dist/test/helpers/derivative-storage.js.map +1 -0
- package/dist/test/helpers/media-config.d.ts +2 -0
- package/dist/test/helpers/media-config.js +16 -0
- package/dist/test/helpers/media-config.js.map +1 -0
- package/dist/test/helpers/member-rights.d.ts +99 -0
- package/dist/test/helpers/member-rights.js +85 -0
- package/dist/test/helpers/member-rights.js.map +1 -0
- package/dist/test/helpers/png.d.ts +1 -0
- package/dist/test/helpers/png.js +15 -0
- package/dist/test/helpers/png.js.map +1 -0
- package/dist/test/helpers/renderings.d.ts +27 -0
- package/dist/test/helpers/renderings.js +106 -0
- package/dist/test/helpers/renderings.js.map +1 -0
- package/dist/test/helpers/uploads.d.ts +26 -0
- package/dist/test/helpers/uploads.js +48 -0
- package/dist/test/helpers/uploads.js.map +1 -0
- package/dist/test/helpers/zip.d.ts +6 -0
- package/dist/test/helpers/zip.js +50 -0
- package/dist/test/helpers/zip.js.map +1 -0
- package/dist/test/integration/media-activity-overview.test.js +109 -0
- package/dist/test/integration/media-activity-overview.test.js.map +1 -1
- package/dist/test/integration/media-api.test.js +101 -0
- package/dist/test/integration/media-api.test.js.map +1 -1
- package/dist/test/integration/media-bindings.test.js +259 -0
- package/dist/test/integration/media-bindings.test.js.map +1 -1
- package/dist/test/integration/media-bulk-writes.test.d.ts +1 -0
- package/dist/test/integration/media-bulk-writes.test.js +159 -0
- package/dist/test/integration/media-bulk-writes.test.js.map +1 -0
- package/dist/test/integration/media-declared-rules.test.d.ts +1 -0
- package/dist/test/integration/media-declared-rules.test.js +297 -0
- package/dist/test/integration/media-declared-rules.test.js.map +1 -0
- package/dist/test/integration/media-declared-sources.test.d.ts +1 -0
- package/dist/test/integration/media-declared-sources.test.js +194 -0
- package/dist/test/integration/media-declared-sources.test.js.map +1 -0
- package/dist/test/integration/media-delivery-active-content.test.d.ts +1 -0
- package/dist/test/integration/media-delivery-active-content.test.js +138 -0
- package/dist/test/integration/media-delivery-active-content.test.js.map +1 -0
- package/dist/test/integration/media-delivery-editing.test.js +41 -51
- package/dist/test/integration/media-delivery-editing.test.js.map +1 -1
- package/dist/test/integration/media-delivery.test.js +145 -0
- package/dist/test/integration/media-delivery.test.js.map +1 -1
- package/dist/test/integration/media-explorer-writes.test.js +173 -18
- package/dist/test/integration/media-explorer-writes.test.js.map +1 -1
- package/dist/test/integration/media-file-reads.test.d.ts +1 -0
- package/dist/test/integration/media-file-reads.test.js +147 -0
- package/dist/test/integration/media-file-reads.test.js.map +1 -0
- package/dist/test/integration/media-guards.test.js +1 -0
- package/dist/test/integration/media-guards.test.js.map +1 -1
- package/dist/test/integration/media-library-views.test.d.ts +1 -0
- package/dist/test/integration/media-library-views.test.js +377 -0
- package/dist/test/integration/media-library-views.test.js.map +1 -0
- package/dist/test/integration/media-linked-subfolders.test.js +2 -0
- package/dist/test/integration/media-linked-subfolders.test.js.map +1 -1
- package/dist/test/integration/media-member-access.test.js +96 -0
- package/dist/test/integration/media-member-access.test.js.map +1 -1
- package/dist/test/integration/media-permissions.test.js +7 -0
- package/dist/test/integration/media-permissions.test.js.map +1 -1
- package/dist/test/integration/media-preset-delivery.test.d.ts +1 -0
- package/dist/test/integration/media-preset-delivery.test.js +161 -0
- package/dist/test/integration/media-preset-delivery.test.js.map +1 -0
- package/dist/test/integration/media-presets.test.d.ts +1 -0
- package/dist/test/integration/media-presets.test.js +223 -0
- package/dist/test/integration/media-presets.test.js.map +1 -0
- package/dist/test/integration/media-transform.test.js +105 -10
- package/dist/test/integration/media-transform.test.js.map +1 -1
- package/dist/test/integration/media-upload-content.test.d.ts +1 -0
- package/dist/test/integration/media-upload-content.test.js +142 -0
- package/dist/test/integration/media-upload-content.test.js.map +1 -0
- package/dist/test/integration/media-upload-policy.test.js +13 -12
- package/dist/test/integration/media-upload-policy.test.js.map +1 -1
- package/dist/test/integration/media-upload-types.test.d.ts +1 -0
- package/dist/test/integration/media-upload-types.test.js +176 -0
- package/dist/test/integration/media-upload-types.test.js.map +1 -0
- package/dist/test/integration/permission-levels.d.ts +1 -0
- package/dist/test/integration/permission-levels.js +207 -0
- package/dist/test/integration/permission-levels.js.map +1 -0
- package/dist/test/integration/permission-reach.d.ts +1 -0
- package/dist/test/integration/permission-reach.js +209 -0
- package/dist/test/integration/permission-reach.js.map +1 -0
- package/dist/test/integration/permissions-management.js +7 -0
- package/dist/test/integration/permissions-management.js.map +1 -1
- package/dist/test/unit/asset-type.test.d.ts +1 -0
- package/dist/test/unit/asset-type.test.js +63 -0
- package/dist/test/unit/asset-type.test.js.map +1 -0
- package/frontend-vue/package.json +1 -1
- package/frontend-vue/pnpm-lock.yaml +11 -11
- package/package.json +10 -8
- package/src/test/antelope.test.ts +1 -0
- package/src/test/helpers/config.ts +14 -0
- package/src/test/helpers/delivery.ts +83 -0
- package/src/test/helpers/derivative-storage.ts +25 -0
- package/src/test/helpers/media-config.ts +16 -0
- package/src/test/helpers/member-rights.ts +116 -0
- package/src/test/helpers/png.ts +10 -0
- package/src/test/helpers/renderings.ts +147 -0
- package/src/test/helpers/uploads.ts +92 -0
- package/src/test/helpers/zip.ts +62 -0
- package/src/test/integration/media-activity-overview.test.ts +170 -0
- package/src/test/integration/media-api.test.ts +135 -1
- package/src/test/integration/media-bindings.test.ts +316 -2
- package/src/test/integration/media-bulk-writes.test.ts +180 -0
- package/src/test/integration/media-declared-rules.test.ts +405 -0
- package/src/test/integration/media-declared-sources.test.ts +228 -0
- package/src/test/integration/media-delivery-active-content.test.ts +185 -0
- package/src/test/integration/media-delivery-editing.test.ts +24 -46
- package/src/test/integration/media-delivery.test.ts +217 -1
- package/src/test/integration/media-explorer-writes.test.ts +253 -23
- package/src/test/integration/media-file-reads.test.ts +226 -0
- package/src/test/integration/media-guards.test.ts +1 -0
- package/src/test/integration/media-library-views.test.ts +478 -0
- package/src/test/integration/media-linked-subfolders.test.ts +2 -0
- package/src/test/integration/media-member-access.test.ts +146 -0
- package/src/test/integration/media-permissions.test.ts +10 -1
- package/src/test/integration/media-preset-delivery.test.ts +261 -0
- package/src/test/integration/media-presets.test.ts +304 -0
- package/src/test/integration/media-transform.test.ts +152 -10
- package/src/test/integration/media-upload-content.test.ts +192 -0
- package/src/test/integration/media-upload-policy.test.ts +20 -18
- package/src/test/integration/media-upload-types.test.ts +267 -0
- package/src/test/integration/permission-levels.ts +332 -0
- package/src/test/integration/permission-reach.ts +302 -0
- package/src/test/integration/permissions-management.ts +7 -0
- package/src/test/unit/asset-type.test.ts +87 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { GetModel } from "@antelopejs/interface-database-decorators";
|
|
2
|
+
import { DEFAULT_TENANT_ID } from "@antelopejs/interface-dms/constants";
|
|
3
|
+
import { DeleteFile } from "@antelopejs/interface-file-storage";
|
|
4
|
+
import type { AxiosInstance } from "axios";
|
|
5
|
+
import { assert, expect } from "chai";
|
|
6
|
+
import { getMediaConfig } from "../../config";
|
|
7
|
+
import { withMediaConfig } from "../helpers/media-config";
|
|
8
|
+
import { MediaAssetModel } from "../../db";
|
|
9
|
+
import { type UploadedAsset, uploadAsset } from "../helpers/assets";
|
|
10
|
+
import { createdBody, readBody } from "../helpers/bodies";
|
|
11
|
+
import { deliver, longText } from "../helpers/delivery";
|
|
12
|
+
import { authorizedClient } from "../helpers/http";
|
|
13
|
+
import { ensureOwnerSession } from "../helpers/owner";
|
|
14
|
+
|
|
15
|
+
const HTTP_OK = 200;
|
|
16
|
+
const HTTP_NOT_MODIFIED = 304;
|
|
17
|
+
const HTTP_PARTIAL_CONTENT = 206;
|
|
18
|
+
const HTTP_BAD_GATEWAY = 502;
|
|
19
|
+
const FOREIGN_ORIGIN = "https://shop.example.com";
|
|
20
|
+
const LONG_DESCRIPTION_LINES = 3000;
|
|
21
|
+
const SVG_SOURCE =
|
|
22
|
+
'<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"><script>alert(1)</script><circle cx="10" cy="10" r="9"/></svg>';
|
|
23
|
+
|
|
24
|
+
let client: AxiosInstance;
|
|
25
|
+
let token: string;
|
|
26
|
+
let folderId: string;
|
|
27
|
+
|
|
28
|
+
function assetPath(asset: UploadedAsset): string {
|
|
29
|
+
return `/media/${asset.id}/${encodeURIComponent(asset.name)}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function longSvg(): Buffer {
|
|
33
|
+
const opening = SVG_SOURCE.indexOf(">") + 1;
|
|
34
|
+
return Buffer.concat([
|
|
35
|
+
Buffer.from(SVG_SOURCE.slice(0, opening)),
|
|
36
|
+
Buffer.from("<desc>"),
|
|
37
|
+
longText(LONG_DESCRIPTION_LINES),
|
|
38
|
+
Buffer.from("</desc>"),
|
|
39
|
+
Buffer.from(SVG_SOURCE.slice(opening)),
|
|
40
|
+
]);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function describeActiveContent(): void {
|
|
44
|
+
describe("files the API streams itself", () => {
|
|
45
|
+
it("serves an SVG inline under a sandbox, with an ETag and no byte ranges", async () => {
|
|
46
|
+
const drawing = longSvg();
|
|
47
|
+
const svg = await uploadAsset(
|
|
48
|
+
client,
|
|
49
|
+
folderId,
|
|
50
|
+
"logo étoilé.svg",
|
|
51
|
+
"image/svg+xml",
|
|
52
|
+
drawing,
|
|
53
|
+
);
|
|
54
|
+
const auth = { Authorization: `Bearer ${token}` };
|
|
55
|
+
const read = await deliver(assetPath(svg), auth);
|
|
56
|
+
expect(read.status).to.equal(HTTP_OK);
|
|
57
|
+
expect(read.body.equals(drawing)).to.equal(true);
|
|
58
|
+
expect(read.headers.get("content-type")).to.match(/^image\/svg\+xml/);
|
|
59
|
+
expect(read.headers.get("content-disposition")).to.match(/^inline/);
|
|
60
|
+
expect(read.headers.get("content-security-policy")).to.equal("sandbox");
|
|
61
|
+
expect(read.headers.get("x-content-type-options")).to.equal("nosniff");
|
|
62
|
+
expect(read.headers.get("cache-control")).to.equal("private, no-cache");
|
|
63
|
+
const etag = read.headers.get("etag");
|
|
64
|
+
assert.isNotNull(etag, "etag");
|
|
65
|
+
const unchanged = await deliver(assetPath(svg), {
|
|
66
|
+
...auth,
|
|
67
|
+
"If-None-Match": etag,
|
|
68
|
+
});
|
|
69
|
+
expect(unchanged.status).to.equal(HTTP_NOT_MODIFIED);
|
|
70
|
+
expect(unchanged.body.byteLength).to.equal(0);
|
|
71
|
+
const other = await deliver(assetPath(svg), {
|
|
72
|
+
...auth,
|
|
73
|
+
"If-None-Match": '"other"',
|
|
74
|
+
});
|
|
75
|
+
expect(other.status).to.equal(HTTP_OK);
|
|
76
|
+
expect(other.body.byteLength).to.equal(drawing.byteLength);
|
|
77
|
+
const ranged = await deliver(assetPath(svg), {
|
|
78
|
+
...auth,
|
|
79
|
+
Range: "bytes=0-9",
|
|
80
|
+
});
|
|
81
|
+
expect(ranged.status).to.not.equal(HTTP_PARTIAL_CONTENT);
|
|
82
|
+
expect(ranged.status).to.equal(HTTP_OK);
|
|
83
|
+
expect(ranged.body.byteLength).to.equal(drawing.byteLength);
|
|
84
|
+
const head = await deliver(assetPath(svg), auth, "HEAD");
|
|
85
|
+
expect(head.status).to.equal(HTTP_OK);
|
|
86
|
+
expect(head.headers.get("content-length")).to.equal(
|
|
87
|
+
String(drawing.byteLength),
|
|
88
|
+
);
|
|
89
|
+
expect(head.body.byteLength).to.equal(0);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("serves a public SVG to any origin, still sandboxed", async () => {
|
|
93
|
+
const svg = await uploadAsset(
|
|
94
|
+
client,
|
|
95
|
+
folderId,
|
|
96
|
+
"open.svg",
|
|
97
|
+
"image/svg+xml",
|
|
98
|
+
Buffer.from(SVG_SOURCE),
|
|
99
|
+
);
|
|
100
|
+
const flip = await client.post(`/api/media/assets/${svg.id}/visibility`, {
|
|
101
|
+
visibility: "public",
|
|
102
|
+
});
|
|
103
|
+
expect(flip.status).to.equal(HTTP_OK);
|
|
104
|
+
const open = await deliver(assetPath(svg), { Origin: FOREIGN_ORIGIN });
|
|
105
|
+
expect(open.status).to.equal(HTTP_OK);
|
|
106
|
+
expect(open.headers.get("cache-control")).to.equal("public, no-cache");
|
|
107
|
+
expect(open.headers.get("access-control-allow-origin")).to.equal("*");
|
|
108
|
+
expect(open.headers.get("content-security-policy")).to.equal("sandbox");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("streams an XML file as a sandboxed attachment once its type is allowed", async () => {
|
|
112
|
+
const initial = getMediaConfig().allowedMimetypes;
|
|
113
|
+
await withMediaConfig(
|
|
114
|
+
{ allowedMimetypes: [...initial, "application/xml", "text/xml"] },
|
|
115
|
+
async () => {
|
|
116
|
+
const bytes = Buffer.concat([
|
|
117
|
+
Buffer.from('<?xml version="1.0" encoding="UTF-8"?>\n<notes>\n'),
|
|
118
|
+
longText(LONG_DESCRIPTION_LINES),
|
|
119
|
+
Buffer.from("</notes>\n"),
|
|
120
|
+
]);
|
|
121
|
+
const xml = await uploadAsset(
|
|
122
|
+
client,
|
|
123
|
+
folderId,
|
|
124
|
+
"flux.xml",
|
|
125
|
+
"application/xml",
|
|
126
|
+
bytes,
|
|
127
|
+
);
|
|
128
|
+
const read = await deliver(assetPath(xml), {
|
|
129
|
+
Authorization: `Bearer ${token}`,
|
|
130
|
+
});
|
|
131
|
+
expect(read.status).to.equal(HTTP_OK);
|
|
132
|
+
expect(read.headers.get("content-disposition")).to.match(
|
|
133
|
+
/^attachment/,
|
|
134
|
+
);
|
|
135
|
+
expect(read.headers.get("content-security-policy")).to.equal(
|
|
136
|
+
"sandbox",
|
|
137
|
+
);
|
|
138
|
+
expect(read.headers.get("x-content-type-options")).to.equal(
|
|
139
|
+
"nosniff",
|
|
140
|
+
);
|
|
141
|
+
expect(read.body.equals(bytes)).to.equal(true);
|
|
142
|
+
},
|
|
143
|
+
);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("answers 502 for an SVG whose stored file is gone", async () => {
|
|
147
|
+
const svg = await uploadAsset(
|
|
148
|
+
client,
|
|
149
|
+
folderId,
|
|
150
|
+
"gone.svg",
|
|
151
|
+
"image/svg+xml",
|
|
152
|
+
Buffer.from(SVG_SOURCE),
|
|
153
|
+
);
|
|
154
|
+
const stored = await GetModel(MediaAssetModel, DEFAULT_TENANT_ID).get(
|
|
155
|
+
svg.id,
|
|
156
|
+
);
|
|
157
|
+
assert.isDefined(stored, "stored asset");
|
|
158
|
+
await DeleteFile(stored.storageKey, stored.storage);
|
|
159
|
+
const read = await deliver(assetPath(svg), {
|
|
160
|
+
Authorization: `Bearer ${token}`,
|
|
161
|
+
});
|
|
162
|
+
expect(read.status).to.equal(HTTP_BAD_GATEWAY);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
describe("[integration] media delivery — active content", () => {
|
|
168
|
+
before(async function () {
|
|
169
|
+
this.timeout(40_000);
|
|
170
|
+
const session = await ensureOwnerSession();
|
|
171
|
+
token = session.accessToken;
|
|
172
|
+
client = authorizedClient(token);
|
|
173
|
+
const folder = await client.post("/api/media/folders", {
|
|
174
|
+
name: "Active content",
|
|
175
|
+
});
|
|
176
|
+
expect(folder.status).to.equal(HTTP_OK);
|
|
177
|
+
folderId = readBody(folder, createdBody).id;
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
after(async () => {
|
|
181
|
+
await client.delete(`/api/media/folders/${folderId}`);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
describeActiveContent();
|
|
185
|
+
});
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
} from "../../transform";
|
|
15
15
|
import { type UploadedAsset, uploadAsset } from "../helpers/assets";
|
|
16
16
|
import { createdBody, readBody } from "../helpers/bodies";
|
|
17
|
+
import { deliver, follow } from "../helpers/delivery";
|
|
17
18
|
import { authorizedClient, getBaseUrl } from "../helpers/http";
|
|
18
19
|
import { ensureOwnerSession } from "../helpers/owner";
|
|
19
20
|
|
|
@@ -41,44 +42,6 @@ const WIDE_SVG_SOURCE = `<svg xmlns="http://www.w3.org/2000/svg" width="${WIDE_S
|
|
|
41
42
|
const SVG_SOURCE =
|
|
42
43
|
'<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"><script>alert(1)</script><circle cx="10" cy="10" r="9"/></svg>';
|
|
43
44
|
|
|
44
|
-
interface Delivery {
|
|
45
|
-
status: number;
|
|
46
|
-
headers: Headers;
|
|
47
|
-
body: Buffer;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async function deliver(
|
|
51
|
-
path: string,
|
|
52
|
-
headers: Record<string, string> = {},
|
|
53
|
-
): Promise<Delivery> {
|
|
54
|
-
const response = await fetch(`${getBaseUrl()}${path}`, {
|
|
55
|
-
redirect: "manual",
|
|
56
|
-
headers,
|
|
57
|
-
});
|
|
58
|
-
return {
|
|
59
|
-
status: response.status,
|
|
60
|
-
headers: response.headers,
|
|
61
|
-
body: Buffer.from(await response.arrayBuffer()),
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** Follows the delivery redirect to the storage and returns the stored bytes. */
|
|
66
|
-
async function follow(
|
|
67
|
-
path: string,
|
|
68
|
-
headers: Record<string, string> = {},
|
|
69
|
-
): Promise<Delivery> {
|
|
70
|
-
const redirect = await deliver(path, headers);
|
|
71
|
-
expect(redirect.status, path).to.equal(HTTP_FOUND);
|
|
72
|
-
const location = redirect.headers.get("location");
|
|
73
|
-
assert.isNotNull(location, path);
|
|
74
|
-
const response = await fetch(location);
|
|
75
|
-
return {
|
|
76
|
-
status: response.status,
|
|
77
|
-
headers: response.headers,
|
|
78
|
-
body: Buffer.from(await response.arrayBuffer()),
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
|
|
82
45
|
function photo(): Promise<Buffer> {
|
|
83
46
|
return sharp({
|
|
84
47
|
create: {
|
|
@@ -167,15 +130,13 @@ function describeDeliveryLinks(): void {
|
|
|
167
130
|
it("answers a bare 401 to scripts and sends browsers to the file page", async () => {
|
|
168
131
|
const anonymous = await deliver(assetPath(jpeg));
|
|
169
132
|
expect(anonymous.status).to.equal(HTTP_UNAUTHORIZED);
|
|
133
|
+
expect(anonymous.body.toString()).to.not.contain("<html");
|
|
170
134
|
const browser = await deliver(assetPath(jpeg), { Accept: "text/html" });
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
browser.status === HTTP_UNAUTHORIZED &&
|
|
177
|
-
(browser.headers.get("content-type") ?? "").startsWith("text/html");
|
|
178
|
-
expect(isFilePageRedirect || isSignInPage).to.equal(true);
|
|
135
|
+
expect(browser.status).to.equal(HTTP_FOUND);
|
|
136
|
+
expect(browser.headers.get("location")).to.equal(
|
|
137
|
+
`${getBaseUrl()}/modules/media/file?asset=${jpeg.id}`,
|
|
138
|
+
);
|
|
139
|
+
expect(browser.headers.get("cache-control")).to.equal("no-store");
|
|
179
140
|
});
|
|
180
141
|
|
|
181
142
|
it("sends stale or empty file names to the canonical URL", async () => {
|
|
@@ -409,6 +370,8 @@ function describeUnusualFiles(): void {
|
|
|
409
370
|
function describeEdits(): void {
|
|
410
371
|
describe("copies, successive edits and replaced versions", () => {
|
|
411
372
|
it("saves an edit as a copy next to the untouched source", async () => {
|
|
373
|
+
const auth = { Authorization: `Bearer ${token}` };
|
|
374
|
+
const sourceBefore = await follow(assetPath(jpeg), auth);
|
|
412
375
|
const copy = await client.post(
|
|
413
376
|
`/api/media/assets/${jpeg.id}/transform/copy`,
|
|
414
377
|
{ crop: { x: 0, y: 0, width: 0.5, height: 1 } },
|
|
@@ -423,6 +386,21 @@ function describeEdits(): void {
|
|
|
423
386
|
const source = await client.get(`/api/media/assets/${jpeg.id}`);
|
|
424
387
|
expect(source.data.asset.width).to.equal(PHOTO_WIDTH);
|
|
425
388
|
expect(source.data.asset.hasOriginal).to.equal(false);
|
|
389
|
+
const sourceAfter = await follow(assetPath(jpeg), auth);
|
|
390
|
+
expect(sourceAfter.body.equals(sourceBefore.body)).to.equal(true);
|
|
391
|
+
const copyId = readBody(copy, z.object({ asset: createdBody })).asset.id;
|
|
392
|
+
const served = await follow(`/media/${copyId}/photo%20(2).jpg`, auth);
|
|
393
|
+
const metadata = await sharp(served.body).metadata();
|
|
394
|
+
expect([metadata.width, metadata.height]).to.deep.equal([
|
|
395
|
+
PHOTO_WIDTH / 2,
|
|
396
|
+
PHOTO_HEIGHT,
|
|
397
|
+
]);
|
|
398
|
+
const listing = await client.get(`/api/media/folders/${folderId}/assets`);
|
|
399
|
+
const listed = readBody(
|
|
400
|
+
listing,
|
|
401
|
+
z.object({ assets: z.array(createdBody) }),
|
|
402
|
+
).assets.map((asset) => asset.id);
|
|
403
|
+
expect(listed).to.include.members([jpeg.id, copyId]);
|
|
426
404
|
});
|
|
427
405
|
|
|
428
406
|
it("renders successive edits from the original and resets on revert", async () => {
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import type { AxiosInstance } from "axios";
|
|
2
2
|
import { assert, expect } from "chai";
|
|
3
3
|
import { z } from "zod";
|
|
4
|
+
import sharp from "sharp";
|
|
4
5
|
import {
|
|
5
6
|
ONE_PIXEL_PNG,
|
|
6
7
|
PNG_NAME,
|
|
7
8
|
rawGet,
|
|
9
|
+
uploadAsset,
|
|
8
10
|
uploadPixelPng,
|
|
9
11
|
} from "../helpers/assets";
|
|
10
12
|
import { createdBody, readBody, urlBody } from "../helpers/bodies";
|
|
11
|
-
import {
|
|
13
|
+
import { deliver, longText, signedLifetimeSeconds } from "../helpers/delivery";
|
|
14
|
+
import { authorizedClient, getBaseUrl } from "../helpers/http";
|
|
12
15
|
import { ensureOwnerSession } from "../helpers/owner";
|
|
13
16
|
|
|
14
17
|
const HTTP_OK = 200;
|
|
@@ -160,3 +163,216 @@ describe("[integration] media delivery — visibility and derivatives", () => {
|
|
|
160
163
|
expect(response.status).to.equal(HTTP_UNAUTHORIZED);
|
|
161
164
|
});
|
|
162
165
|
});
|
|
166
|
+
|
|
167
|
+
const PRIVATE_SIGNED_MAX_SECONDS = 300;
|
|
168
|
+
const PRIVATE_SIGNED_MIN_SECONDS = 150;
|
|
169
|
+
const PUBLIC_SIGNED_MAX_SECONDS = 3600;
|
|
170
|
+
const PUBLIC_SIGNED_MIN_SECONDS = 1800;
|
|
171
|
+
const BINARY_BYTES = 3 * 1024 * 1024;
|
|
172
|
+
const TEXT_LINES = 4000;
|
|
173
|
+
const FOREIGN_ORIGIN = "https://site.example.org";
|
|
174
|
+
const RENAMED = "après été.png";
|
|
175
|
+
|
|
176
|
+
let contractClient: AxiosInstance;
|
|
177
|
+
let contractToken = "";
|
|
178
|
+
function asOwner() {
|
|
179
|
+
return { Authorization: `Bearer ${contractToken}` };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const contractFolderIds: string[] = [];
|
|
183
|
+
|
|
184
|
+
async function newFolder(name: string): Promise<string> {
|
|
185
|
+
const response = await contractClient.post("/api/media/folders", { name });
|
|
186
|
+
expect(response.status).to.equal(HTTP_OK);
|
|
187
|
+
const { id } = readBody(response, createdBody);
|
|
188
|
+
contractFolderIds.push(id);
|
|
189
|
+
return id;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async function setVisibility(
|
|
193
|
+
kind: "folders" | "assets",
|
|
194
|
+
id: string,
|
|
195
|
+
visibility: string,
|
|
196
|
+
): Promise<void> {
|
|
197
|
+
const response = await contractClient.post(
|
|
198
|
+
`/api/media/${kind}/${id}/visibility`,
|
|
199
|
+
{ visibility },
|
|
200
|
+
);
|
|
201
|
+
expect(response.status).to.equal(HTTP_OK);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function solidPng(width: number): Promise<Buffer> {
|
|
205
|
+
return sharp({
|
|
206
|
+
create: { width, height: width, channels: 3, background: "#3366cc" },
|
|
207
|
+
})
|
|
208
|
+
.png()
|
|
209
|
+
.toBuffer();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function linkOf(asset: { id: string; name: string }): string {
|
|
213
|
+
return `/media/${asset.id}/${encodeURIComponent(asset.name)}`;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function describeLinkAnswers(): void {
|
|
217
|
+
describe("what a link answers", () => {
|
|
218
|
+
it("redirects a public file to a signed URL that serves every byte, and keeps the CORS answer of an allowed origin", async () => {
|
|
219
|
+
const folderId = await newFolder("Public bytes");
|
|
220
|
+
const bytes = Buffer.from(
|
|
221
|
+
Array.from(
|
|
222
|
+
{ length: BINARY_BYTES },
|
|
223
|
+
(_, index) => (index * 31 + (index >> 11)) % 256,
|
|
224
|
+
),
|
|
225
|
+
);
|
|
226
|
+
const file = await uploadAsset(
|
|
227
|
+
contractClient,
|
|
228
|
+
folderId,
|
|
229
|
+
"Données été 東京.bin",
|
|
230
|
+
"application/pdf",
|
|
231
|
+
bytes,
|
|
232
|
+
);
|
|
233
|
+
await setVisibility("folders", folderId, "public");
|
|
234
|
+
const anonymous = await deliver(linkOf(file), {
|
|
235
|
+
Origin: FOREIGN_ORIGIN,
|
|
236
|
+
});
|
|
237
|
+
expect(anonymous.status).to.equal(HTTP_FOUND);
|
|
238
|
+
expect(anonymous.headers.get("cache-control")).to.equal("no-cache");
|
|
239
|
+
expect(anonymous.headers.get("access-control-allow-origin")).to.equal(
|
|
240
|
+
"*",
|
|
241
|
+
);
|
|
242
|
+
const location = anonymous.headers.get("location") ?? "";
|
|
243
|
+
expect(location).to.not.contain("/media/");
|
|
244
|
+
expect((await fetchBytes(location)).equals(bytes)).to.equal(true);
|
|
245
|
+
const allowed = await deliver(linkOf(file), { Origin: getBaseUrl() });
|
|
246
|
+
expect(allowed.headers.get("access-control-allow-origin")).to.equal(
|
|
247
|
+
getBaseUrl(),
|
|
248
|
+
);
|
|
249
|
+
expect(allowed.headers.get("cache-control")).to.equal("no-cache");
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("keeps a private file behind its reader: no open CORS answer, no HTML for a script, the same text bytes", async () => {
|
|
253
|
+
const folderId = await newFolder("Private text");
|
|
254
|
+
const bytes = longText(TEXT_LINES);
|
|
255
|
+
const file = await uploadAsset(
|
|
256
|
+
contractClient,
|
|
257
|
+
folderId,
|
|
258
|
+
"notes privées.txt",
|
|
259
|
+
"text/plain",
|
|
260
|
+
bytes,
|
|
261
|
+
);
|
|
262
|
+
const script = await deliver(linkOf(file), {
|
|
263
|
+
Accept: "application/json",
|
|
264
|
+
});
|
|
265
|
+
expect(script.status).to.equal(HTTP_UNAUTHORIZED);
|
|
266
|
+
expect(script.body.toString()).to.not.contain("<html");
|
|
267
|
+
const owner = await deliver(linkOf(file), asOwner());
|
|
268
|
+
expect(owner.status).to.equal(HTTP_FOUND);
|
|
269
|
+
expect(owner.headers.get("cache-control")).to.equal("no-store");
|
|
270
|
+
expect(owner.headers.get("access-control-allow-origin")).to.not.equal(
|
|
271
|
+
"*",
|
|
272
|
+
);
|
|
273
|
+
const stored = await fetchBytes(owner.headers.get("location") ?? "");
|
|
274
|
+
expect(stored.equals(bytes)).to.equal(true);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it("redirects a stale file name to the canonical link after a rename and refuses a link without a name", async () => {
|
|
278
|
+
const folderId = await newFolder("Stale names");
|
|
279
|
+
const file = await uploadAsset(
|
|
280
|
+
contractClient,
|
|
281
|
+
folderId,
|
|
282
|
+
"avant.png",
|
|
283
|
+
"image/png",
|
|
284
|
+
await solidPng(8),
|
|
285
|
+
);
|
|
286
|
+
const rename = await contractClient.post(
|
|
287
|
+
`/api/media/assets/${file.id}/update`,
|
|
288
|
+
{ name: RENAMED },
|
|
289
|
+
);
|
|
290
|
+
expect(rename.status).to.equal(HTTP_OK);
|
|
291
|
+
const canonical = `/media/${file.id}/${encodeURIComponent(RENAMED)}`;
|
|
292
|
+
const old = await deliver(linkOf(file), asOwner());
|
|
293
|
+
expect(old.status).to.equal(HTTP_FOUND);
|
|
294
|
+
expect(old.headers.get("location")).to.equal(canonical);
|
|
295
|
+
expect(old.headers.get("cache-control")).to.equal("no-cache");
|
|
296
|
+
const current = await deliver(canonical, asOwner());
|
|
297
|
+
expect(current.status).to.equal(HTTP_FOUND);
|
|
298
|
+
expect(current.headers.get("location")).to.contain("/file-storage/");
|
|
299
|
+
const nameless = await deliver(`/media/${file.id}/`, asOwner());
|
|
300
|
+
expect(nameless.status).to.equal(HTTP_NOT_FOUND);
|
|
301
|
+
const unknown = await deliver("/media/no-such-asset/x.png", asOwner());
|
|
302
|
+
expect(unknown.status).to.equal(HTTP_NOT_FOUND);
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function describeLinkLifetimes(): void {
|
|
308
|
+
describe("how long a link's signed URL lives", () => {
|
|
309
|
+
it("signs a private file's URL for five minutes and a public one's for an hour", async () => {
|
|
310
|
+
const folderId = await newFolder("Signed lifetimes");
|
|
311
|
+
const file = await uploadAsset(
|
|
312
|
+
contractClient,
|
|
313
|
+
folderId,
|
|
314
|
+
"signed.png",
|
|
315
|
+
"image/png",
|
|
316
|
+
await solidPng(8),
|
|
317
|
+
);
|
|
318
|
+
const first = await deliver(linkOf(file), asOwner());
|
|
319
|
+
const second = await deliver(linkOf(file), asOwner());
|
|
320
|
+
const privateUrl = first.headers.get("location") ?? "";
|
|
321
|
+
expect(second.headers.get("location")).to.equal(privateUrl);
|
|
322
|
+
expect(await signedLifetimeSeconds(privateUrl)).to.be.within(
|
|
323
|
+
PRIVATE_SIGNED_MIN_SECONDS + 1,
|
|
324
|
+
PRIVATE_SIGNED_MAX_SECONDS,
|
|
325
|
+
);
|
|
326
|
+
await setVisibility("assets", file.id, "public");
|
|
327
|
+
const open = await deliver(linkOf(file));
|
|
328
|
+
const publicUrl = open.headers.get("location") ?? "";
|
|
329
|
+
expect(publicUrl).to.not.equal(privateUrl);
|
|
330
|
+
expect(await signedLifetimeSeconds(publicUrl)).to.be.within(
|
|
331
|
+
PUBLIC_SIGNED_MIN_SECONDS + 1,
|
|
332
|
+
PUBLIC_SIGNED_MAX_SECONDS,
|
|
333
|
+
);
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
it("takes the file's visibility, else its folder's, else private, and never changes the link", async () => {
|
|
337
|
+
const folderId = await newFolder("Visibility order");
|
|
338
|
+
const file = await uploadAsset(
|
|
339
|
+
contractClient,
|
|
340
|
+
folderId,
|
|
341
|
+
"vis.png",
|
|
342
|
+
"image/png",
|
|
343
|
+
await solidPng(8),
|
|
344
|
+
);
|
|
345
|
+
const anonymousStatus = async (): Promise<number> =>
|
|
346
|
+
(await deliver(linkOf(file))).status;
|
|
347
|
+
expect(await anonymousStatus()).to.equal(HTTP_UNAUTHORIZED);
|
|
348
|
+
await setVisibility("folders", folderId, "public");
|
|
349
|
+
expect(await anonymousStatus()).to.equal(HTTP_FOUND);
|
|
350
|
+
await setVisibility("assets", file.id, "private");
|
|
351
|
+
expect(await anonymousStatus()).to.equal(HTTP_UNAUTHORIZED);
|
|
352
|
+
await setVisibility("folders", folderId, "private");
|
|
353
|
+
await setVisibility("assets", file.id, "public");
|
|
354
|
+
expect(await anonymousStatus()).to.equal(HTTP_FOUND);
|
|
355
|
+
await setVisibility("assets", file.id, "inherit");
|
|
356
|
+
expect(await anonymousStatus()).to.equal(HTTP_UNAUTHORIZED);
|
|
357
|
+
const details = await contractClient.get(`/api/media/assets/${file.id}`);
|
|
358
|
+
expect(details.data.asset.url).to.equal(file.url);
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
describe("[integration] media delivery — the link contract", () => {
|
|
364
|
+
before(async function () {
|
|
365
|
+
this.timeout(40_000);
|
|
366
|
+
const session = await ensureOwnerSession();
|
|
367
|
+
contractClient = authorizedClient(session.accessToken);
|
|
368
|
+
contractToken = session.accessToken;
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
after(async () => {
|
|
372
|
+
for (const id of contractFolderIds)
|
|
373
|
+
await contractClient.delete(`/api/media/folders/${id}`);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
describeLinkAnswers();
|
|
377
|
+
describeLinkLifetimes();
|
|
378
|
+
});
|