mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 05:54:44 +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', () => {
|
vi.mock('child_process', () => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- mock functions dynamically return varying types based on test command calls
|
// 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', '')
|
cb(null, '120.0\n', '')
|
||||||
})
|
})
|
||||||
// Setup custom promisify behavior to return { stdout, stderr }
|
// 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
|
// 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) => {
|
execFileMock[Symbol.for('nodejs.util.promisify.custom')] = vi.fn(async (file: any) => {
|
||||||
if (cmd.includes('ffprobe')) {
|
if (file === 'ffprobe') {
|
||||||
return { stdout: '120.0\n', stderr: '' }
|
return { stdout: '120.0\n', stderr: '' }
|
||||||
}
|
}
|
||||||
return { stdout: '', stderr: '' }
|
return { stdout: '', stderr: '' }
|
||||||
})
|
})
|
||||||
return {
|
return {
|
||||||
exec: execMock,
|
execFile: execFileMock,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { GoogleGenAI } from '@google/genai'
|
import { GoogleGenAI } from '@google/genai'
|
||||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||||
import { transcodeService } from '@shumai/transcode'
|
import { transcodeService } from '@shumai/transcode'
|
||||||
import { exec } from 'child_process'
|
import { execFile } from 'child_process'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
@@ -9,7 +9,7 @@ import { promisify } from 'util'
|
|||||||
import { ApplicationFailure } from '@temporalio/activity'
|
import { ApplicationFailure } from '@temporalio/activity'
|
||||||
import { prisma } from '@shumai/db'
|
import { prisma } from '@shumai/db'
|
||||||
|
|
||||||
const execAsync = promisify(exec)
|
const execFileAsync = promisify(execFile)
|
||||||
|
|
||||||
export interface Usage {
|
export interface Usage {
|
||||||
inputTokens: number
|
inputTokens: number
|
||||||
@@ -115,9 +115,15 @@ export async function generateEmbeddingActivity(params: GenerateEmbeddingParams)
|
|||||||
fs.writeFileSync(tmpFile, data)
|
fs.writeFileSync(tmpFile, data)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { stdout } = await execAsync(
|
const { stdout } = await execFileAsync('ffprobe', [
|
||||||
`ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${tmpFile}"`,
|
'-v',
|
||||||
)
|
'error',
|
||||||
|
'-show_entries',
|
||||||
|
'format=duration',
|
||||||
|
'-of',
|
||||||
|
'default=noprint_wrappers=1:nokey=1',
|
||||||
|
tmpFile,
|
||||||
|
])
|
||||||
const duration = parseFloat(stdout.trim())
|
const duration = parseFloat(stdout.trim())
|
||||||
if (isNaN(duration)) {
|
if (isNaN(duration)) {
|
||||||
throw ApplicationFailure.create({
|
throw ApplicationFailure.create({
|
||||||
@@ -132,9 +138,18 @@ export async function generateEmbeddingActivity(params: GenerateEmbeddingParams)
|
|||||||
if (end > duration) end = duration
|
if (end > duration) end = duration
|
||||||
|
|
||||||
const chunkTmp = path.join(os.tmpdir(), `video-chunk-${Date.now()}.mp4`)
|
const chunkTmp = path.join(os.tmpdir(), `video-chunk-${Date.now()}.mp4`)
|
||||||
await execAsync(
|
await execFileAsync('ffmpeg', [
|
||||||
`ffmpeg -y -i "${tmpFile}" -ss ${start} -t ${end - start} -c copy "${chunkTmp}"`,
|
'-y',
|
||||||
)
|
'-i',
|
||||||
|
tmpFile,
|
||||||
|
'-ss',
|
||||||
|
start.toString(),
|
||||||
|
'-t',
|
||||||
|
(end - start).toString(),
|
||||||
|
'-c',
|
||||||
|
'copy',
|
||||||
|
chunkTmp,
|
||||||
|
])
|
||||||
|
|
||||||
const chunkData = fs.readFileSync(chunkTmp)
|
const chunkData = fs.readFileSync(chunkTmp)
|
||||||
fs.unlinkSync(chunkTmp)
|
fs.unlinkSync(chunkTmp)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import sharp from 'sharp'
|
|||||||
|
|
||||||
// Mock child_process
|
// Mock child_process
|
||||||
vi.mock('child_process', () => ({
|
vi.mock('child_process', () => ({
|
||||||
exec: vi.fn(),
|
execFile: vi.fn(),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
// Mock sharp
|
// Mock sharp
|
||||||
@@ -66,7 +66,7 @@ describe('TranscodeService', () => {
|
|||||||
/* eslint-enable @typescript-eslint/naming-convention */
|
/* eslint-enable @typescript-eslint/naming-convention */
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// 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: '' })
|
cb(null, { stdout: mockOutput, stderr: '' })
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -83,8 +83,9 @@ describe('TranscodeService', () => {
|
|||||||
expect(info.audioSampleRate).toBe(48000)
|
expect(info.audioSampleRate).toBe(48000)
|
||||||
expect(info.audioBitDepth).toBe(16)
|
expect(info.audioBitDepth).toBe(16)
|
||||||
|
|
||||||
expect(child_process.exec).toHaveBeenCalledWith(
|
expect(child_process.execFile).toHaveBeenCalledWith(
|
||||||
expect.stringContaining('ffprobe'),
|
'ffprobe',
|
||||||
|
expect.any(Array),
|
||||||
expect.any(Function),
|
expect.any(Function),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -110,7 +111,7 @@ describe('TranscodeService', () => {
|
|||||||
|
|
||||||
it('should construct correct ffmpeg arguments for video transcoding', async () => {
|
it('should construct correct ffmpeg arguments for video transcoding', async () => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// 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: '' })
|
cb(null, { stdout: '', stderr: '' })
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -124,17 +125,20 @@ describe('TranscodeService', () => {
|
|||||||
disableAudio: true,
|
disableAudio: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(child_process.exec).toHaveBeenCalledWith(
|
expect(child_process.execFile).toHaveBeenCalledWith(
|
||||||
expect.stringContaining('scale=w=1280:h=720'),
|
'ffmpeg',
|
||||||
|
expect.arrayContaining(['-filter_complex', expect.stringContaining('scale=w=1280:h=720')]),
|
||||||
expect.any(Function),
|
expect.any(Function),
|
||||||
)
|
)
|
||||||
expect(child_process.exec).toHaveBeenCalledWith(
|
expect(child_process.execFile).toHaveBeenCalledWith(
|
||||||
expect.stringContaining('fps=24'),
|
'ffmpeg',
|
||||||
|
expect.arrayContaining(['-filter_complex', expect.stringContaining('fps=24')]),
|
||||||
expect.any(Function),
|
expect.any(Function),
|
||||||
)
|
)
|
||||||
// Should NOT have audio maps if disabled
|
// Should NOT have audio maps if disabled
|
||||||
expect(child_process.exec).not.toHaveBeenCalledWith(
|
expect(child_process.execFile).not.toHaveBeenCalledWith(
|
||||||
expect.stringContaining('-map 0:a?'),
|
'ffmpeg',
|
||||||
|
expect.arrayContaining(['-map', '0:a?']),
|
||||||
expect.any(Function),
|
expect.any(Function),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@@ -197,7 +201,7 @@ describe('TranscodeService', () => {
|
|||||||
mimeType: 'video/mp4',
|
mimeType: 'video/mp4',
|
||||||
})
|
})
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// 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: '' })
|
cb(null, { stdout: '', stderr: '' })
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -209,8 +213,9 @@ describe('TranscodeService', () => {
|
|||||||
isImage: false,
|
isImage: false,
|
||||||
})
|
})
|
||||||
|
|
||||||
expect(child_process.exec).toHaveBeenCalledWith(
|
expect(child_process.execFile).toHaveBeenCalledWith(
|
||||||
expect.stringContaining('-c:v libwebp'),
|
'ffmpeg',
|
||||||
|
expect.arrayContaining(['-c:v', 'libwebp']),
|
||||||
expect.any(Function),
|
expect.any(Function),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { prisma } from '@shumai/db'
|
import { prisma } from '@shumai/db'
|
||||||
import { WorkflowTaskStatus, WorkflowTaskType } from '@shumai/db'
|
import { WorkflowTaskStatus, WorkflowTaskType } from '@shumai/db'
|
||||||
import '@shumai/db/src/prisma-json-types'
|
import '@shumai/db/src/prisma-json-types'
|
||||||
import { exec } from 'child_process'
|
import { execFile } from 'child_process'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import * as os from 'os'
|
import * as os from 'os'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
@@ -10,7 +10,7 @@ import { promisify } from 'util'
|
|||||||
import { s3Service } from '@shumai/core/src/s3/s3'
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||||
import { ulid } from 'ulid'
|
import { ulid } from 'ulid'
|
||||||
|
|
||||||
const execAsync = promisify(exec)
|
const execFileAsync = promisify(execFile)
|
||||||
|
|
||||||
const dataFormatNames: Record<string, string> = {
|
const dataFormatNames: Record<string, string> = {
|
||||||
// additional codecs
|
// additional codecs
|
||||||
@@ -187,9 +187,15 @@ export class TranscodeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getVideoInfo(inputFile: string): Promise<MediaMetadata> {
|
async getVideoInfo(inputFile: string): Promise<MediaMetadata> {
|
||||||
const { stdout } = await execAsync(
|
const { stdout } = await execFileAsync('ffprobe', [
|
||||||
`ffprobe -v quiet -print_format json -show_format -show_streams "${inputFile}"`,
|
'-v',
|
||||||
)
|
'quiet',
|
||||||
|
'-print_format',
|
||||||
|
'json',
|
||||||
|
'-show_format',
|
||||||
|
'-show_streams',
|
||||||
|
inputFile,
|
||||||
|
])
|
||||||
const info = JSON.parse(stdout)
|
const info = JSON.parse(stdout)
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
const videoStream = info.streams.find((s: any) => s.codec_type === 'video')
|
const videoStream = info.streams.find((s: any) => s.codec_type === 'video')
|
||||||
@@ -250,14 +256,7 @@ export class TranscodeService {
|
|||||||
}
|
}
|
||||||
filterComplex += '[vout]'
|
filterComplex += '[vout]'
|
||||||
|
|
||||||
const args = [
|
const args = ['-i', params.inputFile, '-filter_complex', filterComplex, '-map', '[vout]']
|
||||||
'-i',
|
|
||||||
`"${params.inputFile}"`,
|
|
||||||
'-filter_complex',
|
|
||||||
`"${filterComplex}"`,
|
|
||||||
'-map',
|
|
||||||
'[vout]',
|
|
||||||
]
|
|
||||||
|
|
||||||
if (!params.disableAudio) {
|
if (!params.disableAudio) {
|
||||||
args.push('-map', '0:a?')
|
args.push('-map', '0:a?')
|
||||||
@@ -269,9 +268,9 @@ export class TranscodeService {
|
|||||||
args.push('-c:a', 'aac', '-b:a', '128k')
|
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(
|
async transcodeImage(
|
||||||
@@ -316,9 +315,9 @@ export class TranscodeService {
|
|||||||
|
|
||||||
const args = [
|
const args = [
|
||||||
'-i',
|
'-i',
|
||||||
`"${inputFile}"`,
|
inputFile,
|
||||||
'-filter_complex',
|
'-filter_complex',
|
||||||
`"${filterComplex}"`,
|
filterComplex,
|
||||||
'-map',
|
'-map',
|
||||||
'[sprite_out]',
|
'[sprite_out]',
|
||||||
'-frames:v',
|
'-frames:v',
|
||||||
@@ -327,7 +326,7 @@ export class TranscodeService {
|
|||||||
'libwebp',
|
'libwebp',
|
||||||
'-q:v',
|
'-q:v',
|
||||||
'75',
|
'75',
|
||||||
`"${outputSprite}"`,
|
outputSprite,
|
||||||
'-map',
|
'-map',
|
||||||
'[thumb_out]',
|
'[thumb_out]',
|
||||||
'-c:v',
|
'-c:v',
|
||||||
@@ -338,23 +337,14 @@ export class TranscodeService {
|
|||||||
'1',
|
'1',
|
||||||
'-max_muxing_queue_size',
|
'-max_muxing_queue_size',
|
||||||
'1024',
|
'1024',
|
||||||
`"${outputPoster}"`,
|
outputPoster,
|
||||||
]
|
]
|
||||||
await execAsync(`ffmpeg -y ${args.join(' ')}`)
|
await execFileAsync('ffmpeg', ['-y', ...args])
|
||||||
}
|
}
|
||||||
|
|
||||||
async extractAudio(inputFile: string, outputFile: string, bitrate: string): Promise<void> {
|
async extractAudio(inputFile: string, outputFile: string, bitrate: string): Promise<void> {
|
||||||
const args = [
|
const args = ['-i', inputFile, '-vn', '-acodec', 'libmp3lame', '-b:a', bitrate, outputFile]
|
||||||
'-i',
|
await execFileAsync('ffmpeg', ['-y', ...args])
|
||||||
`"${inputFile}"`,
|
|
||||||
'-vn',
|
|
||||||
'-acodec',
|
|
||||||
'libmp3lame',
|
|
||||||
'-b:a',
|
|
||||||
bitrate,
|
|
||||||
`"${outputFile}"`,
|
|
||||||
]
|
|
||||||
await execAsync(`ffmpeg -y ${args.join(' ')}`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async extractVideoFrames(params: ExtractVideoFramesParams): Promise<string[]> {
|
async extractVideoFrames(params: ExtractVideoFramesParams): Promise<string[]> {
|
||||||
@@ -370,16 +360,16 @@ export class TranscodeService {
|
|||||||
|
|
||||||
const args = [
|
const args = [
|
||||||
'-i',
|
'-i',
|
||||||
`"${params.inputFile}"`,
|
params.inputFile,
|
||||||
'-vf',
|
'-vf',
|
||||||
`fps=${fps},scale=-2:${params.frameHeight}`,
|
`fps=${fps},scale=-2:${params.frameHeight}`,
|
||||||
'-c:v',
|
'-c:v',
|
||||||
'libwebp',
|
'libwebp',
|
||||||
'-q:v',
|
'-q:v',
|
||||||
'80',
|
'80',
|
||||||
`"${outputPattern}"`,
|
outputPattern,
|
||||||
]
|
]
|
||||||
await execAsync(`ffmpeg -y ${args.join(' ')}`)
|
await execFileAsync('ffmpeg', ['-y', ...args])
|
||||||
|
|
||||||
const files = fs.readdirSync(params.outputDir)
|
const files = fs.readdirSync(params.outputDir)
|
||||||
return files
|
return files
|
||||||
@@ -486,7 +476,7 @@ export class TranscodeService {
|
|||||||
'-ss',
|
'-ss',
|
||||||
t.toFixed(3),
|
t.toFixed(3),
|
||||||
'-i',
|
'-i',
|
||||||
`"${videoPath}"`,
|
videoPath,
|
||||||
'-vframes',
|
'-vframes',
|
||||||
'1',
|
'1',
|
||||||
'-vf',
|
'-vf',
|
||||||
@@ -495,9 +485,9 @@ export class TranscodeService {
|
|||||||
'libwebp',
|
'libwebp',
|
||||||
'-q:v',
|
'-q:v',
|
||||||
'80',
|
'80',
|
||||||
`"${localShotPath}"`,
|
localShotPath,
|
||||||
]
|
]
|
||||||
await execAsync(`ffmpeg -y ${args.join(' ')}`)
|
await execFileAsync('ffmpeg', ['-y', ...args])
|
||||||
|
|
||||||
// 5. Overlay annotations if timestamp matches commentTimestamp exactly
|
// 5. Overlay annotations if timestamp matches commentTimestamp exactly
|
||||||
if (
|
if (
|
||||||
|
|||||||
Reference in New Issue
Block a user