-
Notifications
You must be signed in to change notification settings - Fork 8.1k
fix: validate URL scheme in build_github_request #2449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mnriem
merged 6 commits into
github:main
from
ayesha-aziz123:fix/validate-url-in-build-github-request
May 5, 2026
+94
−2
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d2a48b
fix: validate URL scheme in build_github_request
ayesha-aziz123 4266f18
Potential fix for pull request finding
ayesha-aziz123 ee9d58e
test: add missing hostname validation test for build_github_request
ayesha-aziz123 ba0dabf
test: add missing hostname validation test for build_github_request
ayesha-aziz123 64d2624
fix: update docstring and fix import grouping per Copilot feedback
ayesha-aziz123 33cb636
fix: sort imports and simplify url validation in build_github_request
ayesha-aziz123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| """Tests for GitHub-authenticated HTTP request helpers.""" | ||
|
|
||
| import os | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from specify_cli._github_http import ( | ||
| build_github_request, | ||
| ) | ||
|
|
||
|
|
||
| class TestBuildGitHubRequest: | ||
| """Tests for build_github_request() URL validation and auth handling.""" | ||
|
|
||
| # --- URL Validation Tests --- | ||
|
|
||
| def test_empty_url_raises_value_error(self): | ||
|
mnriem marked this conversation as resolved.
|
||
| """build_github_request() must reject an empty string URL.""" | ||
| with pytest.raises(ValueError, match="url must not be empty"): | ||
| build_github_request("") | ||
|
|
||
| def test_whitespace_url_raises_value_error(self): | ||
| """build_github_request() must reject a whitespace-only URL.""" | ||
| with pytest.raises(ValueError, match="url must not be empty"): | ||
| build_github_request(" ") | ||
|
|
||
| def test_non_http_url_raises_value_error(self): | ||
| """build_github_request() must reject URLs without http/https scheme.""" | ||
| with pytest.raises(ValueError, match="url must start with http"): | ||
| build_github_request("not-a-url") | ||
|
|
||
| def test_ftp_url_raises_value_error(self): | ||
| """build_github_request() must reject ftp:// URLs.""" | ||
| with pytest.raises(ValueError, match="url must start with http"): | ||
| build_github_request("ftp://github.com/file.zip") | ||
|
|
||
| # --- Valid URL Tests --- | ||
|
|
||
| def test_valid_https_url_returns_request(self): | ||
| """build_github_request() must return a Request for a valid https URL.""" | ||
| req = build_github_request("https://github.com/github/spec-kit") | ||
| assert req.full_url == "https://github.com/github/spec-kit" | ||
|
|
||
| def test_valid_http_url_returns_request(self): | ||
| """build_github_request() must accept http:// URLs.""" | ||
| req = build_github_request("http://example.com/file") | ||
| assert req.full_url == "http://example.com/file" | ||
|
|
||
| # --- Auth Header Tests --- | ||
|
|
||
| def test_github_token_added_for_github_host(self): | ||
| """Authorization header is set when GITHUB_TOKEN is present.""" | ||
| with patch.dict(os.environ, {"GITHUB_TOKEN": "test-token", "GH_TOKEN": ""}): | ||
| req = build_github_request("https://github.com/github/spec-kit") | ||
| assert req.get_header("Authorization") == "Bearer test-token" | ||
|
|
||
| def test_gh_token_used_as_fallback(self): | ||
| """GH_TOKEN is used when GITHUB_TOKEN is absent.""" | ||
| with patch.dict(os.environ, {"GITHUB_TOKEN": "", "GH_TOKEN": "fallback-token"}): | ||
| req = build_github_request("https://github.com/github/spec-kit") | ||
| assert req.get_header("Authorization") == "Bearer fallback-token" | ||
|
|
||
| def test_no_auth_header_for_non_github_host(self): | ||
| """Authorization header must NOT be set for non-GitHub URLs.""" | ||
| with patch.dict(os.environ, {"GITHUB_TOKEN": "test-token"}): | ||
| req = build_github_request("https://example.com/file") | ||
| assert req.get_header("Authorization") is None | ||
|
|
||
| def test_no_auth_header_when_no_token(self): | ||
| """No Authorization header when no token is set in environment.""" | ||
| with patch.dict(os.environ, {}, clear=True): | ||
| req = build_github_request("https://github.com/github/spec-kit") | ||
| assert req.get_header("Authorization") is None | ||
|
|
||
| def test_missing_hostname_raises_value_error(self): | ||
| """build_github_request() must reject URLs with valid scheme but no hostname.""" | ||
| with pytest.raises(ValueError, match="url must include a hostname"): | ||
| build_github_request("http://") | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.