Repository navigation
fix(security): bound OpenAI server and media fetches (#3970) - #3980
Merged
Merged
Conversation
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
…mplicit (fall through) returns' Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
This was referenced Nov 14, 2026
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.
Fixes #3970
TL;DR
GPTQModel.serve()now defaults to a loopback listener, refuses an unauthenticated non-loopback bind, validates the Host header, and bounds request bodies/token counts/choices. Audio/video calibration URLs now go through the existing bounded image-fetch worker (timeout, total budget, byte cap, redirect cap, per-hop private/link-local filtering) and PyAV getsprotocol_whitelist=file,pipe; local images get themax_pixelscheck. Net diff: +904/-252 across 9 files.1. The problem
At audit revision
93d3ae7a:DEFAULT_HOST = "0.0.0.0"(gptqmodel/utils/openai_server.py:35) andserve(host="0.0.0.0", port=80, api_key=None)(gptqmodel/models/base.py:3168) exposed inference with no key;_warn_if_exposed()only warned (openai_server.py:424-446).OpenAiRequesthadmax_tokens: int = 256,n: int = 1and no body/message caps (openai_server.py:219-226); no Host-header validation, so DNS rebinding could reach a loopback listener and/shutdown.process_audio_info()passedhttp(s)URLs straight toaudioread.ffdec.FFmpegAudioFile(gptqmodel/utils/audio.py:86-110) and calledav.open(path)for any video string before the scheme check (audio.py:35), with no timeout/byte/redirect/protocol bounds.requests.getfollowed redirects with no destination filtering (gptqmodel/utils/image.py:147), and local/file://images skipped themax_pixelscheck (image.py:468-476).Old listener and media flow:
flowchart TD A["serve(host=0.0.0.0, api_key=None)"] --> B["_warn_if_exposed"] B --> C["uvicorn starts on 0.0.0.0:80"] C --> D["any peer can call /v1 or /shutdown"] E["audio/video URL from dataset"] --> F["audioread FFmpegAudioFile or av.open(url)"] F --> G["ffmpeg/PyAV fetch with default protocols"] G --> H["no timeout, byte cap or redirect limit"] classDef failure fill:#ffe8e8,stroke:#b30000,color:#111; class D,H failure;2. The new flow
flowchart TD A["GPTQModel.serve(host, port, api_key, allowed_hosts)"] --> B["OpenAiServer"] B --> C["_validate_bind(host, api_keys)"] C -- "non-loopback without key" --> X["ValueError before uvicorn starts"] C -- "loopback or key present" --> D["_install_host_validation(host)"] D --> E["TrustedHostMiddleware allowed_hosts"] E --> F["uvicorn.Config + Server"] B --> G["BodySizeLimitMiddleware"] G -- "body over MAX_REQUEST_BODY_BYTES" --> Y["HTTP 413"] G -- "within limit" --> H["OpenAiRequest validation"] H -- "max_tokens or n out of range" --> Z["HTTP 422"] H -- "valid" --> I["generate under _generation_lock"] classDef failure fill:#ffe8e8,stroke:#b30000,color:#111; class X,Y,Z failure;flowchart TD A["process_audio_info(audio/video URL)"] --> B["load_bytes_from_url"] B --> C["_download_image_bytes: subprocess worker"] C --> D["_validate_remote_url for every hop"] D -- "link-local or private without opt-in" --> X["ImageFetchError; no connection"] D -- "allowed" --> E["requests.get with allow_redirects=False"] E -- "redirect" --> F{"redirects < max_redirects?"} F -- "no" --> Y["ImageFetchError: redirect limit"] F -- "yes" --> D E -- "200" --> G["bounded body read"] G --> H["_materialized_media writes a temporary local file"] H --> I["av.open with protocol_whitelist=file,pipe"] H --> J["librosa.load on the local path"] classDef failure fill:#ffe8e8,stroke:#b30000,color:#111; class X,Y failure;_address_is_blocked()always rejects link-local (cloud metadata) addresses; loopback/private destinations require the explicitallow_private_addresses=Trueargument. Redirects are followed manually so each hop is validated, bounded byDEFAULT_IMAGE_FETCH_MAX_REDIRECTS.fetch_image()appliesmax_pixelsto local paths,file://URLs and base64 data.3. Before / after
DEFAULT_HOST = "0.0.0.0"(openai_server.py:35)"127.0.0.1";serve()default matches (base.py:3168)openai_server.py:424-446)_validate_bind()raisesValueErrorbefore uvicornTrustedHostMiddleware; loopback binds accept only loopback namesmax_tokens=256,n=1, no body cap (openai_server.py:219-226)BodySizeLimitMiddleware(1 MiB)FFmpegAudioFile(path)(audio.py:86-110)load_bytes_from_urlthen a temporary local fileav.open(video_path)before scheme check (audio.py:35)protocol_whitelist=file,pipeimage.py:147)allow_redirects=False, per-hop validation, capimage.py:468-476)_open_local_imageenforcesmax_pixels4. Before/after repro
Listener (
/tmp/repro_3970_server.py, stub model + FastAPI TestClient):Before (
93d3ae7a):After:
Media (
/tmp/repro_3970_audio.py, fakes record what ffmpeg/librosa/PyAV receive):Before:
After:
Image redirect/SSRF (
/tmp/repro_3970_image.py):Before:
has _validate_remote_url: False,worker uses allow_redirects=False: False, loopback redirect returns(4, 4).After:
has _validate_remote_url: True,worker uses allow_redirects=False: True, default loopback redirect is rejected (ImageFetchError: destination address 127.0.0.1 ...), andallow_private_addresses=Truestill returns(4, 4).5. Tests
New/updated tests:
tests/test_openai_server_contract.py: loopback default, refused unauthenticated public bind, TrustedHost rejection ofHost: evil.example,max_tokens/n422 caps, 413 body cap.tests/test_image_fetch.py: private/link-local rejection, link-local redirect rejection, redirect cap, local/file://pixel limit; existing bounded-fetch tests updated forallow_redirects=Falseand the explicit private opt-in.tests/test_media_fetch.py(new): remote audio uses the bounded worker and a temp path, remote video passesprotocol_whitelist=file,pipeand never opens the URL, local media size cap.Exact commands and results (FastAPI/httpx/uvicorn were installed into a throwaway
--target /tmp/oai_deps; the venvs were not modified):Hardware: NVIDIA RTX 4090 (CUDA UUID
GPU-1ece7ec2-d88d-9284-93c8-d2578d90ad7e, torch 2.16.0.dev+cu130) and AMD Radeon R9700 (HIP_VISIBLE_DEVICES=1, torch 2.16.0.dev+rocm10.1).6. Docs
Reference page:
docs/openai-server-exposure.md, linked from the README OpenAI section. It carries the same diagrams, the before/after table, and the failure contract.7. Limitations
av,librosa, andaudioreadare not installed in either test venv. The media tests use fakes to pin the control flow (bounded worker first, temp local path,protocol_whitelist); a real ffmpeg/PyAV decode was not executed here.allow_private_addressesis an explicit API argument (no environment switch). It permits loopback/private hosts but never link-local metadata addresses.api_keykeeps Host validation permissive (["*"]) unlessallowed_hosts=is supplied, so an existing authenticated public deployment keeps working.