From 929423eb85b20838dbd6e6d8f1b43b7089ce65e9 Mon Sep 17 00:00:00 2001 From: hrhosni Date: Thu, 28 Jan 2021 19:29:20 +0200 Subject: [PATCH 1/5] Update decodeOpaqueId.js This is required in order to be consistent with: https://github.com/reactioncommerce/api-utils/blob/f7690705c49c4a0d94799ed3deaf6be9ff909f76/lib/encodeOpaqueId.js#L20 Since the entire encoding/decoding of IDs was built for a reason that is no longer valid, we've found it very practical to simply set the environment variable REACTION_SHOULD_ENCODE_IDS to false. This makes development considerably easier, especially for quick API tests with graphQL playground. But in order for that to work, `decodeOpaqueId()` should also check for the env variable. If we don't do that, `decodeOpaqueId()` can return wrong `namespace` and `id` values. (Note that `decodeOpaqueId()` did NOT always fail prior to our change. For example, decoding ID `xifjgK9ckbptZ2ew2 ` will yield a null namespace and return the ID as is, whereas decoding ID `dTp6az9ghgoLSGGTo` will yield a namespace equal to `u` and a wrong ID) Signed-off-by: hrhosni --- lib/decodeOpaqueId.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/decodeOpaqueId.js b/lib/decodeOpaqueId.js index abf465a..3998af8 100644 --- a/lib/decodeOpaqueId.js +++ b/lib/decodeOpaqueId.js @@ -1,14 +1,22 @@ +import config from "./config.js"; + /** * @name decodeOpaqueId * @method * @memberof GraphQL/Transforms - * @summary Transforms an opaque ID to an internal ID + * @summary Transforms an opaque ID to an internal ID. Returns the `id` + * unchanged and the `namespace` as null if the `REACTION_SHOULD_ENCODE_IDS` + * environment variable is `false` * @param {String} opaqueId The ID to transform * @returns {String} An internal ID */ export default function decodeOpaqueId(opaqueId) { if (opaqueId === undefined || opaqueId === null) return null; + if (config.REACTION_SHOULD_ENCODE_IDS === false) { + return { namespace: null, id: opaqueId }; + } + const [namespace, id] = Buffer .from(opaqueId, "base64") .toString("utf8") From 8d6497287fd2f3ab099c1d7537b331c784e735aa Mon Sep 17 00:00:00 2001 From: hrhosni Date: Wed, 24 Feb 2021 17:27:31 +0200 Subject: [PATCH 2/5] test: added unit test for decodeOpaqueId Signed-off-by: hrhosni --- lib/decodeOpaqueId.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/decodeOpaqueId.test.js b/lib/decodeOpaqueId.test.js index 6112ca5..2d63ecb 100644 --- a/lib/decodeOpaqueId.test.js +++ b/lib/decodeOpaqueId.test.js @@ -1,5 +1,14 @@ +import { jest } from "@jest/globals"; +import config from "./config.js"; import decodeOpaqueId from "./decodeOpaqueId.js"; +jest.mock("./config.js", () => ({ + __esModule: true, // this property makes it work + default: { + REACTION_SHOULD_ENCODE_IDS: true, + }, +})); + test("decodes base64", () => { const encodedId = "cmVhY3Rpb24vc2hvcDpieTV3cGRnM25NcThnWDU0Yw=="; expect(decodeOpaqueId(encodedId)).toEqual({ @@ -15,3 +24,13 @@ test("passes through non-base64", () => { namespace: null }); }); + +test("skips decoding if REACTION_SHOULD_ENCODE_IDS env is false", async () => { + const id = "by5wpdg3nMq8gX54c"; + config.REACTION_SHOULD_ENCODE_IDS = false; + expect(decodeOpaqueId(id)).toEqual({ + id, + namespace: null + }); + config.REACTION_SHOULD_ENCODE_IDS = true; +}); From 822fc8690595ea229f671e8132d8051765ac1eb5 Mon Sep 17 00:00:00 2001 From: hrhosni Date: Wed, 24 Feb 2021 17:29:59 +0200 Subject: [PATCH 3/5] fix: linting Signed-off-by: hrhosni --- lib/decodeOpaqueId.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/decodeOpaqueId.test.js b/lib/decodeOpaqueId.test.js index 2d63ecb..563dc13 100644 --- a/lib/decodeOpaqueId.test.js +++ b/lib/decodeOpaqueId.test.js @@ -5,8 +5,8 @@ import decodeOpaqueId from "./decodeOpaqueId.js"; jest.mock("./config.js", () => ({ __esModule: true, // this property makes it work default: { - REACTION_SHOULD_ENCODE_IDS: true, - }, + REACTION_SHOULD_ENCODE_IDS: true + } })); test("decodes base64", () => { From 685e0b4b7d0646b6b7012fc267e97975db3f922e Mon Sep 17 00:00:00 2001 From: hrhosni Date: Wed, 24 Feb 2021 17:31:19 +0200 Subject: [PATCH 4/5] fix: linting Signed-off-by: hrhosni --- lib/decodeOpaqueId.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/decodeOpaqueId.js b/lib/decodeOpaqueId.js index 3998af8..a428445 100644 --- a/lib/decodeOpaqueId.js +++ b/lib/decodeOpaqueId.js @@ -16,7 +16,7 @@ export default function decodeOpaqueId(opaqueId) { if (config.REACTION_SHOULD_ENCODE_IDS === false) { return { namespace: null, id: opaqueId }; } - + const [namespace, id] = Buffer .from(opaqueId, "base64") .toString("utf8") From 23143c996869fb0e267c78d69ab4c5b67172f1a1 Mon Sep 17 00:00:00 2001 From: hrhosni Date: Wed, 24 Feb 2021 17:33:33 +0200 Subject: [PATCH 5/5] fix: linting Signed-off-by: hrhosni --- lib/decodeOpaqueId.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/decodeOpaqueId.js b/lib/decodeOpaqueId.js index a428445..bbe0a71 100644 --- a/lib/decodeOpaqueId.js +++ b/lib/decodeOpaqueId.js @@ -5,7 +5,7 @@ import config from "./config.js"; * @method * @memberof GraphQL/Transforms * @summary Transforms an opaque ID to an internal ID. Returns the `id` - * unchanged and the `namespace` as null if the `REACTION_SHOULD_ENCODE_IDS` + * unchanged and the `namespace` as null if the `REACTION_SHOULD_ENCODE_IDS` * environment variable is `false` * @param {String} opaqueId The ID to transform * @returns {String} An internal ID @@ -17,8 +17,7 @@ export default function decodeOpaqueId(opaqueId) { return { namespace: null, id: opaqueId }; } - const [namespace, id] = Buffer - .from(opaqueId, "base64") + const [namespace, id] = Buffer.from(opaqueId, "base64") .toString("utf8") .split(":", 2);