Skip to content

Commit cda0316

Browse files
committed
feat: centralize User-Agent string across all API clients
- Add USER_AGENT constant to socketsecurity/__init__.py - Replace hardcoded 'SocketPythonScript/0.0.1' and 'SocketPythonCLI/0.0.1' with centralized USER_AGENT - Update all SCM clients (GitHub, GitLab) and CLI client to use USER_AGENT - Update unit tests to reference centralized constant - Pin GitHub Actions to commit SHAs for improved security and reproducibility - Fix minor GitLab client bugs (return type, pipeline source support)
1 parent c62c119 commit cda0316

File tree

10 files changed

+37
-30
lines changed

10 files changed

+37
-30
lines changed

.github/workflows/pr-preview.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ jobs:
1111
contents: read
1212
pull-requests: write
1313
steps:
14-
- uses: actions/checkout@v4
14+
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871
1515
with:
1616
fetch-depth: 0
17-
- uses: actions/setup-python@v5
17+
- uses: actions/setup-python@f677139bbe7f9c59b41e40162b753c062f5d49a3
1818
with:
1919
python-version: '3.x'
2020

@@ -43,14 +43,14 @@ jobs:
4343
4444
- name: Publish to Test PyPI
4545
if: steps.version_check.outputs.exists != 'true'
46-
uses: pypa/gh-action-pypi-publish@v1.12.4
46+
uses: pypa/gh-action-pypi-publish@ab69e431e9c9f48a3310be0a56527c679f56e04d
4747
with:
4848
repository-url: https://test.pypi.org/legacy/
4949
verbose: true
5050

5151
- name: Comment on PR
5252
if: steps.version_check.outputs.exists != 'true'
53-
uses: actions/github-script@v7
53+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea
5454
env:
5555
VERSION: ${{ env.VERSION }}
5656
with:
@@ -120,21 +120,21 @@ jobs:
120120
exit 1
121121
122122
- name: Set up QEMU
123-
uses: docker/setup-qemu-action@v3
123+
uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf
124124

125125
- name: Set up Docker Buildx
126-
uses: docker/setup-buildx-action@v3
126+
uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349
127127

128128
- name: Login to Docker Hub with Organization Token
129129
if: steps.verify_package.outputs.success == 'true'
130-
uses: docker/login-action@v3
130+
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567
131131
with:
132132
username: ${{ secrets.DOCKERHUB_USERNAME }}
133133
password: ${{ secrets.DOCKERHUB_TOKEN }}
134134

135135
- name: Build & Push Docker Preview
136136
if: steps.verify_package.outputs.success == 'true'
137-
uses: docker/build-push-action@v5
137+
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75
138138
env:
139139
VERSION: ${{ env.VERSION }}
140140
with:

.github/workflows/release.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ jobs:
1010
id-token: write
1111
contents: read
1212
steps:
13-
- uses: actions/checkout@v4
13+
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871
1414
with:
1515
fetch-depth: 0
16-
- uses: actions/setup-python@v5
16+
- uses: actions/setup-python@f677139bbe7f9c59b41e40162b753c062f5d49a3
1717
with:
1818
python-version: '3.x'
1919

@@ -66,16 +66,16 @@ jobs:
6666
6767
- name: Publish to PyPI
6868
if: steps.version_check.outputs.pypi_exists != 'true'
69-
uses: pypa/gh-action-pypi-publish@v1.12.4
69+
uses: pypa/gh-action-pypi-publish@ab69e431e9c9f48a3310be0a56527c679f56e04d
7070

7171
- name: Set up QEMU
72-
uses: docker/setup-qemu-action@v3
72+
uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf
7373

7474
- name: Set up Docker Buildx
75-
uses: docker/setup-buildx-action@v3
75+
uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349
7676

7777
- name: Login to Docker Hub with Organization Token
78-
uses: docker/login-action@v3
78+
uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567
7979
with:
8080
username: ${{ secrets.DOCKERHUB_USERNAME }}
8181
password: ${{ secrets.DOCKERHUB_TOKEN }}
@@ -102,7 +102,7 @@ jobs:
102102
if: |
103103
steps.verify_package.outputs.success == 'true' &&
104104
steps.docker_check.outputs.docker_exists != 'true'
105-
uses: docker/build-push-action@v5
105+
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75
106106
env:
107107
VERSION: ${{ env.VERSION }}
108108
with:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66

77
[project]
88
name = "socketsecurity"
9-
version = "2.2.12"
9+
version = "2.2.13"
1010
requires-python = ">= 3.10"
1111
license = {"file" = "LICENSE"}
1212
dependencies = [

socketsecurity/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
__author__ = 'socket.dev'
2-
__version__ = '2.2.12'
2+
__version__ = '2.2.13'
3+
USER_AGENT = f'SocketPythonCLI/{__version__}'

socketsecurity/core/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from socketdev.repos import RepositoryInfo
1919
from socketdev.settings import SecurityPolicyRule
2020
import copy
21-
from socketsecurity import __version__
21+
from socketsecurity import __version__, USER_AGENT
2222
from socketsecurity.core.classes import (
2323
Alert,
2424
Diff,
@@ -39,6 +39,7 @@
3939
"Core",
4040
"log",
4141
"__version__",
42+
"USER_AGENT",
4243
]
4344

4445
version = __version__

socketsecurity/core/cli_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import requests
66

7+
from socketsecurity import USER_AGENT
78
from .exceptions import APIFailure
89
from .socket_config import SocketConfig
910

@@ -31,7 +32,7 @@ def request(
3132

3233
default_headers = {
3334
'Authorization': f"Basic {self._encoded_key}",
34-
'User-Agent': 'SocketPythonCLI/0.0.1',
35+
'User-Agent': USER_AGENT,
3536
"accept": "application/json"
3637
}
3738

socketsecurity/core/scm/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from abc import abstractmethod
22
from typing import Dict
33

4+
from socketsecurity import USER_AGENT
45
from ..cli_client import CliClient
56

67

@@ -28,7 +29,7 @@ class GithubClient(ScmClient):
2829
def get_headers(self) -> Dict:
2930
return {
3031
'Authorization': f"Bearer {self.token}",
31-
'User-Agent': 'SocketPythonScript/0.0.1',
32+
'User-Agent': USER_AGENT,
3233
"accept": "application/json"
3334
}
3435

@@ -52,7 +53,7 @@ def _get_gitlab_auth_headers(token: str) -> dict:
5253
import os
5354

5455
base_headers = {
55-
'User-Agent': 'SocketPythonScript/0.0.1',
56+
'User-Agent': USER_AGENT,
5657
"accept": "application/json"
5758
}
5859

socketsecurity/core/scm/github.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from git import Optional
77

8+
from socketsecurity import USER_AGENT
89
from socketsecurity.core import log
910
from socketsecurity.core.classes import Comment
1011
from socketsecurity.core.scm_comments import Comments
@@ -83,7 +84,7 @@ def from_env(cls, pr_number: Optional[str] = None) -> 'GithubConfig':
8384
event_action=event_action,
8485
headers={
8586
'Authorization': f"Bearer {token}",
86-
'User-Agent': 'SocketPythonScript/0.0.1',
87+
'User-Agent': USER_AGENT,
8788
"accept": "application/json"
8889
}
8990
)

socketsecurity/core/scm/gitlab.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from dataclasses import dataclass
44
from typing import Optional
55

6+
from socketsecurity import USER_AGENT
67
from socketsecurity.core import log
78
from socketsecurity.core.classes import Comment
89
from socketsecurity.core.scm_comments import Comments
@@ -79,7 +80,7 @@ def _get_auth_headers(token: str) -> dict:
7980
- Other tokens: Use PRIVATE-TOKEN as fallback
8081
"""
8182
base_headers = {
82-
'User-Agent': 'SocketPythonScript/0.0.1',
83+
'User-Agent': USER_AGENT,
8384
"accept": "application/json"
8485
}
8586

@@ -150,7 +151,7 @@ def _get_fallback_headers(self, original_headers: dict) -> dict:
150151
If using Bearer, fallback to PRIVATE-TOKEN and vice versa.
151152
"""
152153
base_headers = {
153-
'User-Agent': 'SocketPythonScript/0.0.1',
154+
'User-Agent': USER_AGENT,
154155
"accept": "application/json"
155156
}
156157

@@ -171,11 +172,11 @@ def _get_fallback_headers(self, original_headers: dict) -> dict:
171172
}
172173

173174
# No fallback available
174-
return None
175+
return {}
175176

176177
def check_event_type(self) -> str:
177178
pipeline_source = self.config.pipeline_source.lower()
178-
if pipeline_source in ["web", 'merge_request_event', "push", "api"]:
179+
if pipeline_source in ["web", 'merge_request_event', "push", "api", 'pipeline']:
179180
if not self.config.mr_iid:
180181
return "main"
181182
return "diff"
@@ -234,8 +235,8 @@ def add_socket_comments(
234235
new_security_comment: bool = True,
235236
new_overview_comment: bool = True
236237
) -> None:
237-
existing_overview_comment = comments.get("overview")
238-
existing_security_comment = comments.get("security")
238+
existing_overview_comment = comments.get("overview", "")
239+
existing_security_comment = comments.get("security", "")
239240
if new_overview_comment:
240241
log.debug("New Dependency Overview comment")
241242
if existing_overview_comment is not None:
@@ -256,7 +257,7 @@ def add_socket_comments(
256257
self.post_comment(security_comment)
257258

258259
def remove_comment_alerts(self, comments: dict):
259-
security_alert = comments.get("security")
260+
security_alert = comments.get("security", "")
260261
if security_alert is not None:
261262
security_alert: Comment
262263
new_body = Comments.process_security_comment(security_alert, comments)

tests/unit/test_gitlab_auth.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
from unittest.mock import patch, MagicMock
55

6+
from socketsecurity import USER_AGENT
67
from socketsecurity.core.scm.gitlab import GitlabConfig
78

89

@@ -58,7 +59,7 @@ def test_all_headers_include_base_headers(self):
5859

5960
for token in test_tokens:
6061
headers = GitlabConfig._get_auth_headers(token)
61-
assert headers['User-Agent'] == 'SocketPythonScript/0.0.1'
62+
assert headers['User-Agent'] == USER_AGENT
6263
assert headers['accept'] == 'application/json'
6364

6465
@patch.dict(os.environ, {'CI_JOB_TOKEN': 'ci-token-123'})

0 commit comments

Comments
 (0)