Skip to content

Commit

Permalink
Decode the sdk meta if url encoded meta is passed (#165)
Browse files Browse the repository at this point in the history
* Decode the sdk meta if url encoded meta is passed

* decode inline for null safety

* fix flow error

* remove type conversion
  • Loading branch information
ravishekhar authored Sep 18, 2023
1 parent a102dd6 commit 6e1ad20
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 3 additions & 1 deletion server/meta.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ function sanitizeSDKUrl(sdkUrl: string): string {

export function unpackSDKMeta(sdkMeta?: string): SDKMeta {
const { url, attrs } = sdkMeta
? JSON.parse(Buffer.from(sdkMeta, "base64").toString("utf8"))
? JSON.parse(
Buffer.from(decodeURIComponent(sdkMeta), "base64").toString("utf8")
)
: DEFAULT_SDK_META;

if (url) {
Expand Down
2 changes: 2 additions & 0 deletions src/experiment.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ export function createExperiment(

logCheckpoint({ treatment, checkpoint, payload }) {
if (treatment.indexOf(name) !== -1) {
// $FlowFixMe
log.info(`${treatment}_${checkpoint}`, payload);
} else {
// $FlowFixMe
log.info(`${name}_${treatment}_${checkpoint}`, payload);
}

Expand Down
3 changes: 1 addition & 2 deletions src/tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ export function setupLogger() {
// https://www.paypal.com/buttons/smart
// https://www.paypal.com/us/gifts/
const isLoadedInFrame = isPayPalDomain() && window.xprops;
const sdkLoadTime =
typeof loadTime === "number" ? loadTime.toString() : undefined;
const sdkLoadTime = typeof loadTime === "number" ? loadTime : undefined;

logger
.info(`setup_${getEnv()}`)
Expand Down
22 changes: 22 additions & 0 deletions test/server/meta.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ test("should construct a valid script url with paypalobjects", () => {
}
});

test("should construct a valid script url with url encoded sdkMeta and trailing ? in checkout.js", () => {
const sdkUrl = "https://www.paypalobjects.com/api/checkout.js?";

const { getSDKLoader } = unpackSDKMeta(
encodeURIComponent(
Buffer.from(
JSON.stringify({
url: sdkUrl,
})
).toString("base64")
)
);

const $ = cheerio.load(getSDKLoader());
const script = $("script[data-paypal-checkout]");
const src = script.attr("src");

if (src !== "https://www.paypalobjects.com/api/checkout.js") {
throw new Error(`unexpected script url ${src}`);
}
});

test("should construct a valid script url with checkout.js using the qa cdn", () => {
const sdkUrl =
"https://uideploy--staticcontent--7482d416a81b5--ghe.preview.dev.paypalinc.com/api/checkout.js";
Expand Down

0 comments on commit 6e1ad20

Please sign in to comment.