Skip to content

Commit 223d8c2

Browse files
rads-1996Copilot
andauthored
Add a check to prevent non-string URLs from being passed to fieldRedaction method (#2667)
* Add a check to prevent non-string URLs from being passed tO fieldRedaction * Move the check to the fieldredaction method * Update shared/AppInsightsCore/Tests/Unit/src/ApplicationInsightsCore.Tests.ts removed unused config Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Updated fieldRedaction method with string check * Retrigger CI/CD pipeline * Retrigger CI/CD pipeline * Retrigger CI/CD pipeline --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a9c0b89 commit 223d8c2

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

shared/AppInsightsCore/Tests/Unit/src/ApplicationInsightsCore.Tests.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { Assert, AITestClass, PollingAssert } from "@microsoft/ai-test-framework
22
import {
33
IConfiguration, ITelemetryPlugin, ITelemetryItem, IPlugin, IAppInsightsCore, normalizeJsName,
44
random32, mwcRandomSeed, newId, randomValue, mwcRandom32, isNullOrUndefined, SenderPostManager,
5-
OnCompleteCallback, IPayloadData, _ISenderOnComplete, TransportType, _ISendPostMgrConfig, fieldRedaction
5+
OnCompleteCallback, IPayloadData, _ISenderOnComplete, TransportType, _ISendPostMgrConfig, fieldRedaction,
6+
isString
67
} from "../../../src/applicationinsights-core-js"
78
import { AppInsightsCore } from "../../../src/JavaScriptSDK/AppInsightsCore";
89
import { IChannelControls } from "../../../src/JavaScriptSDK.Interfaces/IChannelControls";
@@ -2106,6 +2107,38 @@ export class ApplicationInsightsCoreTests extends AITestClass {
21062107
}
21072108
});
21082109

2110+
this.testCase({
2111+
name: "FieldRedaction: should return non-string values unchanged without processing",
2112+
test: () => {
2113+
2114+
let config = {} as IConfiguration;
2115+
// Test with null - should return null unchanged
2116+
const nullUrl = null;
2117+
const redactedLocation = fieldRedaction(nullUrl as any, config);
2118+
Assert.strictEqual(redactedLocation, null, "fieldRedaction should return null unchanged");
2119+
2120+
// Test with number - should return the number unchanged
2121+
const numberUrl = 12345;
2122+
const redactedNumber = fieldRedaction(numberUrl as any, config);
2123+
Assert.strictEqual(redactedNumber, 12345, "fieldRedaction should return number unchanged");
2124+
2125+
// Test with object - should return the object unchanged
2126+
const objectUrl = { url: "https://example.com" };
2127+
const redactedObject = fieldRedaction(objectUrl as any, config);
2128+
Assert.strictEqual(redactedObject, objectUrl, "fieldRedaction should return object unchanged");
2129+
2130+
// Test with array - should return the array unchanged
2131+
const arrayUrl = ["https://example.com"];
2132+
const redactedArray = fieldRedaction(arrayUrl as any, config);
2133+
Assert.strictEqual(redactedArray, arrayUrl, "fieldRedaction should return array unchanged");
2134+
2135+
// Test with boolean - should return the boolean unchanged
2136+
const boolUrl = true;
2137+
const redactedBool = fieldRedaction(boolUrl as any, config);
2138+
Assert.strictEqual(redactedBool, true, "fieldRedaction should return boolean unchanged");
2139+
}
2140+
});
2141+
21092142
this.testCase({
21102143
name: "FieldRedaction: should handle URLs with Unicode characters",
21112144
test: () => {

shared/AppInsightsCore/src/JavaScriptSDK/EnvUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ function redactQueryParameters(url: string, config?: IConfiguration): string {
461461
* @returns The redacted URL string or the original string if no redaction was needed or possible.
462462
*/
463463
export function fieldRedaction(input: string, config: IConfiguration): string {
464-
if (!input || input.indexOf(" ") !== -1) {
464+
if (!input || !isString(input) || input.indexOf(" ") !== -1) {
465465
return input;
466466
}
467467
const isRedactionDisabled = config && config.redactUrls === false;

0 commit comments

Comments
 (0)