Compare commits
6 Commits
edc1ab2438
...
863a4bf528
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
863a4bf528
|
||
|
|
9a1cdb05a4
|
||
|
|
28caf92f9a
|
||
|
|
8b2728dc5a
|
||
|
|
3bbbbc307f
|
||
|
|
f080919563
|
@@ -77,6 +77,8 @@ import { UsersModule } from "./users/users.module";
|
|||||||
})
|
})
|
||||||
export class AppModule implements NestModule {
|
export class AppModule implements NestModule {
|
||||||
configure(consumer: MiddlewareConsumer) {
|
configure(consumer: MiddlewareConsumer) {
|
||||||
consumer.apply(HTTPLoggerMiddleware, CrawlerDetectionMiddleware).forRoutes("*");
|
consumer
|
||||||
|
.apply(HTTPLoggerMiddleware, CrawlerDetectionMiddleware)
|
||||||
|
.forRoutes("*");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -169,7 +169,9 @@ export class AuthService {
|
|||||||
|
|
||||||
const isValid = authenticator.verify({ token, secret });
|
const isValid = authenticator.verify({ token, secret });
|
||||||
if (!isValid) {
|
if (!isValid) {
|
||||||
this.logger.warn(`2FA verification failed for user ${userId}: invalid token`);
|
this.logger.warn(
|
||||||
|
`2FA verification failed for user ${userId}: invalid token`,
|
||||||
|
);
|
||||||
throw new UnauthorizedException("Invalid 2FA token");
|
throw new UnauthorizedException("Invalid 2FA token");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
|
import { createHash } from "node:crypto";
|
||||||
import { Injectable, Logger, NestMiddleware } from "@nestjs/common";
|
import { Injectable, Logger, NestMiddleware } from "@nestjs/common";
|
||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from "express";
|
||||||
import { createHash } from "node:crypto";
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class HTTPLoggerMiddleware implements NestMiddleware {
|
export class HTTPLoggerMiddleware implements NestMiddleware {
|
||||||
@@ -16,7 +16,7 @@ export class HTTPLoggerMiddleware implements NestMiddleware {
|
|||||||
const contentLength = response.get("content-length");
|
const contentLength = response.get("content-length");
|
||||||
const duration = Date.now() - startTime;
|
const duration = Date.now() - startTime;
|
||||||
|
|
||||||
const hashedIp = createHash("sha256").update(ip).digest("hex");
|
const hashedIp = createHash("sha256").update(ip as string).digest("hex");
|
||||||
const message = `${method} ${originalUrl} ${statusCode} ${contentLength || 0} - ${userAgent} ${hashedIp} +${duration}ms`;
|
const message = `${method} ${originalUrl} ${statusCode} ${contentLength || 0} - ${userAgent} ${hashedIp} +${duration}ms`;
|
||||||
|
|
||||||
if (statusCode >= 500) {
|
if (statusCode >= 500) {
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import { Readable } from "node:stream";
|
import { Readable } from "node:stream";
|
||||||
import { NotFoundException } from "@nestjs/common";
|
import { NotFoundException } from "@nestjs/common";
|
||||||
import { Test, TestingModule } from "@nestjs/testing";
|
import { Test, TestingModule } from "@nestjs/testing";
|
||||||
|
import type { Response } from "express";
|
||||||
import { S3Service } from "../s3/s3.service";
|
import { S3Service } from "../s3/s3.service";
|
||||||
import { MediaController } from "./media.controller";
|
import { MediaController } from "./media.controller";
|
||||||
|
|
||||||
describe("MediaController", () => {
|
describe("MediaController", () => {
|
||||||
let controller: MediaController;
|
let controller: MediaController;
|
||||||
let s3Service: S3Service;
|
|
||||||
|
|
||||||
const mockS3Service = {
|
const mockS3Service = {
|
||||||
getFileInfo: jest.fn(),
|
getFileInfo: jest.fn(),
|
||||||
@@ -20,7 +20,6 @@ describe("MediaController", () => {
|
|||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
controller = module.get<MediaController>(MediaController);
|
controller = module.get<MediaController>(MediaController);
|
||||||
s3Service = module.get<S3Service>(S3Service);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should be defined", () => {
|
it("should be defined", () => {
|
||||||
@@ -31,7 +30,7 @@ describe("MediaController", () => {
|
|||||||
it("should stream the file and set headers with path containing slashes", async () => {
|
it("should stream the file and set headers with path containing slashes", async () => {
|
||||||
const res = {
|
const res = {
|
||||||
setHeader: jest.fn(),
|
setHeader: jest.fn(),
|
||||||
} as any;
|
} as unknown as Response;
|
||||||
const stream = new Readable();
|
const stream = new Readable();
|
||||||
stream.pipe = jest.fn();
|
stream.pipe = jest.fn();
|
||||||
const key = "contents/user-id/test.webp";
|
const key = "contents/user-id/test.webp";
|
||||||
@@ -52,7 +51,7 @@ describe("MediaController", () => {
|
|||||||
|
|
||||||
it("should throw NotFoundException if file is not found", async () => {
|
it("should throw NotFoundException if file is not found", async () => {
|
||||||
mockS3Service.getFileInfo.mockRejectedValue(new Error("Not found"));
|
mockS3Service.getFileInfo.mockRejectedValue(new Error("Not found"));
|
||||||
const res = {} as any;
|
const res = {} as unknown as Response;
|
||||||
|
|
||||||
await expect(controller.getFile("invalid", res)).rejects.toThrow(
|
await expect(controller.getFile("invalid", res)).rejects.toThrow(
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Controller, Get, NotFoundException, Param, Res } from "@nestjs/common";
|
import { Controller, Get, NotFoundException, Param, Res } from "@nestjs/common";
|
||||||
import type { Response } from "express";
|
import type { Response } from "express";
|
||||||
|
import type { BucketItemStat } from "minio";
|
||||||
import { S3Service } from "../s3/s3.service";
|
import { S3Service } from "../s3/s3.service";
|
||||||
|
|
||||||
@Controller("media")
|
@Controller("media")
|
||||||
@@ -9,7 +10,7 @@ export class MediaController {
|
|||||||
@Get("*key")
|
@Get("*key")
|
||||||
async getFile(@Param("key") key: string, @Res() res: Response) {
|
async getFile(@Param("key") key: string, @Res() res: Response) {
|
||||||
try {
|
try {
|
||||||
const stats = (await this.s3Service.getFileInfo(key)) as any;
|
const stats = (await this.s3Service.getFileInfo(key)) as BucketItemStat;
|
||||||
const stream = await this.s3Service.getFile(key);
|
const stream = await this.s3Service.getFile(key);
|
||||||
|
|
||||||
const contentType =
|
const contentType =
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ describe("S3Service", () => {
|
|||||||
|
|
||||||
it("should use DOMAIN_NAME and PORT for localhost", () => {
|
it("should use DOMAIN_NAME and PORT for localhost", () => {
|
||||||
(configService.get as jest.Mock).mockImplementation(
|
(configService.get as jest.Mock).mockImplementation(
|
||||||
(key: string, def: any) => {
|
(key: string, def: unknown) => {
|
||||||
if (key === "API_URL") return null;
|
if (key === "API_URL") return null;
|
||||||
if (key === "DOMAIN_NAME") return "localhost";
|
if (key === "DOMAIN_NAME") return "localhost";
|
||||||
if (key === "PORT") return 3000;
|
if (key === "PORT") return 3000;
|
||||||
@@ -210,7 +210,7 @@ describe("S3Service", () => {
|
|||||||
|
|
||||||
it("should use api.DOMAIN_NAME for production", () => {
|
it("should use api.DOMAIN_NAME for production", () => {
|
||||||
(configService.get as jest.Mock).mockImplementation(
|
(configService.get as jest.Mock).mockImplementation(
|
||||||
(key: string, def: any) => {
|
(key: string, def: unknown) => {
|
||||||
if (key === "API_URL") return null;
|
if (key === "API_URL") return null;
|
||||||
if (key === "DOMAIN_NAME") return "memegoat.fr";
|
if (key === "DOMAIN_NAME") return "memegoat.fr";
|
||||||
return def;
|
return def;
|
||||||
|
|||||||
Reference in New Issue
Block a user