fix(youtube): address Greptile review feedback

Three changes from automated review on PR #376:

1. Add `--` option terminator before host in _wrap_ytdlp_cmd (P1 security)
   Prevents SSH option injection if LAST30DAYS_YT_SSH_HOST were ever set
   to a value starting with `-` (e.g. `-oProxyCommand=...`). Low
   exploitability since the env var is user-controlled config — but the
   fix is a single arg and turns a self-harm footgun into no footgun.

2. Hoist `import shlex` to module-level (P2 style)
   Pure stdlib import, no reason for the deferred form. Cleaner.

3. Cache _ytdlp_ssh_host() result in fetch_transcript (P2 style)
   Was being called 2-3x per video; the function is cheap (env lookup
   + strip) so this is purely about readability.

Adds test_wrap_cmd_uses_option_terminator covering the security fix
explicitly with a `-oFoo=bar` host value. Updates index assertions in
the two existing tests that check command shape (host is now at index
4, command string at 5, with `--` at 3).
This commit is contained in:
shoobee
2026-05-10 20:48:01 +00:00
committed by Trevin Chow
parent 79b5d049ce
commit f4eb0af104
2 changed files with 34 additions and 13 deletions
+7 -4
View File
@@ -10,6 +10,7 @@ import json
import math
import os
import re
import shlex
import shutil
import sys
import tempfile
@@ -134,13 +135,14 @@ def _wrap_ytdlp_cmd(cmd: List[str]) -> List[str]:
Args are shell-quoted to survive the remote shell. Uses BatchMode=yes so
a misconfigured key fails fast instead of hanging on a password prompt.
The `--` option terminator prevents an SSH option-injection if
LAST30DAYS_YT_SSH_HOST were ever set to a value starting with `-`.
"""
host = _ytdlp_ssh_host()
if not host:
return cmd
import shlex
remote_cmd = " ".join(shlex.quote(a) for a in cmd)
return ["ssh", "-o", "BatchMode=yes", host, remote_cmd]
return ["ssh", "-o", "BatchMode=yes", "--", host, remote_cmd]
def _extract_core_subject(topic: str) -> str:
@@ -520,14 +522,15 @@ def fetch_transcript(video_id: str, temp_dir: str) -> Optional[str]:
# file on the remote host that we can't easily read back. Skip it and
# use the HTTP transcript fallback (different YouTube endpoint, less
# bot-walled, works fine from datacenter IPs).
use_ytdlp = is_ytdlp_installed() and not _ytdlp_ssh_host()
ssh_host = _ytdlp_ssh_host()
use_ytdlp = is_ytdlp_installed() and not ssh_host
if use_ytdlp:
raw_vtt = _fetch_transcript_ytdlp(video_id, temp_dir)
if not raw_vtt:
_log(f"yt-dlp transcript failed for {video_id}, trying direct HTTP fallback")
raw_vtt = _fetch_transcript_direct(video_id)
else:
if _ytdlp_ssh_host():
if ssh_host:
_log("SSH-routing active, using direct HTTP transcript fetch")
else:
_log("yt-dlp not installed, using direct HTTP transcript fetch")