Skip to content

Commit

Permalink
feat: support new method to validate url (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
ravishekhar authored Jan 29, 2025
1 parent 5b1e159 commit a1a1781
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/domains.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ export function isPayPalTrustedDomain(): boolean {
Boolean(getDomain().match(getVenmoDomainRegex()))
);
}

export function isPayPalTrustedUrl(href: string): boolean {
try {
// eslint-disable-next-line compat/compat
const url = new URL(href);
const domain = url.origin;
return (
Boolean(domain.match(getPayPalDomainRegex())) ||
Boolean(domain.match(getVenmoDomainRegex()))
);
} catch (err) {
return false;
}
}
73 changes: 72 additions & 1 deletion src/domains.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
/* @flow */
import { ENV } from "@paypal/sdk-constants/src";
import { describe, it, expect } from "vitest";
import {
describe,
it,
expect,
beforeEach,
afterEach,
beforeAll,
afterAll,
} from "vitest";

import {
getAuthAPIUrl,
getOrderAPIUrl,
getPayPalDomainRegex,
getVenmoDomainRegex,
isPayPalTrustedDomain,
isPayPalTrustedUrl,
} from "./domains";

describe(`domains test`, () => {
let env;
beforeEach(() => {
env = window.__ENV__;
});
afterEach(() => {
window.__ENV__ = env;
});

it("should successfully match valid paypal domain", () => {
const validDomains = [
"master.qa.paypal.com",
Expand Down Expand Up @@ -104,3 +121,57 @@ describe(`domains test`, () => {
expect(url.pathname).toEqual("/v2/checkout/orders");
});
});

describe(`isPayPalTrustedUrl test`, () => {
let env;
beforeAll(() => {
env = window.__ENV__;
window.__ENV__ = "production";
});
afterAll(() => {
window.__ENV__ = env;
});

const validUrls = [
"https://master.qa.paypal.com/abc/abc",
"https://master.qa.paypal.com",
"https://test-env.qa.paypal.com:3000/abc",
"https://geo.qa.paypal.com/abc",
"https://www.paypal.com:3080/abc",
"https://www.paypal.cn/abc",
"https://www.paypal.cn:3000/abc",
"https://www.mschina.qa.paypal.cn/abc",
"https://www.paypal.com/abc",
"https://www.paypal.com",
"https://venmo.com/abc",
"http://www.venmo.com",
"http://www.venmo.com/abc",
"https://id.venmo.com",
"http://www.venmo.com:8000",
"https://account.qa.venmo.com",
"http://www.account.qa.venmo.com",
"https://account.qa.venmo.com",
"https://account.venmo.com",
];

validUrls.forEach((url) => {
it(`isPayPalTrustedUrl(${url}) should be true`, () => {
const result = isPayPalTrustedUrl(url);
expect(result).toBe(true);
});
});

const unknownUrls = [
"https://www.paypal.com.example.com",
"https://www.paypal.cn.example.com",
"",
"---",
];

unknownUrls.forEach((url) => {
it(`isPayPalTrustedUrl(${url}) should be false`, () => {
const result = isPayPalTrustedUrl(url);
expect(result).toBe(false);
});
});
});

0 comments on commit a1a1781

Please sign in to comment.