Skip to content

Escape the persistence key everywhere & scope online state by project #1182

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

Merged
merged 1 commit into from
Aug 31, 2018
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
52 changes: 33 additions & 19 deletions packages/firestore/src/local/shared_client_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ const MUTATION_BATCH_KEY_PREFIX = 'firestore_mutations';
// firestore_targets_<persistence_prefix>_<target_id>
const QUERY_TARGET_KEY_PREFIX = 'firestore_targets';

// The LocalStorage key that stores the primary tab's online state.
const ONLINE_STATE_KEY = 'firestore_online_state';
// The LocalStorage prefix that stores the primary tab's online state. The
// format of the key is:
// firestore_online_state_<persistence_prefix>
const ONLINE_STATE_KEY_PREFIX = 'firestore_online_state';

/**
* A randomly-generated key assigned to each Firestore instance at startup.
Expand Down Expand Up @@ -511,6 +513,8 @@ export class WebStorageSharedClientState implements SharedClientState {
private readonly localClientStorageKey: string;
private readonly activeClients: { [key: string]: ClientState } = {};
private readonly storageListener = this.handleLocalStorageEvent.bind(this);
private readonly escapedPersistenceKey: string;
private readonly onlineStateKey: string;
private readonly clientStateKeyRe: RegExp;
private readonly mutationBatchKeyRe: RegExp;
private readonly queryTargetKeyRe: RegExp;
Expand All @@ -526,7 +530,7 @@ export class WebStorageSharedClientState implements SharedClientState {
constructor(
private readonly queue: AsyncQueue,
private readonly platform: Platform,
private readonly persistenceKey: string,
persistenceKey: string,
private readonly localClientId: ClientId,
initialUser: User
) {
Expand All @@ -536,30 +540,36 @@ export class WebStorageSharedClientState implements SharedClientState {
'LocalStorage is not available on this platform.'
);
}
// Escape the special characters mentioned here:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
this.escapedPersistenceKey = persistenceKey.replace(
/[.*+?^${}()|[\]\\]/g,
'\\$&'
);

this.storage = this.platform.window!.localStorage;
this.currentUser = initialUser;
this.localClientStorageKey = this.toLocalStorageClientStateKey(
this.localClientId
);
this.activeClients[this.localClientId] = new LocalClientState();

// Escape the special characters mentioned here:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
const escapedPersistenceKey = persistenceKey.replace(
/[.*+?^${}()|[\]\\]/g,
'\\$&'
);

this.clientStateKeyRe = new RegExp(
`^${CLIENT_STATE_KEY_PREFIX}_${escapedPersistenceKey}_([^_]*)$`
`^${CLIENT_STATE_KEY_PREFIX}_${this.escapedPersistenceKey}_([^_]*)$`
);
this.mutationBatchKeyRe = new RegExp(
`^${MUTATION_BATCH_KEY_PREFIX}_${escapedPersistenceKey}_(\\d+)(?:_(.*))?$`
`^${MUTATION_BATCH_KEY_PREFIX}_${
this.escapedPersistenceKey
}_(\\d+)(?:_(.*))?$`
);
this.queryTargetKeyRe = new RegExp(
`^${QUERY_TARGET_KEY_PREFIX}_${escapedPersistenceKey}_(\\d+)$`
`^${QUERY_TARGET_KEY_PREFIX}_${this.escapedPersistenceKey}_(\\d+)$`
);

this.onlineStateKey = `${ONLINE_STATE_KEY_PREFIX}_${
this.escapedPersistenceKey
}`;

// Rather than adding the storage observer during start(), we add the
// storage observer during initialization. This ensures that we collect
// events before other components populate their initial state (during their
Expand Down Expand Up @@ -612,7 +622,7 @@ export class WebStorageSharedClientState implements SharedClientState {

// Check if there is an existing online state and call the callback handler
// if applicable.
const onlineStateJSON = this.storage.getItem(ONLINE_STATE_KEY);
const onlineStateJSON = this.storage.getItem(this.onlineStateKey);
if (onlineStateJSON) {
const onlineState = this.fromLocalStorageOnlineState(onlineStateJSON);
if (onlineState) {
Expand Down Expand Up @@ -821,7 +831,7 @@ export class WebStorageSharedClientState implements SharedClientState {
return this.handleQueryTargetEvent(queryTargetMetadata);
}
}
} else if (event.key === ONLINE_STATE_KEY) {
} else if (event.key === this.onlineStateKey) {
if (event.newValue !== null) {
const onlineState = this.fromLocalStorageOnlineState(
event.newValue
Expand Down Expand Up @@ -871,7 +881,7 @@ export class WebStorageSharedClientState implements SharedClientState {
clientId: this.localClientId,
onlineState: OnlineState[onlineState]
};
this.storage.setItem(ONLINE_STATE_KEY, JSON.stringify(entry));
this.storage.setItem(this.onlineStateKey, JSON.stringify(entry));
}

private persistQueryTargetState(
Expand All @@ -891,18 +901,22 @@ export class WebStorageSharedClientState implements SharedClientState {
`Client key cannot contain '_', but was '${clientId}'`
);

return `${CLIENT_STATE_KEY_PREFIX}_${this.persistenceKey}_${clientId}`;
return `${CLIENT_STATE_KEY_PREFIX}_${
this.escapedPersistenceKey
}_${clientId}`;
}

/** Assembles the key for a query state in LocalStorage */
private toLocalStorageQueryTargetMetadataKey(targetId: TargetId): string {
return `${QUERY_TARGET_KEY_PREFIX}_${this.persistenceKey}_${targetId}`;
return `${QUERY_TARGET_KEY_PREFIX}_${
this.escapedPersistenceKey
}_${targetId}`;
}

/** Assembles the key for a mutation batch in LocalStorage */
private toLocalStorageMutationBatchKey(batchId: BatchId): string {
let mutationKey = `${MUTATION_BATCH_KEY_PREFIX}_${
this.persistenceKey
this.escapedPersistenceKey
}_${batchId}`;

if (this.currentUser.isAuthenticated()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function targetKey(targetId: TargetId): string {
}

function onlineStateKey(): string {
return 'firestore_online_state';
return `firestore_online_state_${persistenceHelpers.TEST_PERSISTENCE_PREFIX}`;
}

interface TestSharedClientState {
Expand Down