Validate portable release discovery manifests
This commit is contained in:
parent
d2ab740ace
commit
dc95a3e8db
|
|
@ -92,6 +92,29 @@
|
|||
"executable": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Za-z0-9_-][A-Za-z0-9._/-]*\\.exe$"
|
||||
},
|
||||
"releaseApi": {
|
||||
"type": "string",
|
||||
"pattern": "^https://[^/]+(?:/[^/]+)*/api/v1/repos/[^/]+/[^/]+/releases/?$"
|
||||
},
|
||||
"releaseChannel": {
|
||||
"enum": [
|
||||
"stable",
|
||||
"prerelease"
|
||||
]
|
||||
},
|
||||
"assetTemplate": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Za-z0-9._+{}-]*\\{version\\}[A-Za-z0-9._+{}-]*\\.zip$"
|
||||
},
|
||||
"executableTemplate": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Za-z0-9_{}-][A-Za-z0-9._/{}/-]*\\.exe$"
|
||||
},
|
||||
"checksumAsset": {
|
||||
"type": "string",
|
||||
"pattern": "^[A-Za-z0-9._+-]+$",
|
||||
"default": "SHA256SUMS"
|
||||
}
|
||||
},
|
||||
"allOf": [
|
||||
|
|
@ -148,6 +171,34 @@
|
|||
"install-only"
|
||||
]
|
||||
}
|
||||
},
|
||||
"dependentRequired": {
|
||||
"releaseApi": [
|
||||
"releaseChannel",
|
||||
"assetTemplate",
|
||||
"executableTemplate"
|
||||
],
|
||||
"releaseChannel": [
|
||||
"releaseApi",
|
||||
"assetTemplate",
|
||||
"executableTemplate"
|
||||
],
|
||||
"assetTemplate": [
|
||||
"releaseApi",
|
||||
"releaseChannel",
|
||||
"executableTemplate"
|
||||
],
|
||||
"executableTemplate": [
|
||||
"releaseApi",
|
||||
"releaseChannel",
|
||||
"assetTemplate"
|
||||
],
|
||||
"checksumAsset": [
|
||||
"releaseApi",
|
||||
"releaseChannel",
|
||||
"assetTemplate",
|
||||
"executableTemplate"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -170,6 +221,31 @@
|
|||
"required": [
|
||||
"executable"
|
||||
]
|
||||
},
|
||||
{
|
||||
"required": [
|
||||
"releaseApi"
|
||||
]
|
||||
},
|
||||
{
|
||||
"required": [
|
||||
"releaseChannel"
|
||||
]
|
||||
},
|
||||
{
|
||||
"required": [
|
||||
"assetTemplate"
|
||||
]
|
||||
},
|
||||
{
|
||||
"required": [
|
||||
"executableTemplate"
|
||||
]
|
||||
},
|
||||
{
|
||||
"required": [
|
||||
"checksumAsset"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,6 +61,31 @@ function Assert-PortableDeclaration {
|
|||
if ($Item.sha256 -notmatch '^[a-fA-F0-9]{64}$') { throw 'Portable sha256 must contain 64 hex digits.' }
|
||||
if ($Item.executable -notmatch '^[A-Za-z0-9_-][A-Za-z0-9._/-]*\.exe$' -or @($Item.executable -split '/' | Where-Object { $_ -in @('', '.', '..') -or $_ -match '\.$|^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\.|$)' }).Count) { throw 'Unsafe portable executable path.' }
|
||||
if ($Item.update -notin @('pinned', 'install-only')) { throw 'Portable packages require pinned or install-only updates.' }
|
||||
|
||||
$releaseFields = @('releaseApi', 'releaseChannel', 'assetTemplate', 'executableTemplate')
|
||||
$hasReleaseField = [bool]($releaseFields | Where-Object { $null -ne $Item.PSObject.Properties[$_] })
|
||||
if ($hasReleaseField) {
|
||||
foreach ($field in $releaseFields) {
|
||||
if ([string]::IsNullOrWhiteSpace([string](Get-OptionalProperty $Item $field))) { throw "Portable release discovery requires $field." }
|
||||
}
|
||||
$releaseUri = $null
|
||||
if (-not [uri]::TryCreate($Item.releaseApi, [UriKind]::Absolute, [ref]$releaseUri) -or
|
||||
$releaseUri.Scheme -ne 'https' -or $releaseUri.UserInfo -or $releaseUri.Query -or $releaseUri.Fragment -or
|
||||
$releaseUri.AbsolutePath -notmatch '/api/v1/repos/[^/]+/[^/]+/releases/?$') {
|
||||
throw 'Portable releaseApi must be an HTTPS Gitea releases endpoint.'
|
||||
}
|
||||
if ($Item.releaseChannel -notin @('stable', 'prerelease')) { throw 'Portable releaseChannel must be stable or prerelease.' }
|
||||
if ([regex]::Matches($Item.assetTemplate, '\{version\}').Count -ne 1 -or
|
||||
$Item.assetTemplate -notmatch '^[A-Za-z0-9._+{}-]+\.zip$') { throw 'Portable assetTemplate must be a ZIP filename with one {version} placeholder.' }
|
||||
if ([regex]::Matches($Item.executableTemplate, '\{version\}').Count -ne 1) { throw 'Portable executableTemplate requires one {version} placeholder.' }
|
||||
$exampleExecutable = $Item.executableTemplate.Replace('{version}', '1.2.3')
|
||||
if ($exampleExecutable -notmatch '^[A-Za-z0-9_-][A-Za-z0-9._/-]*\.exe$' -or
|
||||
@($exampleExecutable -split '/' | Where-Object { $_ -in @('', '.', '..') -or $_ -match '\.$|^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\.|$)' }).Count) {
|
||||
throw 'Unsafe portable executableTemplate.'
|
||||
}
|
||||
$checksumAsset = [string](Get-OptionalProperty $Item 'checksumAsset' 'SHA256SUMS')
|
||||
if ($checksumAsset -notmatch '^[A-Za-z0-9._+-]+$') { throw 'Portable checksumAsset must be a filename.' }
|
||||
}
|
||||
}
|
||||
|
||||
function Assert-ItemList {
|
||||
|
|
@ -72,7 +97,10 @@ function Assert-ItemList {
|
|||
$index = 0
|
||||
foreach ($item in $Items) {
|
||||
if ($Kind -eq 'package') {
|
||||
Assert-AllowedProperties $item @('id', 'source', 'scope', 'version', 'update', 'enabled', 'provider', 'url', 'sha256', 'executable') "$Location[$index]"
|
||||
Assert-AllowedProperties $item @(
|
||||
'id', 'source', 'scope', 'version', 'update', 'enabled', 'provider', 'url', 'sha256', 'executable',
|
||||
'releaseApi', 'releaseChannel', 'assetTemplate', 'executableTemplate', 'checksumAsset'
|
||||
) "$Location[$index]"
|
||||
if ([string]::IsNullOrWhiteSpace([string](Get-OptionalProperty $item 'id'))) { throw "$Location[$index].id is required." }
|
||||
$source = [string](Get-OptionalProperty $item 'source' 'winget')
|
||||
$key = "$source/$($item.id)"
|
||||
|
|
@ -82,7 +110,7 @@ function Assert-ItemList {
|
|||
$key = "portable/$key"
|
||||
Assert-PortableDeclaration $item $source
|
||||
}
|
||||
elseif (@('url', 'sha256', 'executable') | Where-Object { $null -ne $item.PSObject.Properties[$_] }) { throw 'Portable fields cannot be used by WinGet packages.' }
|
||||
elseif (@('url', 'sha256', 'executable', 'releaseApi', 'releaseChannel', 'assetTemplate', 'executableTemplate', 'checksumAsset') | Where-Object { $null -ne $item.PSObject.Properties[$_] }) { throw 'Portable fields cannot be used by WinGet packages.' }
|
||||
$scope = Get-OptionalProperty $item 'scope'
|
||||
if ($null -ne $scope -and $scope -notin @('user', 'machine')) { throw "Invalid package scope at $Location[$index]." }
|
||||
$update = [string](Get-OptionalProperty $item 'update' 'latest')
|
||||
|
|
@ -151,7 +179,10 @@ function Add-ManifestItems {
|
|||
|
||||
if ($Kind -eq 'package') {
|
||||
$normalized = [ordered]@{ id = [string]$item.id; source = $source; update = [string](Get-OptionalProperty $item 'update' 'latest') }
|
||||
foreach ($field in @('scope', 'version', 'provider', 'url', 'sha256', 'executable')) {
|
||||
foreach ($field in @(
|
||||
'scope', 'version', 'provider', 'url', 'sha256', 'executable',
|
||||
'releaseApi', 'releaseChannel', 'assetTemplate', 'executableTemplate', 'checksumAsset'
|
||||
)) {
|
||||
$value = Get-OptionalProperty $item $field
|
||||
if ($null -ne $value) { $normalized[$field] = [string]$value }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,5 +42,36 @@ Describe 'Portable declarations' {
|
|||
$item.provider = 'winget'
|
||||
{ Assert-ItemList @($item) package test } | Should -Throw '*Portable fields*'
|
||||
}
|
||||
|
||||
It 'accepts and preserves a complete release discovery declaration' {
|
||||
$item | Add-Member releaseApi 'https://example.org/gitea/api/v1/repos/owner/project/releases'
|
||||
$item | Add-Member releaseChannel 'prerelease'
|
||||
$item | Add-Member assetTemplate 'presenter-v{version}.zip'
|
||||
$item | Add-Member executableTemplate 'presenter-v{version}/presenter.exe'
|
||||
$item | Add-Member checksumAsset 'SHA256SUMS'
|
||||
Assert-ItemList @($item) package test
|
||||
$items = [ordered]@{}
|
||||
Add-ManifestItems $items @($item) package
|
||||
$items['portable/gitea/presenter'].releaseChannel | Should -Be 'prerelease'
|
||||
$items['portable/gitea/presenter'].assetTemplate | Should -Be 'presenter-v{version}.zip'
|
||||
}
|
||||
|
||||
It 'rejects incomplete or unsafe release discovery declarations' -ForEach @(
|
||||
@{ Field = 'releaseApi'; Value = 'http://example.org/api/v1/repos/a/b/releases' }
|
||||
@{ Field = 'releaseApi'; Value = 'https://example.org/not-gitea' }
|
||||
@{ Field = 'releaseChannel'; Value = 'nightly' }
|
||||
@{ Field = 'assetTemplate'; Value = '../app-{version}.zip' }
|
||||
@{ Field = 'assetTemplate'; Value = 'app.zip' }
|
||||
@{ Field = 'executableTemplate'; Value = '../{version}/app.exe' }
|
||||
@{ Field = 'checksumAsset'; Value = '../SHA256SUMS' }
|
||||
) {
|
||||
$item | Add-Member releaseApi 'https://example.org/api/v1/repos/a/b/releases'
|
||||
$item | Add-Member releaseChannel 'stable'
|
||||
$item | Add-Member assetTemplate 'app-{version}.zip'
|
||||
$item | Add-Member executableTemplate 'app-{version}/app.exe'
|
||||
$item | Add-Member checksumAsset 'SHA256SUMS'
|
||||
$item | Add-Member -NotePropertyName $Field -NotePropertyValue $Value -Force
|
||||
{ Assert-ItemList @($item) package test } | Should -Throw
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue