@terreno/admin-backend 57.8.0 → 57.9.0
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/documentStorageApp.d.ts +6 -0
- package/dist/documentStorageApp.js +2 -0
- package/dist/fileUploadsGate.d.ts +8 -0
- package/dist/fileUploadsGate.js +18 -0
- package/package.json +4 -4
- package/src/documentStorageApp.test.ts +17 -0
- package/src/documentStorageApp.ts +8 -0
- package/src/fileUploadsGate.ts +23 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type AdminContribution, type TerrenoPlugin } from "@terreno/api";
|
|
2
2
|
import type express from "express";
|
|
3
|
+
import { type FileUploadsEnabled } from "./fileUploadsGate";
|
|
3
4
|
export interface DocumentStorageOptions {
|
|
4
5
|
bucketName: string;
|
|
5
6
|
folderPrefix?: string;
|
|
@@ -7,6 +8,11 @@ export interface DocumentStorageOptions {
|
|
|
7
8
|
allowedMimeTypes?: string[];
|
|
8
9
|
maxFileSize?: number;
|
|
9
10
|
signedUrlExpiration?: number;
|
|
11
|
+
/**
|
|
12
|
+
* When `false` or the function returns `false`, `POST` uploads are rejected.
|
|
13
|
+
* Listing, download, and delete stay available. Omit or pass `true` to leave uploads enabled.
|
|
14
|
+
*/
|
|
15
|
+
fileUploadsEnabled?: FileUploadsEnabled;
|
|
10
16
|
}
|
|
11
17
|
export interface DocumentFile {
|
|
12
18
|
name: string;
|
|
@@ -9,6 +9,7 @@ const storage_1 = require("@google-cloud/storage");
|
|
|
9
9
|
const api_1 = require("@terreno/api");
|
|
10
10
|
const luxon_1 = require("luxon");
|
|
11
11
|
const multer_1 = __importDefault(require("multer"));
|
|
12
|
+
const fileUploadsGate_1 = require("./fileUploadsGate");
|
|
12
13
|
// Express 5 wildcard route params (e.g. `*filepath`) come through as string[]
|
|
13
14
|
// when the URL has one or more path segments. Older signatures (and manual
|
|
14
15
|
// calls) may still provide a string. Normalize both shapes to a single
|
|
@@ -137,6 +138,7 @@ class DocumentStorageApp {
|
|
|
137
138
|
}));
|
|
138
139
|
// POST basePath/ — Upload a file
|
|
139
140
|
app.post(`${basePath}/`, ...adminGuard, upload.single("file"), (0, api_1.asyncHandler)(async (req, res) => {
|
|
141
|
+
await (0, fileUploadsGate_1.assertFileUploadsEnabled)(req, this.options.fileUploadsEnabled);
|
|
140
142
|
const file = req.file;
|
|
141
143
|
if (!file) {
|
|
142
144
|
throw new api_1.APIError({ status: 400, title: "No file provided" });
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type express from "express";
|
|
2
|
+
/** Static switch, or a per-request check (for example a feature flag). */
|
|
3
|
+
export type FileUploadsEnabled = boolean | ((req: express.Request) => boolean | Promise<boolean>);
|
|
4
|
+
/**
|
|
5
|
+
* Reject the request when file uploads are turned off.
|
|
6
|
+
* Missing or `true` leaves uploads enabled.
|
|
7
|
+
*/
|
|
8
|
+
export declare const assertFileUploadsEnabled: (req: express.Request, fileUploadsEnabled: FileUploadsEnabled | undefined) => Promise<void>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assertFileUploadsEnabled = void 0;
|
|
4
|
+
const api_1 = require("@terreno/api");
|
|
5
|
+
/**
|
|
6
|
+
* Reject the request when file uploads are turned off.
|
|
7
|
+
* Missing or `true` leaves uploads enabled.
|
|
8
|
+
*/
|
|
9
|
+
const assertFileUploadsEnabled = async (req, fileUploadsEnabled) => {
|
|
10
|
+
if (fileUploadsEnabled === undefined || fileUploadsEnabled === true) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const allowed = typeof fileUploadsEnabled === "function" ? await fileUploadsEnabled(req) : fileUploadsEnabled;
|
|
14
|
+
if (!allowed) {
|
|
15
|
+
throw new api_1.APIError({ status: 403, title: "File uploads are disabled" });
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
exports.assertFileUploadsEnabled = assertFileUploadsEnabled;
|
package/package.json
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@google-cloud/storage": "^7.15.0",
|
|
8
|
-
"@terreno/api": "57.
|
|
9
|
-
"@terreno/jobs": "57.
|
|
8
|
+
"@terreno/api": "57.9.0",
|
|
9
|
+
"@terreno/jobs": "57.9.0",
|
|
10
10
|
"express": "^5.2.1",
|
|
11
11
|
"luxon": "^3.7.2",
|
|
12
12
|
"multer": "2.2.0"
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"description": "Admin panel backend plugin for @terreno/api — part of Terreno, the batteries-included TypeScript framework for universal apps.",
|
|
15
15
|
"devDependencies": {
|
|
16
16
|
"@biomejs/biome": "^2.3.6",
|
|
17
|
-
"@terreno/test": "57.
|
|
17
|
+
"@terreno/test": "57.9.0",
|
|
18
18
|
"@types/bun": "^1.4.0",
|
|
19
19
|
"@types/express": "^5.0.6",
|
|
20
20
|
"@types/luxon": "^3.7.1",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"test:coverage": "bun run ../scripts/check-coverage.ts"
|
|
56
56
|
},
|
|
57
57
|
"types": "dist/index.d.ts",
|
|
58
|
-
"version": "57.
|
|
58
|
+
"version": "57.9.0"
|
|
59
59
|
}
|
|
@@ -196,6 +196,23 @@ describe("DocumentStorageApp", () => {
|
|
|
196
196
|
expect(saved.body?.toString()).toBe("hello");
|
|
197
197
|
});
|
|
198
198
|
|
|
199
|
+
it("rejects uploads when file uploads are disabled", async () => {
|
|
200
|
+
app = buildApp({
|
|
201
|
+
bucketName: "test-bucket",
|
|
202
|
+
fileUploadsEnabled: async () => false,
|
|
203
|
+
folderPrefix: "tenant/",
|
|
204
|
+
});
|
|
205
|
+
const agent = await authAsUser(app, "admin");
|
|
206
|
+
const res = await agent
|
|
207
|
+
.post("/documents/")
|
|
208
|
+
.attach("file", Buffer.from("hello"), {
|
|
209
|
+
contentType: "application/pdf",
|
|
210
|
+
filename: "notes.pdf",
|
|
211
|
+
})
|
|
212
|
+
.expect(403);
|
|
213
|
+
expect(res.body.title).toBe("File uploads are disabled");
|
|
214
|
+
});
|
|
215
|
+
|
|
199
216
|
it("rejects disallowed mime types via multer fileFilter", async () => {
|
|
200
217
|
await adminAgent
|
|
201
218
|
.post("/documents/")
|
|
@@ -12,6 +12,8 @@ import type express from "express";
|
|
|
12
12
|
import {DateTime} from "luxon";
|
|
13
13
|
import multer from "multer";
|
|
14
14
|
|
|
15
|
+
import {assertFileUploadsEnabled, type FileUploadsEnabled} from "./fileUploadsGate";
|
|
16
|
+
|
|
15
17
|
export interface DocumentStorageOptions {
|
|
16
18
|
bucketName: string;
|
|
17
19
|
folderPrefix?: string;
|
|
@@ -19,6 +21,11 @@ export interface DocumentStorageOptions {
|
|
|
19
21
|
allowedMimeTypes?: string[];
|
|
20
22
|
maxFileSize?: number;
|
|
21
23
|
signedUrlExpiration?: number;
|
|
24
|
+
/**
|
|
25
|
+
* When `false` or the function returns `false`, `POST` uploads are rejected.
|
|
26
|
+
* Listing, download, and delete stay available. Omit or pass `true` to leave uploads enabled.
|
|
27
|
+
*/
|
|
28
|
+
fileUploadsEnabled?: FileUploadsEnabled;
|
|
22
29
|
}
|
|
23
30
|
|
|
24
31
|
export interface DocumentFile {
|
|
@@ -194,6 +201,7 @@ export class DocumentStorageApp implements TerrenoPlugin {
|
|
|
194
201
|
...adminGuard,
|
|
195
202
|
upload.single("file") as unknown as express.RequestHandler,
|
|
196
203
|
asyncHandler(async (req: express.Request, res: express.Response) => {
|
|
204
|
+
await assertFileUploadsEnabled(req, this.options.fileUploadsEnabled);
|
|
197
205
|
const file = (req as unknown as {file?: Express.Multer.File}).file;
|
|
198
206
|
if (!file) {
|
|
199
207
|
throw new APIError({status: 400, title: "No file provided"});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {APIError} from "@terreno/api";
|
|
2
|
+
import type express from "express";
|
|
3
|
+
|
|
4
|
+
/** Static switch, or a per-request check (for example a feature flag). */
|
|
5
|
+
export type FileUploadsEnabled = boolean | ((req: express.Request) => boolean | Promise<boolean>);
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Reject the request when file uploads are turned off.
|
|
9
|
+
* Missing or `true` leaves uploads enabled.
|
|
10
|
+
*/
|
|
11
|
+
export const assertFileUploadsEnabled = async (
|
|
12
|
+
req: express.Request,
|
|
13
|
+
fileUploadsEnabled: FileUploadsEnabled | undefined
|
|
14
|
+
): Promise<void> => {
|
|
15
|
+
if (fileUploadsEnabled === undefined || fileUploadsEnabled === true) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const allowed =
|
|
19
|
+
typeof fileUploadsEnabled === "function" ? await fileUploadsEnabled(req) : fileUploadsEnabled;
|
|
20
|
+
if (!allowed) {
|
|
21
|
+
throw new APIError({status: 403, title: "File uploads are disabled"});
|
|
22
|
+
}
|
|
23
|
+
};
|