Skip to content

Commit

Permalink
adding check on whether the redirect respons is a valid redirect resp…
Browse files Browse the repository at this point in the history
…onse status code. (#6543)
  • Loading branch information
arjgupta authored Feb 27, 2018
1 parent 1e3e611 commit ae67428
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,7 @@
"loc.messages.AFC_UnableToSetCustomScriptExtension": "Unable to set the custom script extension '{0}' for virtual machine '{1}': {2}",
"loc.messages.AFC_CopyPrereqsFailed": "Failed to enable copy prerequisites. {0}",
"loc.messages.AFC_BlobStorageNotFound": "Storage account: {0} not found. Please specify existing storage account",
"loc.messages.AFC_RootContainerAndDirectory": "'/S' option is not valid for $root containers."
"loc.messages.AFC_RootContainerAndDirectory": "'/S' option is not valid for $root containers.",
"loc.messages.AFC_RedirectResponseInvalidStatusCode": "The HTTP response code: '{0}' is not a valid redirect status code",
"loc.messages.AFC_RedirectResponseLocationHeaderIsNull": "Redirect response location header is null."
}
23 changes: 16 additions & 7 deletions Tasks/AzureFileCopy/Utility.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1277,24 +1277,33 @@ function Is-WinRMCustomScriptExtensionExists

function Get-TargetUriFromFwdLink {
param(
[string]$fwdLink,
[string]$extensionName,
[string]$vmName
[string]$fwdLink
)
Write-Verbose "Trying to get the target uri from the fwdLink: $fwdLink"
$proxy = Get-VstsWebProxy
Add-Type -AssemblyName System.Net.Http
$validHttpRedirectCodes = @(
[System.Net.HttpStatusCode]::Moved,
[System.Net.HttpStatusCode]::MovedPermanently,
[System.Net.HttpStatusCode]::Found,
[System.Net.HttpStatusCode]::Redirect,
[System.Net.HttpStatusCode]::RedirectKeepVerb,
[System.Net.HttpStatusCode]::TemporaryRedirect
)
$HttpClientHandler = New-Object System.Net.Http.HttpClientHandler
$HttpClientHandler.Proxy = $proxy
$HttpClientHandler.AllowAutoRedirect = $false
$HttpClient = New-Object System.Net.Http.HttpClient -ArgumentList $HttpClientHandler
$response = $HttpClient.GetAsync($fwdLink)
$response.Wait()
if($validHttpRedirectCodes.IndexOf($response.Result.StatusCode) -eq -1) {
Write-Verbose "The http response code: $([int]$response.Result.StatusCode) is not a valid redirect response code."
throw (Get-VstsLocString -Key "AFC_RedirectResponseInvalidStatusCode" -ArgumentList $([int]$response.Result.StatusCode))
}
$targetUri = $response.Result.Headers.Location.AbsoluteUri
if([string]::IsNullOrEmpty($targetUri)) {
Write-Verbose "The target uri is null"
$errMessage = "targetUri = $targetUri"
throw (Get-VstsLocString -Key "AFC_SetCustomScriptExtensionFailed" -ArgumentList $extensionName, $vmName, $errMessage)
throw (Get-VstsLocString -Key "AFC_RedirectResponseLocationHeaderIsNull")
}
Write-Verbose "The target uri is: $targetUri"
return $targetUri
Expand Down Expand Up @@ -1361,8 +1370,8 @@ function Add-AzureVMCustomScriptExtension
return
}

$configWinRMScriptFile = Get-TargetUriFromFwdLink -fwdLink $configWinRMScriptFileFwdLink -extensionName $extensionName -vmName $vmName
$makeCertFile = Get-TargetUriFromFwdLink -fwdLink $makeCertFileFwdLink -extensionName $extensionName -vmName $vmName
$configWinRMScriptFile = Get-TargetUriFromFwdLink -fwdLink $configWinRMScriptFileFwdLink
$makeCertFile = Get-TargetUriFromFwdLink -fwdLink $makeCertFileFwdLink

$result = Set-AzureMachineCustomScriptExtension -resourceGroupName $resourceGroupName -vmName $vmName -name $extensionName -fileUri $configWinRMScriptFile, $makeCertFile -run $scriptToRun -argument $dnsName -location $location
$resultDetails = $result | ConvertTo-Json
Expand Down
4 changes: 3 additions & 1 deletion Tasks/AzureFileCopy/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@
"AFC_UnableToSetCustomScriptExtension": "Unable to set the custom script extension '{0}' for virtual machine '{1}': {2}",
"AFC_CopyPrereqsFailed": "Failed to enable copy prerequisites. {0}",
"AFC_BlobStorageNotFound": "Storage account: {0} not found. Please specify existing storage account",
"AFC_RootContainerAndDirectory": "'/S' option is not valid for $root containers."
"AFC_RootContainerAndDirectory": "'/S' option is not valid for $root containers.",
"AFC_RedirectResponseInvalidStatusCode": "The HTTP response code: '{0}' is not a valid redirect status code",
"AFC_RedirectResponseLocationHeaderIsNull": "Redirect response location header is null."
}
}
4 changes: 3 additions & 1 deletion Tasks/AzureFileCopy/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@
"AFC_UnableToSetCustomScriptExtension": "ms-resource:loc.messages.AFC_UnableToSetCustomScriptExtension",
"AFC_CopyPrereqsFailed": "ms-resource:loc.messages.AFC_CopyPrereqsFailed",
"AFC_BlobStorageNotFound": "ms-resource:loc.messages.AFC_BlobStorageNotFound",
"AFC_RootContainerAndDirectory": "ms-resource:loc.messages.AFC_RootContainerAndDirectory"
"AFC_RootContainerAndDirectory": "ms-resource:loc.messages.AFC_RootContainerAndDirectory",
"AFC_RedirectResponseInvalidStatusCode": "ms-resource:loc.messages.AFC_RedirectResponseInvalidStatusCode",
"AFC_RedirectResponseLocationHeaderIsNull": "ms-resource:loc.messages.AFC_RedirectResponseLocationHeaderIsNull"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,7 @@
"loc.messages.FailedToUploadFile": "Failed to upload file '%s/%s from Kudu. Error: %s",
"loc.messages.FailedToGetFileContent": "Failed to get file content '%s/%s' from Kudu. Error: %s",
"loc.messages.FailedToListPath": "Failed to list path '%s'. Error: %s",
"loc.messages.FailedToGetDeploymentLogs": "Failed to get deployment logs. Error: %s"
"loc.messages.FailedToGetDeploymentLogs": "Failed to get deployment logs. Error: %s",
"loc.messages.ARG_RedirectResponseInvalidStatusCode": "The HTTP response code: '%s' is not a valid redirect status code.",
"loc.messages.ARG_RedirectResponseLocationHeaderIsNull": "Location header is null for HTTP response with status code: %s"
}
4 changes: 3 additions & 1 deletion Tasks/Common/azure-arm-rest/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@
"FailedToUploadFile": "Failed to upload file '%s/%s from Kudu. Error: %s",
"FailedToGetFileContent": "Failed to get file content '%s/%s' from Kudu. Error: %s",
"FailedToListPath": "Failed to list path '%s'. Error: %s",
"FailedToGetDeploymentLogs": "Failed to get deployment logs. Error: %s"
"FailedToGetDeploymentLogs": "Failed to get deployment logs. Error: %s",
"ARG_RedirectResponseInvalidStatusCode": "The HTTP response code: '%s' is not a valid redirect status code.",
"ARG_RedirectResponseLocationHeaderIsNull": "Location header is null for HTTP response with status code: %s"
}
}
4 changes: 2 additions & 2 deletions Tasks/Common/azure-arm-rest/webRequestUtility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class WebRequestUtility {
httpRequest.uri = fwdLink;
var httpResponse = await webClient.sendRequest(httpRequest);
if(HttpRedirectCodes.indexOf(httpResponse.statusCode) == -1) {
throw new Error(`HttpResponse statuscode: ${httpResponse.statusCode} is not a valid HTTP redirect code.`);
throw new Error(tl.loc('ARG_RedirectResponseInvalidStatusCode', httpResponse.statusCode));
}
var targetLink: string = httpResponse.headers["location"];
if(!targetLink) {
throw new Error(`Unable to find location header after HTTP ${httpResponse.statusCode} redirect.`);
throw new Error(tl.loc('ARG_RedirectResponseLocationHeaderIsNull', httpResponse.statusCode));
}
tl.debug("the target link is : " + targetLink);
return targetLink;
Expand Down

0 comments on commit ae67428

Please sign in to comment.