Skip to content

Commit

Permalink
Check first 1kb of unidentified upload for text, set text/plain if so
Browse files Browse the repository at this point in the history
  • Loading branch information
nakedmcse committed Sep 15, 2024
1 parent 895f8d0 commit 40f644d
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/services/MimeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ import { Constant, Service } from "@tsed/di";
import mime from "mime";
import GlobalEnv from "../model/constants/GlobalEnv.js";
import { fileTypeFromBuffer, fileTypeFromFile } from "file-type";
import fs from "node:fs/promises";

@Service()
export class MimeService {
@Constant(GlobalEnv.BLOCKED_MIME_TYPES)
private readonly blockedMimeTypes: string;

public async readFirstKB(filePath: string): Promise<Buffer> {
const fileHandle = await fs.open(filePath, "r");
const buff = Buffer.alloc(1024);
try {
await fileHandle.read(buff, 0, 1024, 0);
} finally {
await fileHandle.close();
}
return buff;
}

public async isBlocked(filepath: string): Promise<boolean> {
if (!this.blockedMimeTypes) {
return false;
Expand All @@ -26,7 +38,14 @@ export class MimeService {
return mimeFromBuffer.mime;
}
if (resourceName) {
return mime.getType(resourceName);
const extType = mime.getType(resourceName);
if (extType) {
return extType;
}
}
const isText = !buff.toString("utf-8", 0, 1024).includes("\uFFFD");
if (isText) {
return "text/plain";
}
return null;
}
Expand All @@ -40,6 +59,15 @@ export class MimeService {
} catch {
return null;
}
return mime.getType(filepath);
const extType = mime.getType(filepath);
if (extType) {
return extType;
}
const firstKb = await this.readFirstKB(filepath);
const isText = !firstKb.toString("utf-8", 0, 1024).includes("\uFFFD");
if (isText) {
return "text/plain";
}
return null;
}
}

0 comments on commit 40f644d

Please sign in to comment.