Skip to content

Commit

Permalink
Decode ai_authUser URI (#1200)
Browse files Browse the repository at this point in the history
* Update check and add test.

* Add unencoded authUser tests and clean up authUser cookie parsing.
  • Loading branch information
JacksonWeber authored Sep 5, 2023
1 parent 84d57aa commit feb5241
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
19 changes: 15 additions & 4 deletions AutoCollection/HttpRequestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import CorrelationIdManager = require("../Library/CorrelationIdManager");
import Tracestate = require("../Library/Tracestate");
import Traceparent = require("../Library/Traceparent");
import { HttpRequest } from "../Library/Functions";

import Logging = require("../Library/Logging");
import { HttpRequestCookieNames } from "../Declarations/Constants";

/**
* Helper class to read data from the request/response objects and convert them into the telemetry contract
Expand Down Expand Up @@ -127,9 +128,9 @@ class HttpRequestParser extends RequestParser {

// don't override tags if they are already set
newTags[HttpRequestParser.keys.locationIp] = tags[HttpRequestParser.keys.locationIp] || this._getIp();
newTags[HttpRequestParser.keys.sessionId] = tags[HttpRequestParser.keys.sessionId] || this._getId("ai_session");
newTags[HttpRequestParser.keys.userId] = tags[HttpRequestParser.keys.userId] || this._getId("ai_user");
newTags[HttpRequestParser.keys.userAuthUserId] = tags[HttpRequestParser.keys.userAuthUserId] || this._getId("ai_authUser");
newTags[HttpRequestParser.keys.sessionId] = tags[HttpRequestParser.keys.sessionId] || this._getId(HttpRequestCookieNames.SESSION);
newTags[HttpRequestParser.keys.userId] = tags[HttpRequestParser.keys.userId] || this._getId(HttpRequestCookieNames.USER);
newTags[HttpRequestParser.keys.userAuthUserId] = tags[HttpRequestParser.keys.userAuthUserId] || this._getId(HttpRequestCookieNames.AUTH_USER);
newTags[HttpRequestParser.keys.operationName] = this.getOperationName(tags);
newTags[HttpRequestParser.keys.operationParentId] = this.getOperationParentId(tags);
newTags[HttpRequestParser.keys.operationId] = this.getOperationId(tags);
Expand Down Expand Up @@ -247,6 +248,16 @@ class HttpRequestParser extends RequestParser {
private _getId(name: string) {
var cookie = (this.rawHeaders && this.rawHeaders["cookie"] &&
typeof this.rawHeaders["cookie"] === "string" && this.rawHeaders["cookie"]) || "";

if (name === HttpRequestCookieNames.AUTH_USER) {
try {
cookie = decodeURI(cookie);
} catch (error) {
// Failed to decode, ignore cookie
cookie = "";
Logging.warn("Could not decode the auth cookie with error: ", Util.dumpObj(error));
}
}
var value = HttpRequestParser.parseId(Util.getCookie(name, cookie));
return value;
}
Expand Down
6 changes: 6 additions & 0 deletions Declarations/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ export enum StatsbeatNetworkCategory {
Quickpulse,
}

export enum HttpRequestCookieNames {
SESSION = "ai_session",
USER = "ai_user",
AUTH_USER = "ai_authUser",
}

//Azure SDK Span Attributes
export const AzNamespace = "az.namespace";
export const MicrosoftEventHub = "Microsoft.EventHub";
Expand Down
27 changes: 25 additions & 2 deletions Tests/AutoCollection/HttpRequestParser.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,22 @@ describe("AutoCollection/HttpRequestParser", () => {
headers: {
host: "bing.com",
"x-forwarded-for": "123.123.123.123",
"cookie": "ai_user=cookieUser|time;ai_session=cookieSession|time;ai_authUser=cookieAuthUser|time",
"cookie": `ai_user=cookieUser|time;ai_session=cookieSession|time;ai_authUser=${encodeURI("cookieAuthUser{}/test|time")}`,
"x-ms-request-id": "parentRequestId",
"x-ms-request-root-id": "operationId",
}
}

const request2 = {
method: "GET",
url: "/search?q=test",
connection: {
encrypted: false
},
headers: {
host: "bing.com",
"x-forwarded-for": "123.123.123.123",
"cookie": "ai_user=cookieUser|time;ai_session=cookieSession|time;ai_authUser=userAuthName|time",
"x-ms-request-id": "parentRequestId",
"x-ms-request-root-id": "operationId",
}
Expand Down Expand Up @@ -239,11 +254,19 @@ describe("AutoCollection/HttpRequestParser", () => {
var newTags = helper.getRequestTags(originalTags);
assert.equal(newTags[(<any>HttpRequestParser).keys.locationIp], '123.123.123.123');
assert.equal(newTags[(<any>HttpRequestParser).keys.userId], 'cookieUser');
assert.equal(newTags[(<any>HttpRequestParser).keys.userAuthUserId], 'cookieAuthUser');
assert.equal(newTags[(<any>HttpRequestParser).keys.userAuthUserId], 'cookieAuthUser{}/test');
assert.equal(newTags[(<any>HttpRequestParser).keys.userAgent], undefined);
assert.equal(newTags[(<any>HttpRequestParser).keys.operationName], 'GET /search');
assert.equal(newTags[(<any>HttpRequestParser).keys.operationId], 'operationId');
assert.equal(newTags[(<any>HttpRequestParser).keys.operationParentId], 'parentRequestId');
});

it("should read non-encoded auth user values from headers", () => {
const helper = new HttpRequestParser(<any>request2);
const originalTags: {[key: string]:string} = {};

var newTags = helper.getRequestTags(originalTags);
assert.equal(newTags[(<any>HttpRequestParser).keys.userAuthUserId], 'userAuthName');
});
});
});

0 comments on commit feb5241

Please sign in to comment.