4444
4545logger = logging .getLogger (__name__ )
4646
47+ # User-Agent string for all HTTP requests made by VulnerableCode
48+ VULNERABLECODE_USER_AGENT = "VulnerableCode/37.0.0 (https://github.com/aboutcode-org/vulnerablecode)"
49+
50+
51+ def get_http_headers (extra_headers = None ):
52+ """
53+ Return HTTP headers with the VulnerableCode User-Agent.
54+ Optionally merge with extra_headers if provided.
55+ """
56+ headers = {"User-Agent" : VULNERABLECODE_USER_AGENT }
57+ if extra_headers :
58+ headers .update (extra_headers )
59+ return headers
60+
4761cve_regex = re .compile (r"CVE-[0-9]{4}-[0-9]{4,19}" , re .IGNORECASE )
4862is_cve = cve_regex .match
4963find_all_cve = cve_regex .findall
@@ -75,7 +89,7 @@ def load_toml(path):
7589
7690
7791def fetch_yaml (url ):
78- response = requests .get (url )
92+ response = requests .get (url , headers = get_http_headers () )
7993 return saneyaml .load (response .content )
8094
8195
@@ -113,7 +127,7 @@ def contains_alpha(string):
113127def requests_with_5xx_retry (max_retries = 5 , backoff_factor = 0.5 ):
114128 """
115129 Returns a requests sessions which retries on 5xx errors with
116- a backoff_factor
130+ a backoff_factor. The session includes the VulnerableCode User-Agent header.
117131 """
118132 retries = urllib3 .Retry (
119133 total = max_retries ,
@@ -123,6 +137,7 @@ def requests_with_5xx_retry(max_retries=5, backoff_factor=0.5):
123137 )
124138 adapter = requests .adapters .HTTPAdapter (max_retries = retries )
125139 session = requests .Session ()
140+ session .headers .update (get_http_headers ())
126141 session .mount ("https://" , adapter )
127142 session .mount ("http://" , adapter )
128143 return session
@@ -284,7 +299,7 @@ def _get_gh_response(gh_token, graphql_query):
284299 Convenience function to easy mocking in tests
285300 """
286301 endpoint = "https://api.github.com/graphql"
287- headers = {"Authorization" : f"bearer { gh_token } " }
302+ headers = get_http_headers ( {"Authorization" : f"bearer { gh_token } " })
288303 try :
289304 return requests .post (endpoint , headers = headers , json = graphql_query ).json ()
290305 except Exception as e :
@@ -390,7 +405,7 @@ def fetch_response(url):
390405 Fetch and return `response` from the `url`
391406 """
392407 try :
393- response = requests .get (url )
408+ response = requests .get (url , headers = get_http_headers () )
394409 if response .status_code == HTTPStatus .OK :
395410 return response
396411 raise Exception (
0 commit comments