From f7860c6197eeba9ea4a87a34be88c97648b66950 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Sat, 26 Apr 2025 11:09:10 +0800 Subject: [PATCH] fix: update CLIFileExistenceChecker to accept containerId in constructor --- .../src/api/controllers/commandController.ts | 2 +- server/src/containers/cli.ts | 35 ++++++++----------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/server/src/api/controllers/commandController.ts b/server/src/api/controllers/commandController.ts index f225d9a..a09171d 100644 --- a/server/src/api/controllers/commandController.ts +++ b/server/src/api/controllers/commandController.ts @@ -39,7 +39,7 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => { const gameSession = new GameSession( session, quest, - new CLIFileExistenceChecker() + new CLIFileExistenceChecker(session.containerId!) ) const event = await gameSession.runCommand(command) diff --git a/server/src/containers/cli.ts b/server/src/containers/cli.ts index feb5a6a..8024fb7 100644 --- a/server/src/containers/cli.ts +++ b/server/src/containers/cli.ts @@ -7,26 +7,21 @@ function sanitizeId(id: string) { return id.replace(/[^a-zA-Z0-9]/g, '') } -// eslint-disable-next-line import/prefer-default-export -export function checkFile(id: string, file: IFileInput) { - return new Promise((resolve) => { - const cmd = buildFileCheckCmd(file) - // WARNING: This is vulnerable to command injection - execFile( - 'docker', - ['exec', sanitizeId(id), ...cmd], - { timeout: 1000 }, - (error) => { - const exists = !error - resolve(exists) - } - ) - }) -} - export class CLIFileExistenceChecker implements IFileExistenceChecker { - // eslint-disable-next-line class-methods-use-this - async exists(file: IFileInput) { - return checkFile(file.path, file) + constructor(private readonly containerId: string) {} + exists(file: IFileInput) { + return new Promise((resolve) => { + const cmd = buildFileCheckCmd(file) + // WARNING: This is vulnerable to command injection + execFile( + 'docker', + ['exec', sanitizeId(this.containerId), ...cmd], + { timeout: 1000 }, + (error) => { + const exists = !error + resolve(exists) + } + ) + }) } }