Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support new method to validate url #224

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});