Skip to content

Commit 3caa741

Browse files
Merge pull request #14 from GitTimeraider/alert-autofix-1
Potential fix for code scanning alert no. 1: Incomplete URL substring sanitization
2 parents 891c0da + 8dd5209 commit 3caa741

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

backup_service.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pathlib import Path
99
from github import Github
1010
from models import db, BackupJob
11+
from urllib.parse import urlparse
1112

1213
logger = logging.getLogger(__name__)
1314

@@ -179,22 +180,26 @@ def _get_file_size(self, path):
179180
def verify_github_access(self, repo_url, github_token=None):
180181
"""Verify if we can access a GitHub repository"""
181182
try:
182-
# Extract owner and repo name from URL
183-
if 'github.com/' in repo_url:
184-
parts = repo_url.split('github.com/')[-1].split('/')
185-
if len(parts) >= 2:
186-
owner = parts[0]
187-
repo_name = parts[1].replace('.git', '')
188-
183+
# Parse the URL and check the hostname
184+
parsed = urlparse(repo_url)
185+
if parsed.hostname and parsed.hostname.lower() == "github.com":
186+
# Path is of the form /owner/repo(.git)? or /owner/repo/
187+
path_parts = parsed.path.strip("/").split("/")
188+
if len(path_parts) >= 2:
189+
owner = path_parts[0]
190+
repo_name = path_parts[1]
191+
if repo_name.endswith('.git'):
192+
repo_name = repo_name[:-4]
193+
189194
if github_token:
190195
g = Github(github_token)
191196
else:
192197
g = Github() # Anonymous access for public repos
193-
198+
194199
repo = g.get_repo(f"{owner}/{repo_name}")
195200
return True, f"Repository access verified: {repo.full_name}"
196-
201+
197202
return False, "Invalid GitHub repository URL"
198-
203+
199204
except Exception as e:
200205
return False, f"Repository access failed: {str(e)}"

0 commit comments

Comments
 (0)