Skip to content

Commit a311619

Browse files
Move GitHub access token from query parameter into header (#1261)
GitHub deprecated passing the access token in the URL. This change moves it to the Authorization header, which is more secure and compliant with GitHub's current API standards. It also adds a test file. --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 1728ca1 commit a311619

2 files changed

Lines changed: 44 additions & 6 deletions

File tree

dev_tools/prepared_env.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ def report_status_to_github(
9797
if target_url is not None:
9898
payload['target_url'] = target_url
9999

100-
url = "https://api.github.com/repos/{}/{}/statuses/{}?access_token={}".format(
101-
self.repository.organization,
102-
self.repository.name,
103-
self.actual_commit_id,
104-
self.repository.access_token,
100+
if self.actual_commit_id is None:
101+
return
102+
url = "https://api.github.com/repos/{}/{}/statuses/{}".format(
103+
self.repository.organization, self.repository.name, self.actual_commit_id
105104
)
105+
headers = {'Authorization': 'Bearer {}'.format(self.repository.access_token)}
106106

107-
response = requests.post(url, json=payload)
107+
response = requests.post(url, json=payload, headers=headers, timeout=30)
108108

109109
if response.status_code != 201:
110110
raise IOError(
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import unittest
2+
from unittest.mock import patch, MagicMock
3+
from dev_tools.prepared_env import PreparedEnv
4+
from dev_tools.github_repository import GithubRepository
5+
6+
7+
class TestPreparedEnvSecurity(unittest.TestCase):
8+
@patch('requests.post')
9+
def test_report_status_to_github_token_in_header(self, mock_post):
10+
# Setup
11+
mock_response = MagicMock()
12+
mock_response.status_code = 201
13+
mock_post.return_value = mock_response
14+
15+
repo = GithubRepository('my-org', 'my-repo', 'my-token')
16+
env = PreparedEnv(repo, 'my-commit', 'compare-commit', None, None)
17+
18+
# Execute
19+
env.report_status_to_github('success', 'desc', 'ctx')
20+
21+
# Verify
22+
args, kwargs = mock_post.call_args
23+
url = args[0]
24+
headers = kwargs.get('headers', {})
25+
26+
# Security check: Token should NOT be in the URL
27+
self.assertNotIn('access_token=my-token', url, "Token should not be passed in the URL")
28+
29+
# Security check: Token should be in the Authorization header
30+
self.assertEqual(
31+
headers.get('Authorization'),
32+
'Bearer my-token',
33+
"Token should be passed in the Authorization header",
34+
)
35+
36+
37+
if __name__ == '__main__':
38+
unittest.main()

0 commit comments

Comments
 (0)