mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-22 16:08:01 +00:00
fix(transcode): replace unsafe shell execution with execFile to prevent command injection
This commit is contained in:
@@ -46,19 +46,19 @@ vi.mock('@shumai/transcode', () => ({
|
||||
|
||||
vi.mock('child_process', () => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock functions dynamically return varying types based on test command calls
|
||||
const execMock: any = vi.fn((cmd: any, cb: any) => {
|
||||
const execFileMock: any = vi.fn((file: any, args: any, cb: any) => {
|
||||
cb(null, '120.0\n', '')
|
||||
})
|
||||
// Setup custom promisify behavior to return { stdout, stderr }
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock functions dynamically return varying types based on test command calls
|
||||
execMock[Symbol.for('nodejs.util.promisify.custom')] = vi.fn(async (cmd: any) => {
|
||||
if (cmd.includes('ffprobe')) {
|
||||
execFileMock[Symbol.for('nodejs.util.promisify.custom')] = vi.fn(async (file: any) => {
|
||||
if (file === 'ffprobe') {
|
||||
return { stdout: '120.0\n', stderr: '' }
|
||||
}
|
||||
return { stdout: '', stderr: '' }
|
||||
})
|
||||
return {
|
||||
exec: execMock,
|
||||
execFile: execFileMock,
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { GoogleGenAI } from '@google/genai'
|
||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||
import { transcodeService } from '@shumai/transcode'
|
||||
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'
|
||||
@@ -9,7 +9,7 @@ import { promisify } from 'util'
|
||||
import { ApplicationFailure } from '@temporalio/activity'
|
||||
import { prisma } from '@shumai/db'
|
||||
|
||||
const execAsync = promisify(exec)
|
||||
const execFileAsync = promisify(execFile)
|
||||
|
||||
export interface Usage {
|
||||
inputTokens: number
|
||||
@@ -115,9 +115,15 @@ export async function generateEmbeddingActivity(params: GenerateEmbeddingParams)
|
||||
fs.writeFileSync(tmpFile, data)
|
||||
|
||||
try {
|
||||
const { stdout } = await execAsync(
|
||||
`ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${tmpFile}"`,
|
||||
)
|
||||
const { stdout } = await execFileAsync('ffprobe', [
|
||||
'-v',
|
||||
'error',
|
||||
'-show_entries',
|
||||
'format=duration',
|
||||
'-of',
|
||||
'default=noprint_wrappers=1:nokey=1',
|
||||
tmpFile,
|
||||
])
|
||||
const duration = parseFloat(stdout.trim())
|
||||
if (isNaN(duration)) {
|
||||
throw ApplicationFailure.create({
|
||||
@@ -132,9 +138,18 @@ export async function generateEmbeddingActivity(params: GenerateEmbeddingParams)
|
||||
if (end > duration) end = duration
|
||||
|
||||
const chunkTmp = path.join(os.tmpdir(), `video-chunk-${Date.now()}.mp4`)
|
||||
await execAsync(
|
||||
`ffmpeg -y -i "${tmpFile}" -ss ${start} -t ${end - start} -c copy "${chunkTmp}"`,
|
||||
)
|
||||
await execFileAsync('ffmpeg', [
|
||||
'-y',
|
||||
'-i',
|
||||
tmpFile,
|
||||
'-ss',
|
||||
start.toString(),
|
||||
'-t',
|
||||
(end - start).toString(),
|
||||
'-c',
|
||||
'copy',
|
||||
chunkTmp,
|
||||
])
|
||||
|
||||
const chunkData = fs.readFileSync(chunkTmp)
|
||||
fs.unlinkSync(chunkTmp)
|
||||
|
||||
@@ -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),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -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 (
|
||||
|
||||
Reference in New Issue
Block a user