Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redact ?token= querystring parameter from GHES <3.8 archive URLs in logs #1152

Merged
merged 8 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 2 additions & 1 deletion RELEASENOTES.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

- Skip GitHub status checking on startup if environment variable `GEI_SKIP_STATUS_CHECK` is set to true.
- Skip GitHub status checking on startup if environment variable `GEI_SKIP_STATUS_CHECK` is set to `true`
- Redact `?token=` querystring parameter from GHES <3.8 archive URLs in logs
11 changes: 11 additions & 0 deletions src/Octoshift/Services/OctoLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions;

namespace OctoshiftCLI.Services;

Expand Down Expand Up @@ -30,6 +31,11 @@ public class OctoLogger

private const string GENERIC_ERROR_MESSAGE = "An unexpected error happened. Please see the logs for details.";

private readonly HashSet<Regex> _redactionPatterns = new()
{
new Regex("\\b(?<=token=)(.+?)\\b")
};

public OctoLogger()
{
var logStartTime = DateTime.Now;
Expand Down Expand Up @@ -88,6 +94,11 @@ private string MaskSecrets(string msg)
.Replace(Uri.EscapeDataString(secret), "***");
}

foreach (var redactionPattern in _redactionPatterns)
{
result = redactionPattern.Replace(result, "***");
}

return result;
}

Expand Down
26 changes: 26 additions & 0 deletions src/OctoshiftCLI.Tests/Octoshift/Services/OctoLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ public void Secrets_Should_Be_Masked_From_Logs_And_Console()
_consoleError.Should().NotContain(urlEncodedSecret);
}

[Fact]
public void Ghes_Archive_Url_Tokens_Should_Be_Replaced_In_Logs_And_Console()
{

var ghesArchiveUrl = "https://files.github.acmeinc.com/foo?token=foobar";

_octoLogger.Verbose = false;
_octoLogger.LogInformation($"Archive URL: {ghesArchiveUrl}");
_octoLogger.LogVerbose($"Archive URL: {ghesArchiveUrl}");
_octoLogger.LogWarning($"Archive URL: {ghesArchiveUrl}");
_octoLogger.LogSuccess($"Archive URL: {ghesArchiveUrl}");
_octoLogger.LogError($"Archive URL: {ghesArchiveUrl}");
_octoLogger.LogError(new OctoshiftCliException("Archive URL: {ghesArchiveUrl}"));
_octoLogger.LogError(new InvalidOperationException("Archive URL: {ghesArchiveUrl}"));

_octoLogger.Verbose = true;
_octoLogger.LogVerbose("Archive URL: {ghesArchiveUrl}");

_consoleOutput.Should().NotContain(ghesArchiveUrl);
_logOutput.Should().NotContain(ghesArchiveUrl);
_verboseLogOutput.Should().NotContain(ghesArchiveUrl);
_consoleError.Should().NotContain(ghesArchiveUrl);

_consoleOutput.Should().Contain("Archive URL: https://files.github.acmeinc.com/foo?token=***");
}

[Fact]
public void LogError_For_OctoshiftCliException_Should_Log_Exception_Message_In_Non_Verbose_Mode()
{
Expand Down