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

Limit cookies to subdomains [READ] #707

Merged
merged 1 commit into from
Nov 15, 2024
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
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