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
+4 -4
View File
@@ -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,
}
})
+23 -8
View File
@@ -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)