Skip to content

Commit 3533442

Browse files
authored
fix(checkver): Harden github checkver (#6641)
Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
1 parent cc8d7ca commit 3533442

2 files changed

Lines changed: 41 additions & 27 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- **core:** Check `$deprecated_dir` exists before accessing it ([#6574](https://github.com/ScoopInstaller/Scoop/issues/6574))
3535
- **checkver:** Remove redundant always-true condition in GitHub checkver logic ([#6571](https://github.com/ScoopInstaller/Scoop/issues/6571))
3636
- **core:** Give `dark` higher priority when use `Extract-DarkArchive` ([#6637](https://github.com/ScoopInstaller/Scoop/issues/6637))
37+
- **checkver:** Harden github checkver ([#6641](https://github.com/ScoopInstaller/Scoop/issues/6641))
3738

3839
### Code Refactoring
3940

bin/checkver.ps1

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Get-Event | Remove-Event
108108
Get-EventSubscriber | Unregister-Event
109109

110110
# start all downloads
111+
$in_progress = 0
111112
$Queue | ForEach-Object {
112113
$name, $json, $file = $_
113114

@@ -119,7 +120,6 @@ $Queue | ForEach-Object {
119120
} else {
120121
$wc.Headers.Add('User-Agent', (Get-UserAgent))
121122
}
122-
Register-ObjectEvent $wc downloadDataCompleted -ErrorAction Stop | Out-Null
123123

124124
# Not Specified
125125
if ($json.checkver.url) {
@@ -139,27 +139,46 @@ $Queue | ForEach-Object {
139139
$jsonpath = ''
140140
$xpath = ''
141141
$replace = ''
142-
$useGithubAPI = $false
143142

144-
# GitHub
145-
if ($regex) {
146-
$githubRegex = $regex
147-
} else {
148-
$githubRegex = '/releases/tag/(?:v|V)?([\d.]+)'
149-
}
150-
if ($json.checkver -eq 'github') {
151-
if (!$json.homepage.StartsWith('https://github.com/')) {
152-
error "$name checkver expects the homepage to be a github repository"
143+
## GitHub
144+
#
145+
# ```json
146+
# "homepage": "<valid-repository-url>",
147+
# "checkver": "github"
148+
# ```
149+
#
150+
# or
151+
#
152+
# ```json
153+
# "checkver": {
154+
# "github": "<valid-repository-url-or-repository-api-url>"
155+
# }
156+
# ```
157+
if (($json.checkver -eq 'github') -or $json.checkver.github) {
158+
$githubUrlPattern = '^https://((www\.)?github\.com/[\w.-]+/[\w.-]+/?|api\.github\.com/repos/[\w.-]+/[\w.-]+/.+)$'
159+
$regex = if ($regex) { $regex } else { '/releases/tag/(?:v|V)?([\d.]+)' }
160+
161+
$inputGithubUrl = $json.homepage
162+
$fieldUsed = 'homepage'
163+
if ($json.checkver.github) {
164+
$inputGithubUrl = $json.checkver.github
165+
$fieldUsed = 'checkver.github'
166+
}
167+
168+
if ($inputGithubUrl -notmatch $githubUrlPattern) {
169+
error "$name checkver expects $fieldUsed to be a valid GitHub repository URL"
170+
return
153171
}
154-
$url = $json.homepage.TrimEnd('/') + '/releases/latest'
155-
$regex = $githubRegex
156-
$useGithubAPI = $true
157-
}
158172

159-
if ($json.checkver.github) {
160-
$url = $json.checkver.github.TrimEnd('/') + '/releases/latest'
161-
$regex = $githubRegex
162-
$useGithubAPI = $true
173+
$url = $inputGithubUrl.TrimEnd('/')
174+
if ($url -notlike 'https://api.github.com*') {
175+
$url = $url + '/releases/latest'
176+
}
177+
178+
if ($GitHubToken) {
179+
$url = $url -replace '//(www\.)?github\.com/', '//api.github.com/repos/'
180+
$wc.Headers.Add('Authorization', "token $GitHubToken")
181+
}
163182
}
164183

165184
# SourceForge
@@ -216,13 +235,6 @@ $Queue | ForEach-Object {
216235

217236
$reverse = $json.checkver.reverse -and $json.checkver.reverse -eq 'true'
218237

219-
if ($url -like '*api.github.com/*') { $useGithubAPI = $true }
220-
221-
if ($useGithubAPI -and ($null -ne $GitHubToken)) {
222-
$url = $url -replace '//(www\.)?github.com/', '//api.github.com/repos/'
223-
$wc.Headers.Add('Authorization', "token $GitHubToken")
224-
}
225-
226238
$url = substitute $url $substitutions
227239

228240
$state = New-Object psobject @{
@@ -244,7 +256,9 @@ $Queue | ForEach-Object {
244256
}
245257

246258
$wc.Headers.Add('Referer', (strip_filename $url))
259+
Register-ObjectEvent $wc downloadDataCompleted -ErrorAction Stop | Out-Null
247260
$wc.DownloadDataAsync($url, $state)
261+
$in_progress++
248262
}
249263

250264
function next($er) {
@@ -253,7 +267,6 @@ function next($er) {
253267
}
254268

255269
# wait for all to complete
256-
$in_progress = $Queue.length
257270
while ($in_progress -gt 0) {
258271
$ev = Wait-Event
259272
Remove-Event $ev.SourceIdentifier

0 commit comments

Comments
 (0)