summary refs log tree commit diff
path: root/src/cdn
diff options
context:
space:
mode:
authorPuyodead1 <puyodead@proton.me>2025-04-16 23:28:43 -0400
committerRory& <root@rory.gay>2025-05-03 12:30:22 +0200
commit080b2c7d383b5e09fee97f267d35f4e7cd22f0a9 (patch)
tree066bfcba19fdc7060d28d46db8b1218c0e365011 /src/cdn
parentMerge pull request #1274 from dank074/patch/fix-requestGuildMembers-libraires (diff)
downloadserver-ts-080b2c7d383b5e09fee97f267d35f4e7cd22f0a9.tar.xz
Implement signed cdn urls
Diffstat (limited to 'src/cdn')
-rw-r--r--src/cdn/routes/attachments.ts32
1 files changed, 27 insertions, 5 deletions
diff --git a/src/cdn/routes/attachments.ts b/src/cdn/routes/attachments.ts

index 19bb0b90..3b79e7f8 100644 --- a/src/cdn/routes/attachments.ts +++ b/src/cdn/routes/attachments.ts
@@ -16,13 +16,18 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { Router, Response, Request } from "express"; -import { Config, Snowflake } from "@spacebar/util"; -import { storage } from "../util/Storage"; +import { + Config, + getUrlSignature, + hasValidSignature, + Snowflake, +} from "@spacebar/util"; +import { Request, Response, Router } from "express"; import FileType from "file-type"; +import imageSize from "image-size"; import { HTTPError } from "lambert-server"; import { multer } from "../util/multer"; -import imageSize from "image-size"; +import { storage } from "../util/Storage"; const router = Router(); @@ -39,6 +44,7 @@ router.post( async (req: Request, res: Response) => { if (req.headers.signature !== Config.get().security.requestSignature) throw new HTTPError("Invalid request signature"); + if (!req.file) throw new HTTPError("file missing"); const { buffer, mimetype, size, originalname } = req.file; @@ -63,12 +69,20 @@ router.post( } } + let finalUrl = `${endpoint}/${path}`; + + if (Config.get().security.cdnSignUrls) { + const signatureData = getUrlSignature(path); + console.log(signatureData); + finalUrl = `${finalUrl}?ex=${signatureData.expiresAt}&is=${signatureData.issuedAt}&hm=${signatureData.hash}&`; + } + const file = { id, content_type: mimetype, filename: filename, size, - url: `${endpoint}/${path}`, + url: finalUrl, width, height, }; @@ -84,6 +98,14 @@ router.get( // const { format } = req.query; const path = `attachments/${channel_id}/${id}/${filename}`; + + if ( + Config.get().security.cdnSignUrls && + !hasValidSignature(path, req.query) + ) { + return res.status(404).send("This content is no longer available."); + } + const file = await storage.get(path); if (!file) throw new HTTPError("File not found"); const type = await FileType.fromBuffer(file);