Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: turn join to pathJoinSafe #164

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

generator client {
provider = "prisma-client-js"
binaryTargets = ["debian-openssl-3.0.x", "debian-openssl-1.1.x"]
binaryTargets = ["debian-openssl-3.0.x", "debian-openssl-1.1.x", "windows"]
}

datasource db {
Expand Down Expand Up @@ -231,15 +231,21 @@ model SessionRefreshLog {
// avatars.legacy.prisma
//

enum AvatarType {
default
predefined
upload
}

model Avatar {
id Int @id @default(autoincrement())
url String @db.VarChar
name String @db.VarChar
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
avatarType String @map("avatar_type") @db.VarChar
avatarType AvatarType @map("avatar_type")
usageCount Int @default(0) @map("usage_count")
groupProfile GroupProfile[]
userProfile UserProfile[]
GroupProfile GroupProfile[]
UserProfile UserProfile[]

@@map("avatar")
}
Expand Down
2 changes: 1 addition & 1 deletion src/app.prisma
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
generator client {
provider = "prisma-client-js"
binaryTargets = ["debian-openssl-3.0.x", "debian-openssl-1.1.x"]
binaryTargets = ["debian-openssl-3.0.x", "debian-openssl-1.1.x", "windows"]
}

datasource db {
Expand Down
6 changes: 6 additions & 0 deletions src/avatars/avatars.error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ export class InvalidAvatarTypeError extends BaseError {
super('InvalidAvatarTypeError', `Invalid Avatar type: ${avatarType}`, 400);
}
}

export class InvalidPathError extends BaseError {
constructor() {
super('InvalidPathError', `Invalid path`, 400);
}
}
54 changes: 51 additions & 3 deletions src/avatars/avatars.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
import { Module } from '@nestjs/common';
import { MulterModule } from '@nestjs/platform-express';
import { TypeOrmModule } from '@nestjs/typeorm';
import { existsSync, mkdirSync } from 'fs';
import { diskStorage } from 'multer';
import { extname, join } from 'path';
import * as path from 'path';
import { extname } from 'path';
import { v4 as uuidv4 } from 'uuid';
import { AuthModule } from '../auth/auth.module';
import { AvatarsController } from './avatars.controller';
import { InvalidPathError } from './avatars.error';
import { Avatar } from './avatars.legacy.entity';
import { AvatarsService } from './avatars.service';
import { existsSync, mkdirSync } from 'fs';
declare module 'path' {
interface PlatformPath {
joinSafe(...paths: string[]): string | undefined;
}
}
function pathJoinSafePosix(
dir: string,
...paths: string[]
): string | undefined {
dir = path.posix.normalize(dir);
const pathname = path.posix.join(dir, ...paths);
if (pathname.substring(0, dir.length) !== dir) return undefined;
return pathname;
}

function pathJoinSafeWin32(
dir: string,
...paths: string[]
): string | undefined {
dir = path.win32.normalize(dir);
const pathname = path.win32.join(dir, ...paths);
if (pathname.substring(0, dir.length) !== dir) return undefined;
return pathname;
}

path.posix.joinSafe = pathJoinSafePosix;
path.win32.joinSafe = pathJoinSafeWin32;

export function pathJoinSafe(
dir: string,
...paths: string[]
): string | undefined {
dir = path.normalize(dir);
let joinedPath: string;
if (path.sep === '/') {
joinedPath = path.posix.join(dir, ...paths);
} else {
joinedPath = path.win32.join(dir, ...paths);
}

if (joinedPath.substring(0, dir.length) !== dir) return undefined;
return joinedPath;
}
@Module({
imports: [
TypeOrmModule.forFeature([Avatar]),
Expand All @@ -22,7 +67,10 @@ import { existsSync, mkdirSync } from 'fs';
'error',
);
}
const dest = join(process.env.FILE_UPLOAD_PATH, 'avatars');
const dest = pathJoinSafe(process.env.FILE_UPLOAD_PATH, 'avatars');
if (!dest) {
throw new InvalidPathError();
}
if (!existsSync(dest)) {
mkdirSync(dest, { recursive: true });
}
Expand Down
18 changes: 11 additions & 7 deletions src/avatars/avatars.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { readdirSync } from 'fs';
import { join } from 'path';
import { Repository } from 'typeorm';
import { AvatarNotFoundError } from './avatars.error';
import { AvatarNotFoundError, InvalidPathError } from './avatars.error';
import { Avatar, AvatarType } from './avatars.legacy.entity';
import { pathJoinSafe } from './avatars.module';
@Injectable()
export class AvatarsService implements OnModuleInit {
constructor(
Expand All @@ -15,8 +15,10 @@ export class AvatarsService implements OnModuleInit {
this.initialize();
}
private async initialize(): Promise<void> {
const sourcePath = join(__dirname, '../resources/avatars');

const sourcePath = pathJoinSafe(__dirname, '../avatars');
if (!sourcePath) {
throw new InvalidPathError();
}
const avatarFiles = readdirSync(sourcePath);
/* istanbul ignore if */
if (!process.env.DEFAULT_AVATAR_NAME) {
Expand All @@ -26,8 +28,10 @@ export class AvatarsService implements OnModuleInit {
}
const defaultAvatarName = process.env.DEFAULT_AVATAR_NAME;

const defaultAvatarPath = join(sourcePath, defaultAvatarName);

const defaultAvatarPath = pathJoinSafe(sourcePath, defaultAvatarName);
if (!defaultAvatarPath) {
throw new InvalidPathError();
}
let defaultAvatar = await this.avatarRepository.findOneBy({
avatarType: AvatarType.default,
});
Expand Down Expand Up @@ -55,7 +59,7 @@ export class AvatarsService implements OnModuleInit {
}
await Promise.all(
predefinedAvatars.map(async (name) => {
const avatarPath = join(sourcePath, name);
const avatarPath = pathJoinSafe(sourcePath, name);
const predefinedAvatar = this.avatarRepository.create({
url: avatarPath,
name,
Expand Down
Loading