fix(transcode): replace unsafe shell execution with execFile to prevent command injection

This commit is contained in:
yiling
2026-06-05 23:32:27 +08:00
parent 25d12d1574
commit 49f010ca85
4 changed files with 73 additions and 63 deletions
+19 -14
View File
@@ -7,7 +7,7 @@ import sharp from 'sharp'
// Mock child_process
vi.mock('child_process', () => ({
exec: vi.fn(),
execFile: vi.fn(),
}))
// Mock sharp
@@ -66,7 +66,7 @@ describe('TranscodeService', () => {
/* eslint-enable @typescript-eslint/naming-convention */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(child_process.exec as any).mockImplementation((cmd: string, cb: any) => {
;(child_process.execFile as any).mockImplementation((file: string, args: string[], cb: any) => {
cb(null, { stdout: mockOutput, stderr: '' })
})
@@ -83,8 +83,9 @@ describe('TranscodeService', () => {
expect(info.audioSampleRate).toBe(48000)
expect(info.audioBitDepth).toBe(16)
expect(child_process.exec).toHaveBeenCalledWith(
expect.stringContaining('ffprobe'),
expect(child_process.execFile).toHaveBeenCalledWith(
'ffprobe',
expect.any(Array),
expect.any(Function),
)
})
@@ -110,7 +111,7 @@ describe('TranscodeService', () => {
it('should construct correct ffmpeg arguments for video transcoding', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(child_process.exec as any).mockImplementation((cmd: string, cb: any) => {
;(child_process.execFile as any).mockImplementation((file: string, args: string[], cb: any) => {
cb(null, { stdout: '', stderr: '' })
})
@@ -124,17 +125,20 @@ describe('TranscodeService', () => {
disableAudio: true,
})
expect(child_process.exec).toHaveBeenCalledWith(
expect.stringContaining('scale=w=1280:h=720'),
expect(child_process.execFile).toHaveBeenCalledWith(
'ffmpeg',
expect.arrayContaining(['-filter_complex', expect.stringContaining('scale=w=1280:h=720')]),
expect.any(Function),
)
expect(child_process.exec).toHaveBeenCalledWith(
expect.stringContaining('fps=24'),
expect(child_process.execFile).toHaveBeenCalledWith(
'ffmpeg',
expect.arrayContaining(['-filter_complex', expect.stringContaining('fps=24')]),
expect.any(Function),
)
// Should NOT have audio maps if disabled
expect(child_process.exec).not.toHaveBeenCalledWith(
expect.stringContaining('-map 0:a?'),
expect(child_process.execFile).not.toHaveBeenCalledWith(
'ffmpeg',
expect.arrayContaining(['-map', '0:a?']),
expect.any(Function),
)
})
@@ -197,7 +201,7 @@ describe('TranscodeService', () => {
mimeType: 'video/mp4',
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(child_process.exec as any).mockImplementation((cmd: string, cb: any) => {
;(child_process.execFile as any).mockImplementation((file: string, args: string[], cb: any) => {
cb(null, { stdout: '', stderr: '' })
})
@@ -209,8 +213,9 @@ describe('TranscodeService', () => {
isImage: false,
})
expect(child_process.exec).toHaveBeenCalledWith(
expect.stringContaining('-c:v libwebp'),
expect(child_process.execFile).toHaveBeenCalledWith(
'ffmpeg',
expect.arrayContaining(['-c:v', 'libwebp']),
expect.any(Function),
)
})
+27 -37
View File
@@ -1,7 +1,7 @@
import { prisma } from '@shumai/db'
import { WorkflowTaskStatus, WorkflowTaskType } from '@shumai/db'
import '@shumai/db/src/prisma-json-types'
import { exec } from 'child_process'
import { execFile } from 'child_process'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
@@ -10,7 +10,7 @@ import { promisify } from 'util'
import { s3Service } from '@shumai/core/src/s3/s3'
import { ulid } from 'ulid'
const execAsync = promisify(exec)
const execFileAsync = promisify(execFile)
const dataFormatNames: Record<string, string> = {
// additional codecs
@@ -187,9 +187,15 @@ export class TranscodeService {
}
async getVideoInfo(inputFile: string): Promise<MediaMetadata> {
const { stdout } = await execAsync(
`ffprobe -v quiet -print_format json -show_format -show_streams "${inputFile}"`,
)
const { stdout } = await execFileAsync('ffprobe', [
'-v',
'quiet',
'-print_format',
'json',
'-show_format',
'-show_streams',
inputFile,
])
const info = JSON.parse(stdout)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const videoStream = info.streams.find((s: any) => s.codec_type === 'video')
@@ -250,14 +256,7 @@ export class TranscodeService {
}
filterComplex += '[vout]'
const args = [
'-i',
`"${params.inputFile}"`,
'-filter_complex',
`"${filterComplex}"`,
'-map',
'[vout]',
]
const args = ['-i', params.inputFile, '-filter_complex', filterComplex, '-map', '[vout]']
if (!params.disableAudio) {
args.push('-map', '0:a?')
@@ -269,9 +268,9 @@ export class TranscodeService {
args.push('-c:a', 'aac', '-b:a', '128k')
}
args.push('-movflags', '+faststart', '-max_muxing_queue_size', '1024', `"${params.outputFile}"`)
args.push('-movflags', '+faststart', '-max_muxing_queue_size', '1024', params.outputFile)
await execAsync(`ffmpeg -y ${args.join(' ')}`)
await execFileAsync('ffmpeg', ['-y', ...args])
}
async transcodeImage(
@@ -316,9 +315,9 @@ export class TranscodeService {
const args = [
'-i',
`"${inputFile}"`,
inputFile,
'-filter_complex',
`"${filterComplex}"`,
filterComplex,
'-map',
'[sprite_out]',
'-frames:v',
@@ -327,7 +326,7 @@ export class TranscodeService {
'libwebp',
'-q:v',
'75',
`"${outputSprite}"`,
outputSprite,
'-map',
'[thumb_out]',
'-c:v',
@@ -338,23 +337,14 @@ export class TranscodeService {
'1',
'-max_muxing_queue_size',
'1024',
`"${outputPoster}"`,
outputPoster,
]
await execAsync(`ffmpeg -y ${args.join(' ')}`)
await execFileAsync('ffmpeg', ['-y', ...args])
}
async extractAudio(inputFile: string, outputFile: string, bitrate: string): Promise<void> {
const args = [
'-i',
`"${inputFile}"`,
'-vn',
'-acodec',
'libmp3lame',
'-b:a',
bitrate,
`"${outputFile}"`,
]
await execAsync(`ffmpeg -y ${args.join(' ')}`)
const args = ['-i', inputFile, '-vn', '-acodec', 'libmp3lame', '-b:a', bitrate, outputFile]
await execFileAsync('ffmpeg', ['-y', ...args])
}
async extractVideoFrames(params: ExtractVideoFramesParams): Promise<string[]> {
@@ -370,16 +360,16 @@ export class TranscodeService {
const args = [
'-i',
`"${params.inputFile}"`,
params.inputFile,
'-vf',
`fps=${fps},scale=-2:${params.frameHeight}`,
'-c:v',
'libwebp',
'-q:v',
'80',
`"${outputPattern}"`,
outputPattern,
]
await execAsync(`ffmpeg -y ${args.join(' ')}`)
await execFileAsync('ffmpeg', ['-y', ...args])
const files = fs.readdirSync(params.outputDir)
return files
@@ -486,7 +476,7 @@ export class TranscodeService {
'-ss',
t.toFixed(3),
'-i',
`"${videoPath}"`,
videoPath,
'-vframes',
'1',
'-vf',
@@ -495,9 +485,9 @@ export class TranscodeService {
'libwebp',
'-q:v',
'80',
`"${localShotPath}"`,
localShotPath,
]
await execAsync(`ffmpeg -y ${args.join(' ')}`)
await execFileAsync('ffmpeg', ['-y', ...args])
// 5. Overlay annotations if timestamp matches commentTimestamp exactly
if (