From 7e38641fd33872479b6507cd59d3fc796e5adead Mon Sep 17 00:00:00 2001 From: Greg Jopa <534034+gregjopa@users.noreply.github.com> Date: Mon, 21 Oct 2024 09:26:40 -0500 Subject: [PATCH] Add support for 'debug' query param for the web-sdk bridge --- server/meta.jsx | 14 +++++++++++- server/meta.test.js | 56 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/server/meta.jsx b/server/meta.jsx index bacd8f37..03984f3c 100644 --- a/server/meta.jsx +++ b/server/meta.jsx @@ -91,7 +91,12 @@ function validateWebSDKUrl({ pathname, query }) { ); } // check for extraneous parameters - const validWebSDKBridgeParams = ["origin", "version", "payment-flow"]; + const validWebSDKBridgeParams = [ + "origin", + "version", + "payment-flow", + "debug", + ]; for (const param of Object.keys(query)) { if (!validWebSDKBridgeParams.includes(param)) { throw new Error(`Invalid parameter on web-sdk bridge url: ${param}`); @@ -116,6 +121,13 @@ function validateWebSDKUrl({ pathname, query }) { ); } + // validate the optional debug parameter + if (query.debug && !["true", "false"].includes(query.debug)) { + throw new Error( + `Invalid debug parameter on web-sdk bridge url: ${query["debug"]}` + ); + } + // validate the origin parameter let url = null; try { diff --git a/server/meta.test.js b/server/meta.test.js index df7ba213..17167226 100644 --- a/server/meta.test.js +++ b/server/meta.test.js @@ -1224,7 +1224,7 @@ test("should error when invalid characters are found in the subdomain - we allow test("should construct a valid web-sdk bridge url", () => { const sdkUrl = - "https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler"; + "https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler&debug=true"; const sdkUID = "abc123"; const { getSDKLoader } = unpackSDKMeta( @@ -1276,6 +1276,35 @@ test("should error when extra parameters are present", () => { } }); +test("should not error when the optional debug parameter is missing", () => { + const sdkUrl = + "https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler"; + const sdkUID = "abc123"; + + const { getSDKLoader } = unpackSDKMeta( + Buffer.from( + JSON.stringify({ + url: sdkUrl, + attrs: { + "data-uid": sdkUID, + }, + }) + ).toString("base64") + ); + + const $ = cheerio.load(getSDKLoader()); + const script = $("script"); + const src = script.attr("src"); + const uid = script.attr("data-uid"); + + if (src !== sdkUrl) { + throw new Error(`Expected script url to be ${sdkUrl} - got ${src}`); + } + if (uid !== sdkUID) { + throw new Error(`Expected data UID be ${sdkUID} - got ${uid}`); + } +}); + test("should error when the version parameter is missing", () => { const sdkUrl = "https://www.paypal.com/web-sdk/v6/bridge?origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=payment-handler"; @@ -1425,3 +1454,28 @@ test("should error when the payment-flow parameter is invalid", () => { throw new Error("Expected error to be thrown"); } }); + +test("should error when the debug parameter is invalid", () => { + const sdkUrl = + "https://www.paypal.com/web-sdk/v6/bridge?version=1.2.3&origin=https%3A%2F%2Fwww.example.com%3A8000&payment-flow=popup&debug=invalid-value"; + + let error = null; + try { + unpackSDKMeta( + Buffer.from( + JSON.stringify({ + url: sdkUrl, + attrs: { + "data-uid": "abc123", + }, + }) + ).toString("base64") + ); + } catch (err) { + error = err; + } + + if (!error) { + throw new Error("Expected error to be thrown"); + } +});