From 49d531035a6ee58ee086757abc0c9e1c6b0e3477 Mon Sep 17 00:00:00 2001 From: Steve Faulkner Date: Tue, 2 Jul 2019 13:36:28 -0400 Subject: [PATCH] Fix browser atob errors (#363) * Fix errors where browser atob was hitting invalid characters decoding collection rid * Remove only --- src/session/sessionContainer.ts | 4 +++- test/unit/sessionContainer.spec.ts | 6 +++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/session/sessionContainer.ts b/src/session/sessionContainer.ts index e996df5abf46..b749c40d58cf 100644 --- a/src/session/sessionContainer.ts +++ b/src/session/sessionContainer.ts @@ -74,7 +74,9 @@ export class SessionContainer { private validateOwnerID(ownerId: string) { // If ownerId contains exactly 8 bytes it represents a unique database+collection identifier. Otherwise it represents another resource // The first 4 bytes are the database. The last 4 bytes are the collection. - return atob(ownerId).length === 8; + // Cosmos rids potentially contain "-" which is an invalid character in the browser atob implementation + // See https://en.wikipedia.org/wiki/Base64#Filenames + return atob(ownerId.replace(/-/g, "/")).length === 8; } private getPartitionKeyRangeIdToTokenMap(collectionName: string): Map { diff --git a/test/unit/sessionContainer.spec.ts b/test/unit/sessionContainer.spec.ts index 2a89a95b5cd8..c7c2965aa5fe 100644 --- a/test/unit/sessionContainer.spec.ts +++ b/test/unit/sessionContainer.spec.ts @@ -6,7 +6,7 @@ import { SessionContext } from "../../dist-esm/session/SessionContext"; describe("SessionContainer", function() { const collectionLink = "dbs/testDatabase/colls/testCollection"; - const collectionId = "oWxIAN48yN0="; + const collectionRid = "-EdBAKsiRLM="; it("set/get/delete", function() { const sc = new SessionContainer(); @@ -15,7 +15,7 @@ describe("SessionContainer", function() { const nameBasedRequest: SessionContext = { isNameBased: true, - resourceId: null, + resourceId: collectionRid, resourceAddress: "/" + collectionLink + "/", resourceType: ResourceType.item, operationType: OperationType.Create @@ -23,7 +23,7 @@ describe("SessionContainer", function() { const resHeadersNameBased: CosmosHeaders = {}; resHeadersNameBased[Constants.HttpHeaders.OwnerFullName] = collectionLink; - resHeadersNameBased[Constants.HttpHeaders.OwnerId] = collectionId; + resHeadersNameBased[Constants.HttpHeaders.OwnerId] = collectionRid; resHeadersNameBased[Constants.HttpHeaders.SessionToken] = tokenString; // Add a token and get new token, should be equal