@tonder.io/ionic-lite-sdk 1.0.1 → 1.0.2-beta.TEC-1207.53fb846
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -3
- package/dist/classes/3dsHandler.d.ts +26 -2
- package/dist/classes/BaseInlineCheckout.d.ts +12 -9
- package/dist/classes/liteCheckout.d.ts +1 -1
- package/dist/data/businessApi.d.ts +1 -1
- package/dist/data/coreApi.d.ts +15 -0
- package/dist/helpers/corePayment.d.ts +60 -0
- package/dist/helpers/safeUrl.d.ts +15 -0
- package/dist/helpers/sanitize3dsMarkup.d.ts +18 -0
- package/dist/helpers/utils.d.ts +1 -0
- package/dist/helpers/validations.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/shared/constants/apiEndpoints.d.ts +2 -0
- package/dist/shared/utils/appError.d.ts +9 -0
- package/dist/types/commons.d.ts +5 -0
- package/dist/types/core.d.ts +28 -0
- package/jest.config.ts +11 -1
- package/package.json +1 -1
- package/src/classes/3dsHandler.ts +134 -30
- package/src/classes/BaseInlineCheckout.ts +128 -16
- package/src/classes/liteCheckout.ts +9 -2
- package/src/data/businessApi.ts +13 -6
- package/src/data/coreApi.ts +47 -0
- package/src/helpers/corePayment.ts +211 -0
- package/src/helpers/safeUrl.ts +26 -0
- package/src/helpers/sanitize3dsMarkup.ts +133 -0
- package/src/helpers/utils.ts +4 -0
- package/src/helpers/validations.ts +9 -0
- package/src/shared/constants/apiEndpoints.ts +2 -0
- package/src/shared/constants/messages.ts +4 -4
- package/src/shared/utils/appError.ts +28 -11
- package/src/types/commons.ts +5 -0
- package/src/types/core.ts +30 -0
- package/tests/coreBusinessConfig.test.ts +100 -0
- package/tests/corePayment.test.ts +393 -0
- package/tests/corePaymentCheckout.test.ts +668 -0
- package/tests/coreStartupFailure.test.ts +70 -0
- package/tests/redirectToChallenge.test.ts +149 -0
- package/tests/sanitize3dsMarkup.test.ts +248 -0
- package/tests/secureTokenBoundary.test.ts +81 -0
- package/tests/threeDsApiKey.test.ts +41 -0
- package/tests/threeDsCore.test.ts +444 -0
- package/tests/threeDsPolling.test.ts +45 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { LiteCheckout } from "../src/classes/liteCheckout";
|
|
2
|
+
import { ErrorKeyEnum } from "../src/shared/enum/ErrorKeyEnum";
|
|
3
|
+
|
|
4
|
+
const CORE = "https://api-stage.tonder.io";
|
|
5
|
+
|
|
6
|
+
function answer(status: number, body: () => Promise<unknown>) {
|
|
7
|
+
return { ok: status < 400, status, json: body, clone() { return this; } };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const FAILURES: [string, () => Promise<unknown>][] = [
|
|
11
|
+
["a 403", async () => answer(403, async () => ({ error: { code: "forbidden" } }))],
|
|
12
|
+
["a 401", async () => answer(401, async () => ({}))],
|
|
13
|
+
["a 503", async () => answer(503, async () => ({}))],
|
|
14
|
+
["a network error", async () => { throw new TypeError("Failed to fetch"); }],
|
|
15
|
+
["a body that is not JSON", async () => answer(200, async () => { throw new SyntaxError("Unexpected token <"); })],
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
describe("start-up when the business configuration cannot be read", () => {
|
|
19
|
+
let fetchMock: jest.Mock;
|
|
20
|
+
|
|
21
|
+
function failWith(reply: () => Promise<unknown>) {
|
|
22
|
+
fetchMock = jest.fn(async (url: string) => {
|
|
23
|
+
if (url === `${CORE}/transactions/v1/business/config`) return reply();
|
|
24
|
+
throw new Error(`unexpected request to ${url}`);
|
|
25
|
+
});
|
|
26
|
+
(global as any).fetch = fetchMock;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
it.each(FAILURES)("injectCheckout rejects with a clear error after %s", async (_label, reply) => {
|
|
30
|
+
failWith(reply);
|
|
31
|
+
const checkout = new LiteCheckout({ mode: "stage", apiKey: "key" });
|
|
32
|
+
|
|
33
|
+
await expect(checkout.injectCheckout()).rejects.toMatchObject({
|
|
34
|
+
code: ErrorKeyEnum.FETCH_BUSINESS_ERROR,
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it.each(FAILURES)("mountCardFields rejects with a clear error after %s", async (_label, reply) => {
|
|
39
|
+
failWith(reply);
|
|
40
|
+
const checkout = new LiteCheckout({ mode: "stage", apiKey: "key" });
|
|
41
|
+
|
|
42
|
+
await expect(checkout.mountCardFields({ fields: ["card_number"] } as any)).rejects.toMatchObject({
|
|
43
|
+
code: ErrorKeyEnum.FETCH_BUSINESS_ERROR,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("never falls back to the legacy business configuration", async () => {
|
|
48
|
+
failWith(FAILURES[0][1]);
|
|
49
|
+
const checkout = new LiteCheckout({ mode: "stage", apiKey: "key" });
|
|
50
|
+
|
|
51
|
+
await checkout.injectCheckout().catch(() => {});
|
|
52
|
+
|
|
53
|
+
const urls = fetchMock.mock.calls.map(([url]) => url as string);
|
|
54
|
+
expect(urls).toContain(`${CORE}/transactions/v1/business/config`);
|
|
55
|
+
expect(urls.filter((url) => url.includes("/payments/business/"))).toEqual([]);
|
|
56
|
+
expect(urls.filter((url) => new URL(url).host === "stage.tonder.io")).toEqual([]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("asks again on the next call instead of caching the failure", async () => {
|
|
60
|
+
failWith(FAILURES[2][1]);
|
|
61
|
+
const checkout = new LiteCheckout({ mode: "stage", apiKey: "key" });
|
|
62
|
+
await checkout.injectCheckout().catch(() => {});
|
|
63
|
+
|
|
64
|
+
failWith(async () =>
|
|
65
|
+
answer(200, async () => ({ business: { pk: 7 }, openpay_keys: {}, vault_id: "v", vault_url: "u" })),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
await expect(checkout.injectCheckout()).resolves.toBeUndefined();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { ThreeDSHandler } from "../src/classes/3dsHandler";
|
|
2
|
+
import { AppError } from "../src/shared/utils/appError";
|
|
3
|
+
import { ErrorKeyEnum } from "../src/shared/enum/ErrorKeyEnum";
|
|
4
|
+
|
|
5
|
+
const CHALLENGE_URL = "https://acs.example.com/challenge?id=42";
|
|
6
|
+
const IFRAME_ID = "tdsIframe";
|
|
7
|
+
|
|
8
|
+
const realLocation = Object.getOwnPropertyDescriptor(window, "location")!;
|
|
9
|
+
|
|
10
|
+
// jsdom's window.location has no setter, and a real assignment would navigate. Swapping
|
|
11
|
+
// in a plain data property records exactly what redirectToChallenge assigns to it.
|
|
12
|
+
function stubLocation(): { href: string } {
|
|
13
|
+
const stub = { href: "http://localhost/" };
|
|
14
|
+
Object.defineProperty(window, "location", {
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: stub,
|
|
18
|
+
});
|
|
19
|
+
return stub;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function buildHandler(url: string, redirectOnComplete: boolean): ThreeDSHandler {
|
|
23
|
+
return new ThreeDSHandler({
|
|
24
|
+
payload: {
|
|
25
|
+
next_action: {
|
|
26
|
+
redirect_to_url: {
|
|
27
|
+
url,
|
|
28
|
+
verify_transaction_status_url: "/api/v1/verify/42",
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
apiKey: "api-key",
|
|
33
|
+
baseUrl: "https://stage.tonder.io",
|
|
34
|
+
redirectOnComplete,
|
|
35
|
+
tdsIframeId: IFRAME_ID,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function mountIframe(): HTMLIFrameElement {
|
|
40
|
+
const iframe = document.createElement("iframe");
|
|
41
|
+
iframe.id = IFRAME_ID;
|
|
42
|
+
document.body.appendChild(iframe);
|
|
43
|
+
return iframe;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe("ThreeDSHandler.redirectToChallenge", () => {
|
|
47
|
+
beforeEach(() => {
|
|
48
|
+
// Giving the iframe a src makes jsdom fire load, which drives the status poller.
|
|
49
|
+
// Without a stub that call reaches an undefined global fetch and tears the worker
|
|
50
|
+
// down before any assertion is reported.
|
|
51
|
+
(global as any).fetch = jest.fn().mockResolvedValue({
|
|
52
|
+
status: 200,
|
|
53
|
+
json: async () => ({ transaction_status: "Success" }),
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
Object.defineProperty(window, "location", realLocation);
|
|
59
|
+
document.body.innerHTML = "";
|
|
60
|
+
localStorage.clear();
|
|
61
|
+
delete (global as any).fetch;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe("window.location sink", () => {
|
|
65
|
+
it("navigates to a legitimate https challenge URL", () => {
|
|
66
|
+
const stub = stubLocation();
|
|
67
|
+
const handler = buildHandler(CHALLENGE_URL, true);
|
|
68
|
+
|
|
69
|
+
handler.redirectToChallenge();
|
|
70
|
+
|
|
71
|
+
expect(window.location as unknown as string).toBe(CHALLENGE_URL);
|
|
72
|
+
expect(window.location).not.toBe(stub);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("never assigns a javascript: URL to window.location", () => {
|
|
76
|
+
const stub = stubLocation();
|
|
77
|
+
const handler = buildHandler("javascript:alert(document.cookie)", true);
|
|
78
|
+
|
|
79
|
+
expect(() => handler.redirectToChallenge()).toThrow(AppError);
|
|
80
|
+
|
|
81
|
+
expect(window.location).toBe(stub);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe("iframe src sink", () => {
|
|
86
|
+
it("points the iframe at a legitimate https challenge URL and reveals it", () => {
|
|
87
|
+
const iframe = mountIframe();
|
|
88
|
+
const handler = buildHandler(CHALLENGE_URL, false);
|
|
89
|
+
|
|
90
|
+
handler.redirectToChallenge();
|
|
91
|
+
|
|
92
|
+
expect(iframe.getAttribute("src")).toBe(CHALLENGE_URL);
|
|
93
|
+
expect(iframe.getAttribute("style")).toBe("display: block");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("never assigns a javascript: URL to the iframe src", () => {
|
|
97
|
+
const iframe = mountIframe();
|
|
98
|
+
const handler = buildHandler("javascript:alert(document.cookie)", false);
|
|
99
|
+
|
|
100
|
+
expect(() => handler.redirectToChallenge()).toThrow(AppError);
|
|
101
|
+
|
|
102
|
+
expect(iframe.getAttribute("src")).toBeNull();
|
|
103
|
+
expect(iframe.getAttribute("style")).toBeNull();
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe("rejection", () => {
|
|
108
|
+
it("reports a 3DS redirection error the caller can surface", () => {
|
|
109
|
+
stubLocation();
|
|
110
|
+
const handler = buildHandler("javascript:alert(1)", true);
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
handler.redirectToChallenge();
|
|
114
|
+
throw new Error("expected redirectToChallenge to throw");
|
|
115
|
+
} catch (error) {
|
|
116
|
+
expect(error).toBeInstanceOf(AppError);
|
|
117
|
+
expect((error as AppError).code).toBe(ErrorKeyEnum.THREEDS_REDIRECTION_ERROR);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("does not persist the verify transaction URL for a refused challenge", () => {
|
|
122
|
+
stubLocation();
|
|
123
|
+
const handler = buildHandler("javascript:alert(1)", true);
|
|
124
|
+
|
|
125
|
+
expect(() => handler.redirectToChallenge()).toThrow(AppError);
|
|
126
|
+
|
|
127
|
+
expect(handler.getVerifyTransactionUrl()).toBeNull();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it.each([
|
|
131
|
+
["leading whitespace", " javascript:alert(1)"],
|
|
132
|
+
["mixed case", "JaVaScRiPt:alert(1)"],
|
|
133
|
+
["an embedded tab", "java\tscript:alert(1)"],
|
|
134
|
+
["an embedded newline", "java\nscript:alert(1)"],
|
|
135
|
+
["a comment that merely contains http", "javascript:/*http*/alert(1)"],
|
|
136
|
+
["a data: URL", "data:text/html,<script>alert(1)</script>"],
|
|
137
|
+
["a vbscript: URL", "vbscript:msgbox(1)"],
|
|
138
|
+
["a blob: URL", "blob:http://localhost/8f1c2b3a"],
|
|
139
|
+
["a file: URL", "file:///etc/passwd"],
|
|
140
|
+
])("refuses %s", (_label, url) => {
|
|
141
|
+
const stub = stubLocation();
|
|
142
|
+
const handler = buildHandler(url, true);
|
|
143
|
+
|
|
144
|
+
expect(() => handler.redirectToChallenge()).toThrow(AppError);
|
|
145
|
+
|
|
146
|
+
expect(window.location).toBe(stub);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
});
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { ThreeDSHandler } from "../src/classes/3dsHandler";
|
|
2
|
+
import { sanitize3dsMarkup } from "../src/helpers/sanitize3dsMarkup";
|
|
3
|
+
|
|
4
|
+
// Shape of the markup the ACS returns for the 3DS Method step: a hidden form plus the
|
|
5
|
+
// iframe it targets. The flow depends on both surviving sanitization.
|
|
6
|
+
const THREE_DS_METHOD_MARKUP = `
|
|
7
|
+
<iframe id="tdsMmethodTgtFrame" name="tdsMmethodTgtFrame" style="display: none"></iframe>
|
|
8
|
+
<form id="tdsMmethodForm" name="tdsMmethodForm" target="tdsMmethodTgtFrame"
|
|
9
|
+
action="https://acs.example.com/3ds-method" method="POST">
|
|
10
|
+
<input type="hidden" name="threeDSMethodData" value="eyJ0aHJlZURTTWV0aG9kTm90aWZpY2F0aW9uVVJMIjoiIn0" />
|
|
11
|
+
</form>
|
|
12
|
+
`;
|
|
13
|
+
|
|
14
|
+
function buildHandler(iframe: string): ThreeDSHandler {
|
|
15
|
+
return new ThreeDSHandler({
|
|
16
|
+
payload: {
|
|
17
|
+
next_action: {
|
|
18
|
+
iframe_resources: {
|
|
19
|
+
iframe,
|
|
20
|
+
verify_transaction_status_url: "/api/v1/verify/1",
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
apiKey: "api-key",
|
|
25
|
+
baseUrl: "https://stage.tonder.io",
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
describe("sanitize3dsMarkup", () => {
|
|
30
|
+
it("keeps the form, its hidden input and the target iframe the 3DS Method step needs", () => {
|
|
31
|
+
const container = document.createElement("div");
|
|
32
|
+
container.appendChild(sanitize3dsMarkup(THREE_DS_METHOD_MARKUP));
|
|
33
|
+
|
|
34
|
+
const form = container.querySelector("form");
|
|
35
|
+
expect(form).not.toBeNull();
|
|
36
|
+
expect(form!.id).toBe("tdsMmethodForm");
|
|
37
|
+
expect(form!.getAttribute("action")).toBe("https://acs.example.com/3ds-method");
|
|
38
|
+
expect(form!.getAttribute("target")).toBe("tdsMmethodTgtFrame");
|
|
39
|
+
expect(container.querySelector("input[name='threeDSMethodData']")).not.toBeNull();
|
|
40
|
+
expect(container.querySelector("iframe#tdsMmethodTgtFrame")).not.toBeNull();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("removes inline event handlers", () => {
|
|
44
|
+
const container = document.createElement("div");
|
|
45
|
+
container.appendChild(
|
|
46
|
+
sanitize3dsMarkup(`<img src="https://acs.example.com/x.png" onerror="stealCard()" />`),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const img = container.querySelector("img")!;
|
|
50
|
+
expect(img.getAttribute("onerror")).toBeNull();
|
|
51
|
+
expect(img.getAttribute("src")).toBe("https://acs.example.com/x.png");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("removes script elements", () => {
|
|
55
|
+
const container = document.createElement("div");
|
|
56
|
+
container.appendChild(sanitize3dsMarkup(`<div></div><script>stealCard()</script>`));
|
|
57
|
+
|
|
58
|
+
expect(container.querySelector("script")).toBeNull();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("drops non-https URLs while keeping https and relative ones", () => {
|
|
62
|
+
const container = document.createElement("div");
|
|
63
|
+
container.appendChild(
|
|
64
|
+
sanitize3dsMarkup(
|
|
65
|
+
`<a id="js" href="javascript:stealCard()"></a>` +
|
|
66
|
+
`<iframe id="srcdoc" srcdoc="<script>stealCard()</script>"></iframe>` +
|
|
67
|
+
`<form id="rel" action="/api/v1/challenge"></form>`,
|
|
68
|
+
),
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
expect(container.querySelector("#js")!.getAttribute("href")).toBeNull();
|
|
72
|
+
expect(container.querySelector("#srcdoc")!.getAttribute("srcdoc")).toBeNull();
|
|
73
|
+
expect(container.querySelector("#rel")!.getAttribute("action")).toBe("/api/v1/challenge");
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("removes script elements the parser leaves in the SVG namespace", () => {
|
|
77
|
+
const container = document.createElement("div");
|
|
78
|
+
container.appendChild(
|
|
79
|
+
sanitize3dsMarkup(`<div></div><svg><script>stealCard()</script></svg>`),
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
// tagName is "script" in lowercase here, so an uppercase set never matched it and
|
|
83
|
+
// the element travelled into the page intact.
|
|
84
|
+
expect(container.querySelector("script")).toBeNull();
|
|
85
|
+
expect(container.querySelector("svg")).toBeNull();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("removes an SVG link whose javascript: URL hides behind a namespace prefix", () => {
|
|
89
|
+
const container = document.createElement("div");
|
|
90
|
+
container.appendChild(
|
|
91
|
+
sanitize3dsMarkup(
|
|
92
|
+
`<svg><a id="svg-link" xlink:href="javascript:stealCard()"><text>pay</text></a></svg>`,
|
|
93
|
+
),
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
// The qualified name is "xlink:href", which the URL set never matched, so the
|
|
97
|
+
// scheme check was never reached.
|
|
98
|
+
expect(container.querySelector("#svg-link")).toBeNull();
|
|
99
|
+
expect(container.innerHTML).not.toContain("javascript:");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("removes style elements, whose selectors are not scoped to the fragment", () => {
|
|
103
|
+
const container = document.createElement("div");
|
|
104
|
+
container.appendChild(
|
|
105
|
+
sanitize3dsMarkup(
|
|
106
|
+
`<div></div><style>input{background:url("https://attacker.example/x")}</style>`,
|
|
107
|
+
),
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
expect(container.querySelector("style")).toBeNull();
|
|
111
|
+
expect(container.innerHTML).not.toContain("attacker.example");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("removes the style attribute, which the caller no longer depends on", () => {
|
|
115
|
+
const container = document.createElement("div");
|
|
116
|
+
container.appendChild(sanitize3dsMarkup(THREE_DS_METHOD_MARKUP));
|
|
117
|
+
|
|
118
|
+
// The step's own markup carries style="display: none" here, but the SDK hides its
|
|
119
|
+
// container itself, so nothing is lost and an overlay over the card fields has no
|
|
120
|
+
// way in.
|
|
121
|
+
expect(container.querySelector("iframe")!.getAttribute("style")).toBeNull();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("removes an element-scoped style that would cover the card fields", () => {
|
|
125
|
+
const container = document.createElement("div");
|
|
126
|
+
container.appendChild(
|
|
127
|
+
sanitize3dsMarkup(
|
|
128
|
+
`<div id="overlay" style="position:fixed;top:0;left:0;width:100vw;height:100vh"></div>`,
|
|
129
|
+
),
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
expect(container.querySelector("#overlay")!.getAttribute("style")).toBeNull();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("removes ping, which POSTs to a remote origin on click", () => {
|
|
136
|
+
const container = document.createElement("div");
|
|
137
|
+
container.appendChild(
|
|
138
|
+
sanitize3dsMarkup(
|
|
139
|
+
`<a id="beacon" href="https://acs.example.com/x" ping="https://attacker.example/steal"></a>`,
|
|
140
|
+
),
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
// https says nothing about a beacon: the attacker's collector is https too. The
|
|
144
|
+
// attribute goes rather than being scheme-checked.
|
|
145
|
+
expect(container.querySelector("#beacon")!.getAttribute("ping")).toBeNull();
|
|
146
|
+
expect(container.querySelector("#beacon")!.getAttribute("href")).toBe(
|
|
147
|
+
"https://acs.example.com/x",
|
|
148
|
+
);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it("removes srcset, whose candidate list no scheme check can judge", () => {
|
|
152
|
+
const container = document.createElement("div");
|
|
153
|
+
container.appendChild(
|
|
154
|
+
sanitize3dsMarkup(
|
|
155
|
+
`<img id="candidates" srcset="https://acs.example.com/a.png 1x, http://attacker.example/b.png 2x" />`,
|
|
156
|
+
),
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
// Resolved whole, this string parses as one https path and would pass a scheme
|
|
160
|
+
// check while the http candidate inside it still loads.
|
|
161
|
+
expect(container.querySelector("#candidates")!.getAttribute("srcset")).toBeNull();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("scheme-checks background, which is a single URL", () => {
|
|
165
|
+
const container = document.createElement("div");
|
|
166
|
+
container.appendChild(
|
|
167
|
+
sanitize3dsMarkup(
|
|
168
|
+
`<table id="insecure" background="http://attacker.example/bg.png"></table>` +
|
|
169
|
+
`<table id="secure" background="https://acs.example.com/bg.png"></table>`,
|
|
170
|
+
),
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
expect(container.querySelector("#insecure")!.getAttribute("background")).toBeNull();
|
|
174
|
+
expect(container.querySelector("#secure")!.getAttribute("background")).toBe(
|
|
175
|
+
"https://acs.example.com/bg.png",
|
|
176
|
+
);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it("drops non-https URLs whatever case the attribute was written in", () => {
|
|
180
|
+
const container = document.createElement("div");
|
|
181
|
+
container.appendChild(sanitize3dsMarkup(`<a id="js" HREF="javascript:stealCard()"></a>`));
|
|
182
|
+
|
|
183
|
+
expect(container.querySelector("#js")!.getAttribute("href")).toBeNull();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
describe("ThreeDSHandler.loadIframe", () => {
|
|
188
|
+
afterEach(() => {
|
|
189
|
+
document.body.innerHTML = "";
|
|
190
|
+
localStorage.clear();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("renders the 3DS Method form and resolves once its target iframe loads", async () => {
|
|
194
|
+
const handler = buildHandler(THREE_DS_METHOD_MARKUP);
|
|
195
|
+
|
|
196
|
+
const loading = handler.loadIframe();
|
|
197
|
+
const target = document.getElementById("tdsMmethodTgtFrame");
|
|
198
|
+
|
|
199
|
+
expect(target).not.toBeNull();
|
|
200
|
+
expect(document.getElementById("tdsMmethodForm")).not.toBeNull();
|
|
201
|
+
|
|
202
|
+
target!.dispatchEvent(new Event("load"));
|
|
203
|
+
await expect(loading).resolves.toBe(true);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("does not carry attacker-controlled event handlers from the payload into the page", () => {
|
|
207
|
+
const handler = buildHandler(
|
|
208
|
+
`<img id="payload-img" src="https://acs.example.com/x.png" onerror="window.__tonderXss = true" />` +
|
|
209
|
+
THREE_DS_METHOD_MARKUP,
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
handler.loadIframe();
|
|
213
|
+
|
|
214
|
+
const injected = document.getElementById("payload-img")!;
|
|
215
|
+
expect(injected.getAttribute("onerror")).toBeNull();
|
|
216
|
+
// jsdom does not fetch images, so assert the handler itself was never registered.
|
|
217
|
+
expect((injected as HTMLElement & { onerror?: unknown }).onerror).toBeNull();
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("does not carry a namespaced script from the payload into the merchant document", () => {
|
|
221
|
+
const handler = buildHandler(
|
|
222
|
+
`<svg><script id="payload-svg-script">window.__tonderXss = true</script></svg>` +
|
|
223
|
+
THREE_DS_METHOD_MARKUP,
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
handler.loadIframe();
|
|
227
|
+
|
|
228
|
+
// The SDK inserts the fragment into the live document, where a script that was
|
|
229
|
+
// never marked already-started runs on insertion. Removal is the only protection.
|
|
230
|
+
expect(document.getElementById("payload-svg-script")).toBeNull();
|
|
231
|
+
expect(document.querySelector("svg")).toBeNull();
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("hides the container it inserts, so the step renders nothing either way", () => {
|
|
235
|
+
const handler = buildHandler(THREE_DS_METHOD_MARKUP);
|
|
236
|
+
|
|
237
|
+
handler.loadIframe();
|
|
238
|
+
|
|
239
|
+
const target = document.getElementById("tdsMmethodTgtFrame")!;
|
|
240
|
+
// The ACS markup asked for display: none and the sanitizer took the attribute away.
|
|
241
|
+
// The step stays invisible because the SDK hides its own container, not because the
|
|
242
|
+
// payload was polite.
|
|
243
|
+
expect(target.getAttribute("style")).toBeNull();
|
|
244
|
+
// display is not inherited, so the child still computes its own value; the container
|
|
245
|
+
// being none is what takes the whole subtree out of rendering.
|
|
246
|
+
expect(getComputedStyle(target.parentElement!).display).toBe("none");
|
|
247
|
+
});
|
|
248
|
+
});
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { LiteCheckout } from "../src/classes/liteCheckout";
|
|
2
|
+
import { AppError } from "../src/shared/utils/appError";
|
|
3
|
+
import { ErrorKeyEnum } from "../src/shared/enum/ErrorKeyEnum";
|
|
4
|
+
|
|
5
|
+
const VALID_TOKEN =
|
|
6
|
+
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDAwMDAwMDB9.Kx-3vQ0m9R1s2T4u6W8y";
|
|
7
|
+
|
|
8
|
+
function buildCheckout(): LiteCheckout {
|
|
9
|
+
return new LiteCheckout({ mode: "stage", apiKey: "public-api-key" });
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function configure(checkout: LiteCheckout, secureToken: unknown) {
|
|
13
|
+
checkout.configureCheckout({
|
|
14
|
+
customer: { email: "buyer@example.com" },
|
|
15
|
+
secureToken,
|
|
16
|
+
} as any);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe("secure token trust boundary", () => {
|
|
20
|
+
it("accepts a well-formed token", () => {
|
|
21
|
+
const checkout = buildCheckout();
|
|
22
|
+
|
|
23
|
+
configure(checkout, VALID_TOKEN);
|
|
24
|
+
|
|
25
|
+
expect(checkout.secureToken).toBe(VALID_TOKEN);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it.each([
|
|
29
|
+
["a header-splitting value", "good-token\r\nX-Injected: 1"],
|
|
30
|
+
["an embedded newline", "good-token\ninjected"],
|
|
31
|
+
["surrounding whitespace", " good-token "],
|
|
32
|
+
["a null byte", "good-token\u0000"],
|
|
33
|
+
["a non-ASCII character", "good-tokén"],
|
|
34
|
+
["an empty string", ""],
|
|
35
|
+
["a missing value", undefined],
|
|
36
|
+
["a non-string value", { access: VALID_TOKEN }],
|
|
37
|
+
])("rejects %s", (_label, token) => {
|
|
38
|
+
const checkout = buildCheckout();
|
|
39
|
+
|
|
40
|
+
expect(() => configure(checkout, token)).toThrow(AppError);
|
|
41
|
+
try {
|
|
42
|
+
configure(checkout, token);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
expect((error as AppError).code).toBe(ErrorKeyEnum.SECURE_TOKEN_INVALID);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("leaves the previously accepted token in place when a bad one is rejected", () => {
|
|
49
|
+
const checkout = buildCheckout();
|
|
50
|
+
configure(checkout, VALID_TOKEN);
|
|
51
|
+
|
|
52
|
+
expect(() => configure(checkout, "poisoned\r\nX-Injected: 1")).toThrow(AppError);
|
|
53
|
+
|
|
54
|
+
expect(checkout.secureToken).toBe(VALID_TOKEN);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("never lets a rejected token reach the Authorization header", async () => {
|
|
58
|
+
const checkout = buildCheckout();
|
|
59
|
+
configure(checkout, VALID_TOKEN);
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
configure(checkout, "poisoned\r\nX-Injected: 1");
|
|
63
|
+
} catch {
|
|
64
|
+
// Rejected at the boundary; the request below must still carry the good token.
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const fetchMock = jest.fn().mockResolvedValue({
|
|
68
|
+
ok: true,
|
|
69
|
+
status: 200,
|
|
70
|
+
json: async () => ({ user_id: 1, cards: [] }),
|
|
71
|
+
});
|
|
72
|
+
(global as any).fetch = fetchMock;
|
|
73
|
+
|
|
74
|
+
await checkout._getCustomerCards("customer-token", 42);
|
|
75
|
+
|
|
76
|
+
const headers = fetchMock.mock.calls[0][1].headers;
|
|
77
|
+
expect(headers.Authorization).toBe(`Bearer ${VALID_TOKEN}`);
|
|
78
|
+
expect(headers.Authorization).not.toContain("\r");
|
|
79
|
+
expect(headers.Authorization).not.toContain("\n");
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { LiteCheckout } from "../src/classes/liteCheckout";
|
|
2
|
+
|
|
3
|
+
const STORAGE_KEY = "verify_transaction_status_url";
|
|
4
|
+
const VERIFY_PATH = "/api/v1/tonder/ksk/business/7/payments/55/transactions/9";
|
|
5
|
+
|
|
6
|
+
describe("3DS verify credential", () => {
|
|
7
|
+
let fetchMock: jest.Mock;
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
localStorage.setItem(
|
|
11
|
+
STORAGE_KEY,
|
|
12
|
+
JSON.stringify({ url: VERIFY_PATH, expires: Date.now() + 60_000 }),
|
|
13
|
+
);
|
|
14
|
+
fetchMock = jest.fn().mockResolvedValue({
|
|
15
|
+
status: 200,
|
|
16
|
+
json: async () => ({ status: "Success" }),
|
|
17
|
+
});
|
|
18
|
+
(global as any).fetch = fetchMock;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterEach(() => localStorage.clear());
|
|
22
|
+
|
|
23
|
+
it("uses apiKey when the merchant passes it", async () => {
|
|
24
|
+
const checkout = new LiteCheckout({ mode: "stage", apiKey: "the-api-key" });
|
|
25
|
+
|
|
26
|
+
await checkout.process3ds.verifyTransactionStatus();
|
|
27
|
+
|
|
28
|
+
expect(fetchMock.mock.calls[0][1].headers.Authorization).toBe("Token the-api-key");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("uses the deprecated apiKeyTonder when it is the only key passed", async () => {
|
|
32
|
+
const checkout = new LiteCheckout({
|
|
33
|
+
mode: "stage",
|
|
34
|
+
apiKeyTonder: "the-deprecated-key",
|
|
35
|
+
} as any);
|
|
36
|
+
|
|
37
|
+
await checkout.process3ds.verifyTransactionStatus();
|
|
38
|
+
|
|
39
|
+
expect(fetchMock.mock.calls[0][1].headers.Authorization).toBe("Token the-deprecated-key");
|
|
40
|
+
});
|
|
41
|
+
});
|