Skip to content

Fix CWE-918 SSRF in chat-with-index/setup_env.py - #4089

Open
ayushhgarg-work wants to merge 1 commit into
Azure:mainfrom
ayushhgarg-work:ayushhgarg/codeqlfix
Open

Fix CWE-918 SSRF in chat-with-index/setup_env.py#4089
ayushhgarg-work wants to merge 1 commit into
Azure:mainfrom
ayushhgarg-work:ayushhgarg/codeqlfix

Conversation

@ayushhgarg-work

Copy link
Copy Markdown
Member

Description

Fixes CWE-918 (Server-Side Request Forgery) in sdk/python/generative-ai/rag/code_first/flows/chat-with-index/setup_env.py, flagged by CodeQL under the Glasswing Mythos - July campaign (IcM 841731124).

The Promptflow setup_env tool previously copied connection.api_base directly into os.environ["OPENAI_API_BASE"], allowing an untrusted URL to flow to an HTTP sink. This PR adds _validate_api_base() which validates the URL before it is written to the environment:

  • Must be a non-empty str
  • No whitespace or control characters (closes urlparse leading-whitespace bypass, CRLF injection, null-byte tricks)
  • https scheme only
  • Hostname must match a static allow-list: *.openai.azure.com, *.cognitiveservices.azure.com, *.api.cognitive.microsoft.com, api.openai.com

Validated against a 52-case adversarial suite locally: all 8 legitimate URL patterns accepted; all 44 SSRF vectors (IMDS 169.254.169.254, loopback, private ranges, suffix collision e.g. x.openai.azure.com.evil.com, userinfo tricks api.openai.com@evil.com, fragment/query tricks, scheme downgrades) blocked with ValueError.

No behavior change for real Azure OpenAI / OpenAI endpoints.

Checklist

  • I have read the contribution guidelines.
  • I have coordinated with the docs team (mldocs@microsoft.com) if this PR deletes files or changes any file names or file extensions. — N/A, no file/path changes.
  • Pull request includes test coverage for the included changes. — Validator verified against a 52-case adversarial suite locally (see PR description); existing sdk-generative-ai-rag-code_first-flows-chat-with-index-src-test.yml CI workflow exercises the sample end-to-end.
  • This notebook or file is added to the CODEOWNERS file, pointing to the author or the author's team. — File already covered by existing CODEOWNERS entry for sdk/python/generative-ai/rag/.

Validate connection.api_base against an https + hostname allow-list before writing to OPENAI_API_BASE. Rejects non-https schemes, whitespace/control-char bypasses of urlparse normalization, cloud metadata endpoints, loopback/private ranges, userinfo tricks, and suffix-collision attacks. Addresses Glasswing Mythos - July Twin Flames finding, IcM 841731124.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants