Repository navigation
fix(subtitle-mcp): validate mediaUrl scheme before FFmpeg to prevent protocol/argument injection - #580
Closed
andesyteoss wants to merge 1 commit into
Closed
fix(subtitle-mcp): validate mediaUrl scheme before FFmpeg to prevent protocol/argument injection#580andesyteoss wants to merge 1 commit into
andesyteoss wants to merge 1 commit into
Conversation
…protocol abuse (CWE-78)
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #579
Vulnerability
The
generateSubtitleMCP tool inproject/aitoearn-backend/apps/aitoearn-ai/src/core/agent/mcp/subtitle.mcp.tsforwards a caller-controlledmediaUrlstraight intoffmpeg -iwithout checking the URL scheme. FFmpeg speaks many protocols beyondhttp(s)—file:,concat:,subfile:,data:,pipe:,rtmp:,gopher:,srtp:, … — so an authenticated MCP caller (or a prompt-injectable LLM/agent connected to the MCP server) can turn the tool into an arbitrary local file reader and an SSRF gateway, and can also inject FFmpeg CLI flags via a leading-.execa('ffmpeg', ['-i', mediaUrl, ...])inextractAudioWithFFmpegmediaUrlfrom thegenerateSubtitleMCP tool.generateSubtitleSchemadeclaresmediaUrl: z.string(), which does not constrain the scheme.The value flows unchanged from the MCP tool handler →
extractAudioWithFFmpeg(mediaUrl)→execa('ffmpeg', ['-i', mediaUrl, ...]).execaavoids a shell, so this is not classic shell injection — but FFmpeg itself dispatches on the URL scheme and treats a leading-as a flag.Fix
Two commits' worth of change squashed into one small diff (
+39 lines, single file):assertSafeMediaUrl(mediaUrl):new URL(...)and requiresprotocol === 'http:' || 'https:'.-(defence against argument injection).extractAudioWithFFmpeg(so the check cannot be bypassed by any future caller of that private method).-protocol_whitelist https,http,tls,tcpto the FFmpeg argv as defence-in-depth against protocol chaining inside playlists/HLS segments that FFmpeg would otherwise follow intofile:orconcat:.Rationale: the tool is documented as taking a media URL, so restricting to http(s) matches its intended contract.
-protocol_whitelistis FFmpeg's own supported mechanism for exactly this class of issue, and layering both gives us protection even if a future refactor loses the JS-level check.Proof of concept
Against the pre-fix code, any caller that can reach the MCP tool can invoke:
{ "name": "generateSubtitle", "arguments": { "mediaUrl": "file:///etc/passwd", "language": "en" } }FFmpeg opens
/etc/passwdas input. Other payloads that reproduce the same class of bug pre-fix:concat:/etc/hosts|/etc/passwd— concatenate local files as the audio inputsubfile,,start,0,end,4096,,:/etc/shadow— byte-range read of a local filehttp://169.254.169.254/latest/meta-data/— SSRF to cloud metadatagopher://internal-service:6379/_...— SSRF against internal Redis/etc.-yor-loglevel debugas the value ofmediaUrl— argument injection into the FFmpeg command lineAfter the fix, every one of these is rejected before FFmpeg is spawned with
Invalid mediaUrl protocol "…"/Invalid mediaUrl: must not start with "-", and even if a future code path skippedassertSafeMediaUrl,-protocol_whitelist https,http,tls,tcpwould still blockfile:/concat:/subfile:/gopher:/data:/pipe:.Testing
extractAudioWithFFmpegand the tool handler at thegenerateSubtitleSchema.shaperegistration) now pass throughassertSafeMediaUrl.new URL('file:///etc/passwd').protocol === 'file:',new URL('concat:a|b').protocol === 'concat:',new URL('http://x/').protocol === 'http:'— the check correctly accepts onlyhttp:/https:.new URL('-y')throws (so it's rejected at the parse step too, but the explicit dash guard is retained for clarity and to defend against any downstream code that ever accepts non-URL strings).git diff origin/main..HEAD --stat→1 file changed, 39 insertions(+).Adversarial review
Before submitting, we tried to disprove this finding. Considered:
execa(no-shell) already prevent this? No.execaprevents shell metacharacter injection, but the vulnerability is inside FFmpeg's own URL-protocol dispatcher, which runs regardless of how the argv was constructed.z.string()accepts any string, includingfile:///…and-y.-protocol_whitelistalone sufficient? It closes the FFmpeg-side attack surface but not the leading--argument-injection case, and it also allows attackers to still pass e.g.http://internal/SSRF. The URL-parse check is what enforces http(s) semantics;-protocol_whitelistis the belt to that suspenders.We couldn't find a mitigation already in the codebase that would prevent exploitation, so the fix is warranted.