Skip to content

Commit

Permalink
Url checking (#148)
Browse files Browse the repository at this point in the history
* Prompt user to use direct URL

* Formatting

* Fix: PS7 Resource Delegation

* Ignore github for url checking

* Add messaging for User Experience

* Fix bug where URI could contain invalid characters
  • Loading branch information
Trenly committed Feb 19, 2022
1 parent 485bbd1 commit 7c9f55e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
56 changes: 41 additions & 15 deletions Tools/YamlCreate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ Function Test-Url {
$HTTP_Request = [System.Net.WebRequest]::Create($URL)
$HTTP_Request.UserAgent = 'Microsoft-Delivery-Optimization/10.1'
$HTTP_Response = $HTTP_Request.GetResponse()
$script:ResponseUri = $HTTP_Response.ResponseUri.AbsoluteUri
$HTTP_Status = [int]$HTTP_Response.StatusCode
} catch {
# Take no action here; If there is an exception, we will treat it like a 404
Expand All @@ -319,22 +320,44 @@ Function Test-ValidFileName {
# Returns the validated URL which was entered
Function Request-InstallerUrl {
do {
Write-Host -ForegroundColor 'Red' $script:_returnValue.ErrorString()
Write-Host -ForegroundColor 'Green' -Object '[Required] Enter the download url to the installer.'
$NewInstallerUrl = Read-Host -Prompt 'Url' | TrimString
if (Test-String $NewInstallerUrl -MaxLength $Patterns.InstallerUrlMaxLength -MatchPattern $Patterns.InstallerUrl -NotNull) {
if ((Test-Url $NewInstallerUrl) -ne 200) {
$script:_returnValue = [ReturnValue]::new(502, 'Invalid URL Response', 'The URL did not return a successful response from the server', 2)
} else {
$script:_returnValue = [ReturnValue]::Success()
}
Write-Host -ForegroundColor $(if ($script:_returnValue.Severity -gt 1) { 'red' } else { 'yellow' }) $script:_returnValue.ErrorString()
if ($script:_returnValue.StatusCode -ne 409) {
Write-Host -ForegroundColor 'Green' -Object '[Required] Enter the download url to the installer.'
$NewInstallerUrl = Read-Host -Prompt 'Url' | TrimString
}
$script:_returnValue = [ReturnValue]::GenericError()
if ((Test-Url $NewInstallerUrl) -ne 200) {
$script:_returnValue = [ReturnValue]::new(502, 'Invalid URL Response', 'The URL did not return a successful response from the server', 2)
} else {
if (Test-String -not $NewInstallerUrl -MaxLength $Patterns.InstallerUrlMaxLength -NotNull) {
$script:_returnValue = [ReturnValue]::LengthError(1, $Patterns.InstallerUrlMaxLength)
} elseif (Test-String -not $NewInstallerUrl -MatchPattern $Patterns.InstallerUrl) {
$script:_returnValue = [ReturnValue]::PatternError()
} else {
$script:_returnValue = [ReturnValue]::GenericError()
if (($script:ResponseUri -ne $NewInstallerUrl) -and ($ScriptSettings.UseRedirectedURL -ne 'never') -and ($NewInstallerUrl -notmatch 'github')) {
#If urls don't match, ask to update; If they do update, set custom error and check for validity;
$_menu = @{
entries = @('*[Y] Use detected URL'; '[N] Use original URL')
Prompt = 'The URL provided appears to be redirected. Would you like to use the destination URL instead?'
HelpText = "Discovered URL: $($script:ResponseUri)"
DefaultString = 'Y'
}
switch ($(if ($ScriptSettings.UseRedirectedURL -eq 'always') { 'Y' } else { Invoke-KeypressMenu -Prompt $_menu['Prompt'] -Entries $_menu['Entries'] -DefaultString $_menu['DefaultString'] -HelpText $_menu['HelpText'] })) {
'N' { Write-Host -ForegroundColor 'Green' "`nOriginal URL Retained - Proceeding with $NewInstallerUrl`n" } #Continue without replacing URL
default {
$NewInstallerUrl = $script:ResponseUri
$script:_returnValue = [ReturnValue]::new(409, 'URL Changed', 'The URL was changed during processing and will be re-validated', 1)
Write-Host
}
}
}
if ($script:_returnValue.StatusCode -ne 409) {
if (Test-String $NewInstallerUrl -MaxLength $Patterns.InstallerUrlMaxLength -MatchPattern $Patterns.InstallerUrl -NotNull) {
$script:_returnValue = [ReturnValue]::Success()
} else {
if (Test-String -not $NewInstallerUrl -MaxLength $Patterns.InstallerUrlMaxLength -NotNull) {
$script:_returnValue = [ReturnValue]::LengthError(1, $Patterns.InstallerUrlMaxLength)
} elseif (Test-String -not $NewInstallerUrl -MatchPattern $Patterns.InstallerUrl) {
$script:_returnValue = [ReturnValue]::PatternError()
} else {
$script:_returnValue = [ReturnValue]::GenericError()
}
}
}
}
} until ($script:_returnValue.StatusCode -eq [ReturnValue]::Success().StatusCode)
Expand Down Expand Up @@ -879,12 +902,14 @@ Function Read-QuickInstallerEntry {

if ($_NewInstaller.Keys -notcontains 'InstallerSha256') {
try {
Write-Host -ForegroundColor 'Green' 'Downloading Installer. . .'
$script:dest = Get-InstallerFile -URI $_NewInstaller['InstallerUrl'] -PackageIdentifier $PackageIdentifier -PackageVersion $PackageVersion
} catch {
# Here we also want to pass any exceptions through for potential debugging
throw [System.Net.WebException]::new('The file could not be downloaded. Try running the script again', $_.Exception)
} finally {
# Check that MSI's aren't actually WIX
Write-Host -ForegroundColor 'Green' "Installer Downloaded!`nProcessing installer data. . . "
if ($_NewInstaller['InstallerType'] -eq 'msi') {
$DetectedType = Get-PathInstallerType $script:dest
if ($DetectedType -in @('msi'; 'wix')) { $_NewInstaller['InstallerType'] = $DetectedType }
Expand Down Expand Up @@ -933,6 +958,7 @@ Function Read-QuickInstallerEntry {
}
# Remove the downloaded files
Remove-Item -Path $script:dest
Write-Host -ForegroundColor 'Green' "Installer updated!`n"
}
}
#Add the updated installer to the new installers array
Expand Down
5 changes: 5 additions & 0 deletions doc/tools/YamlCreate.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ AutoSubmitPRs: ask
# never - Exits the script if conflicting PRs are detected
ContinueWithExistingPRs: ask

# This setting allows you to set a default action for when redirected URLs are detetected
# always - Always uses the detected destination URL
# never - Always uses the originally entered URL
UseRedirectedURL: ask

# This setting allows you to set a default value for whether or not you have signed the Microsoft CLA
# If this value is set to true, all automatic PR's will be marked as having the CLA signed
SignedCLA: false
Expand Down

0 comments on commit 7c9f55e

Please sign in to comment.