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

Ensure HeaderSanitizers apply to list-value headers #7417

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Azure.Sdk.Tools.TestProxy.Common;
using Azure.Sdk.Tools.TestProxy.Common;
using Azure.Sdk.Tools.TestProxy.Common.Exceptions;
using Azure.Sdk.Tools.TestProxy.Sanitizers;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -277,6 +277,35 @@ public void HeaderRegexSanitizerSimpleReplace()
Assert.StartsWith("https://fakeaccount.table.core.windows.net", testValue);
}


[Fact]
public void HeaderRegexSanitizerMultipartReplace()
{
var session = TestHelpers.LoadRecordSession("Test.RecordEntries/multipart_header.json");
var targetEntry = session.Session.Entries[0];
var targetKey = "Cookie";

var headerRegexSanitizer = new HeaderRegexSanitizer(targetKey, value: "REDACTED", regex: "SuperDifferent");
session.Session.Sanitize(headerRegexSanitizer);

Assert.Equal("REDACTEDCookie", targetEntry.Request.Headers[targetKey][0]);
Assert.Equal("KindaDifferentCookie", targetEntry.Request.Headers[targetKey][1]);
}

[Fact]
public void HeaderRegexSanitizerMultipartReplaceLatterOnly()
{
var session = TestHelpers.LoadRecordSession("Test.RecordEntries/multipart_header.json");
var targetEntry = session.Session.Entries[0];
var targetKey = "Cookie";

var headerRegexSanitizer = new HeaderRegexSanitizer(targetKey, value: "REDACTED", regex: "KindaDifferent");
session.Session.Sanitize(headerRegexSanitizer);

Assert.Equal("SuperDifferentCookie", targetEntry.Request.Headers[targetKey][0]);
Assert.Equal("REDACTEDCookie", targetEntry.Request.Headers[targetKey][1]);
}

[Fact]
public void HeaderRegexSanitizerGroupedRegexReplace()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"Entries": [
{
"RequestUri": "http://host.docker.internal:8080/sample_response",
"RequestMethod": "GET",
"RequestHeaders": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US",
"Connection": "keep-alive",
"Content-Length": "11",
"If-None-Match": "W/\u0022d-ua9UN3rfp1JzmLYtPUOjtDq\u002B3co\u0022",
"Referer": "http://localhost:9328/",
"sec-ch-ua": "",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-site",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/93.0.4577.0 Safari/537.36",
"x-ms-client-request-id": "7c65aa72-6b12-4b60-bf3b-7a85c174e2ba",
"x-ms-useragent": "core-rest-pipeline/1.3.3 OS/Win32",
"Cookie": [
"SuperDifferentCookie",
"KindaDifferentCookie"
]
},
"RequestBody": "RequestBody=",
"StatusCode": 304,
"ResponseHeaders": {
"Connection": "keep-alive",
"Keep-Alive": "timeout=5",
"X-Powered-By": "Express"
},
"ResponseBody": null
}
],
"Variables": {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Azure.Sdk.Tools.TestProxy.Common;
using Azure.Sdk.Tools.TestProxy.Common;
using System.Collections.Generic;
using System.Linq;

namespace Azure.Sdk.Tools.TestProxy.Sanitizers
{
Expand Down Expand Up @@ -47,11 +48,7 @@ public override void SanitizeHeaders(IDictionary<string, string[]> headers)
// We do this because letting .NET split and then reassemble header values introduces a space into the header itself
// Ex: "application/json;odata=minimalmetadata" with .NET default header parsing becomes "application/json; odata=minimalmetadata"
// Given this breaks signature verification, we have to avoid it.
var originalValue = headers[_targetKey][0];

var replacement = StringSanitizer.SanitizeValue(originalValue, _newValue, _regexValue, _groupForReplace);

headers[_targetKey] = new string[] { replacement };
headers[_targetKey] = headers[_targetKey].Select(x => StringSanitizer.SanitizeValue(x, _newValue, _regexValue, _groupForReplace)).ToArray();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Azure.Sdk.Tools.TestProxy.Common;
using Azure.Sdk.Tools.TestProxy.Common;
using System.Collections.Generic;
using System.Linq;

namespace Azure.Sdk.Tools.TestProxy.Sanitizers
{
Expand Down Expand Up @@ -39,11 +40,7 @@ public override void SanitizeHeaders(IDictionary<string, string[]> headers)
// We do this because letting .NET split and then reassemble header values introduces a space into the header itself
// Ex: "application/json;odata=minimalmetadata" with .NET default header parsing becomes "application/json; odata=minimalmetadata"
// Given this breaks signature verification, we have to avoid it.
var originalValue = headers[_targetKey][0];

var replacement = StringSanitizer.ReplaceValue(inputValue: originalValue, targetValue: _targetValue, replacementValue: _newValue);

headers[_targetKey] = new string[] { replacement };
headers[_targetKey] = headers[_targetKey].Select(x => StringSanitizer.ReplaceValue(inputValue: x, targetValue: _targetValue, replacementValue: _newValue)).ToArray();
}
}
}
Expand Down