Skip to content

Commit

Permalink
Merge pull request #707 from microsoft/malaref/limit-cookies
Browse files Browse the repository at this point in the history
Limit cookies to subdomains [READ]
  • Loading branch information
malaref authored Nov 15, 2024
2 parents 88e9445 + ca71fb6 commit 7114b46
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/clarity-js/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let config: Config = {
throttleDom: true,
conversions: false,
longTask: 30,
includeSubdomains: true,
};

export default config;
13 changes: 10 additions & 3 deletions packages/clarity-js/src/data/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export function shortid(): string {

function session(): Session {
let output: Session = { session: shortid(), ts: Math.round(Date.now()), count: 1, upgrade: null, upload: Constant.Empty };
let value = getCookie(Constant.SessionKey);
let value = getCookie(Constant.SessionKey, !config.includeSubdomains);
if (value) {
let parts = value.split(Constant.Pipe);
// Making it backward & forward compatible by using greater than comparison (v0.6.21)
Expand All @@ -234,7 +234,7 @@ function num(string: string, base: number = 10): number {

function user(): User {
let output: User = { id: shortid(), version: 0, expiry: null, consent: BooleanFlag.False, dob: 0 };
let cookie = getCookie(Constant.CookieKey);
let cookie = getCookie(Constant.CookieKey, !config.includeSubdomains);
if (cookie && cookie.length > 0) {
// Splitting and looking up first part for forward compatibility, in case we wish to store additional information in a cookie
let parts = cookie.split(Constant.Pipe);
Expand Down Expand Up @@ -266,7 +266,7 @@ function user(): User {
return output;
}

function getCookie(key: string): string {
function getCookie(key: string, limit = false): string {
if (supported(document, Constant.Cookie)) {
let cookies: string[] = document.cookie.split(Constant.Semicolon);
if (cookies) {
Expand All @@ -285,6 +285,13 @@ function getCookie(key: string): string {
[isEncoded, decodedValue] = decodeCookieValue(decodedValue);
}

// If we are limiting cookies, check if the cookie value is limited
if (limit) {
return decodedValue.endsWith(`${Constant.Tilde}1`)
? decodedValue.substring(0, decodedValue.length - 2)
: null;
}

return decodedValue;
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/clarity-js/types/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export interface Config {
throttleDom?: boolean;
conversions?: boolean;
longTask?: number;
includeSubdomains?: boolean;
}

export const enum Constant {
Expand Down

0 comments on commit 7114b46

Please sign in to comment.