Skip to content

Commit 2196ba0

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

1 file changed

Lines changed: 37 additions & 21 deletions

File tree

bin/checkver.ps1

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,44 @@ $Queue | ForEach-Object {
141141
$replace = ''
142142
$useGithubAPI = $false
143143

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

159-
if ($json.checkver.github) {
160-
$url = $json.checkver.github.TrimEnd('/') + '/releases/latest'
161-
$regex = $githubRegex
162-
$useGithubAPI = $true
178+
if ($GitHubToken) {
179+
$url = $url -replace '//(www\.)?github.com/', '//api.github.com/repos/'
180+
$useGithubAPI = $true
181+
}
163182
}
164183

165184
# SourceForge
@@ -216,10 +235,7 @@ $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/'
238+
if ($useGithubAPI) {
223239
$wc.Headers.Add('Authorization', "token $GitHubToken")
224240
}
225241

0 commit comments

Comments
 (0)