Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 27 additions & 35 deletions src/System.Management.Automation/help/UpdatableHelpSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -785,46 +785,36 @@ private bool DownloadHelpContentHttpClient(string uri, string fileName, Updatabl
{
client.Timeout = _defaultTimeout;
// codeql[cs/ssrf] - This is expected Poweshell behavior and the user assumes trust for the module they download and any URIs it references. The URIs are also not executables or scripts that would be invoked by this method.
Task<HttpResponseMessage> responseMsg = client.GetAsync(new Uri(uri), _cancelTokenSource.Token);
try
{
using HttpResponseMessage response = client.GetAsync(new Uri(uri), _cancelTokenSource.Token).GetAwaiter().GetResult();

// TODO: Should I use a continuation to write the stream to a file?
responseMsg.Wait();
if (_stopping)
{
return true;
}

if (_stopping)
{
return true;
}
lock (_syncObject)
{
_progressEvents.Add(new UpdatableHelpProgressEventArgs(CurrentModule, StringUtil.Format(
HelpDisplayStrings.UpdateProgressDownloading), 100));
}

if (!responseMsg.IsCanceled)
{
if (responseMsg.Exception != null)
if (response.IsSuccessStatusCode)
{
Errors.Add(new UpdatableHelpSystemException("HelpContentNotFound",
StringUtil.Format(HelpDisplayStrings.HelpContentNotFound),
ErrorCategory.ResourceUnavailable, null, responseMsg.Exception));
WriteResponseToFile(response, fileName);
}
else
{
lock (_syncObject)
{
_progressEvents.Add(new UpdatableHelpProgressEventArgs(CurrentModule, StringUtil.Format(
HelpDisplayStrings.UpdateProgressDownloading), 100));
}

// Write the stream to the specified file to achieve functional parity with WebClient.DownloadFileAsync().
HttpResponseMessage response = responseMsg.Result;
if (response.IsSuccessStatusCode)
{
WriteResponseToFile(response, fileName);
}
else
{
Errors.Add(new UpdatableHelpSystemException("HelpContentNotFound",
StringUtil.Format(HelpDisplayStrings.HelpContentNotFound),
ErrorCategory.ResourceUnavailable, null, responseMsg.Exception));
}
Errors.Add(new UpdatableHelpSystemException("HelpContentNotFound",
StringUtil.Format(HelpDisplayStrings.HelpContentNotFound),
ErrorCategory.ResourceUnavailable, null, null));
}
}
catch (OperationCanceledException)
{
return false;
}

SendProgressEvents(commandType);
}
Expand All @@ -843,11 +833,13 @@ private void WriteResponseToFile(HttpResponseMessage response, string fileName)
// TODO: Settings to use? FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite
using (FileStream downloadedFileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
Task copyStreamOp = response.Content.CopyToAsync(downloadedFileStream);
copyStreamOp.Wait();
if (copyStreamOp.Exception != null)
try
{
response.Content.CopyToAsync(downloadedFileStream).GetAwaiter().GetResult();
}
catch (Exception e)
{
Errors.Add(copyStreamOp.Exception);
Errors.Add(e);
}
}
}
Expand Down