summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/payment-request/payment-request-constructor-thcrash.https.html
blob: b6003070851459a5c72218951fe47251cfec9dc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="timeout" content="long">
<title>Crash tests PaymentRequest Constructor</title>
<link rel="help" href="https://w3c.github.io/browser-payment-api/#constructor">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

"use strict";
const ABUSIVE_AMOUNT = 100000;

const applePay = {
  supportedMethods: "https://apple.com/apple-pay",
  data: {
    version: 3,
    merchantIdentifier: "merchant.com.example",
    countryCode: "US",
    merchantCapabilities: ["supports3DS"],
    supportedNetworks: ["visa"],
  }
};

const basicCard = Object.freeze({
  supportedMethods: "basic-card",
});

const defaultAmount = Object.freeze({
  currency: "USD",
  value: "1.00",
});

const evilAmount = Object.freeze({
  currency: "USD",
  value: "1".repeat(ABUSIVE_AMOUNT),
});

const defaultMethods = Object.freeze([basicCard, applePay]);

const defaultTotal = Object.freeze({
  label: "label",
  amount: defaultAmount,
});

const evilTotal = Object.freeze({
  label: "a".repeat(ABUSIVE_AMOUNT),
  amount: evilAmount,
});

const defaultDetails = Object.freeze({
  total: defaultTotal,
  get id() {
    return Math.random();
  },
});

const defaultPaymentItem = Object.freeze({
  label: "label",
  amount: defaultAmount,
});

const defaultShippingOption = {
  get id() {
    return "shipping option " + Math.random();
  },
  amount: defaultAmount,
  label: "shipping option label",
};

// First argument is sequence<PaymentMethodData> methodData
test(() => {
  let evilMethods = [Object.assign({}, basicCard)];
  // smoke test
  try {
    new PaymentRequest(evilMethods, defaultDetails);
  } catch (err) {
    assert_unreached("failed smoke test: " + err.stack);
  }
  // Now, let's add an abusive amount of methods.
  while (evilMethods.length < ABUSIVE_AMOUNT) {
    evilMethods.push({supportedMethods: "evil-method" + evilMethods.length});
  }
  try {
    new PaymentRequest(evilMethods, defaultDetails);
  } catch (err) {
    assert_equals(err.name, "TypeError", "must be a TypeError");
  }
}, "Don't crash if there is an abusive number of payment methods in the methodData sequence");

// PaymentMethodData.supportedMethods
test(() => {
  const supportedMethods = "basic-card";
  // Smoke test
  try {
    new PaymentRequest([{ supportedMethods }], defaultDetails);
  } catch (err) {
    assert_unreached("failed smoke test: " + err.stack);
  }
  // Now, we make supportedMethods super large
  const evilMethodData = [
    {
      supportedMethods: supportedMethods.repeat(ABUSIVE_AMOUNT),
    },
  ];
  try {
    new PaymentRequest(evilMethodData, defaultDetails);
  } catch (err) {
    assert_equals(err.name, "TypeError", "must be a TypeError");
  }
}, "Don't crash if PaymentMethodData.supportedMethods is an abusive length");

// PaymentDetailsInit.id
test(() => {
  const id = "abc";
  // Smoke Test
  try {
    new PaymentRequest(
      defaultMethods,
      Object.assign({}, defaultDetails, { id })
    );
  } catch (err) {
    assert_unreached("failed smoke test: " + err.stack);
  }
  // Now, we make the id super large;
  const evilDetails = Object.assign({}, defaultDetails, {
    id: id.repeat(ABUSIVE_AMOUNT),
  });
  try {
    new PaymentRequest(defaultMethods, evilDetails);
  } catch (err) {
    assert_equals(err.name, "TypeError", "must be a TypeError");
  }
}, "Don't crash if the request id has an abusive length");

// PaymentDetailsInit.total.label
test(() => {
  const evilDetails = Object.assign({}, defaultDetails);
  // Smoke Test
  try {
    new PaymentRequest(defaultMethods, evilDetails);
  } catch (err) {
    assert_unreached("failed smoke test: " + err.stack);
  }
  // Now, we make the label super large;
  evilDetails.total = {
    label: "l".repeat(ABUSIVE_AMOUNT),
    amount: defaultAmount,
  };
  try {
    new PaymentRequest(defaultMethods, evilDetails);
  } catch (err) {
    assert_equals(err.name, "TypeError", "must be a TypeError");
  }
}, "Don't crash if PaymentDetailsInit.total.label is an abusive length");

test(() => {
  const evilDetails = Object.assign({}, defaultDetails);
  // Smoke Test
  try {
    new PaymentRequest(defaultMethods, evilDetails);
  } catch (err) {
    assert_unreached("failed smoke test: " + err.stack);
  }
  // Now, we can use evilAmount
  evilDetails.total = evilAmount;
  try {
    new PaymentRequest(defaultMethods, evilDetails);
  } catch (err) {
    assert_equals(err.name, "TypeError", "must be a TypeError");
  }
}, "Don't crash if total.amount.value is an abusive length");

for (const [prop, defaultValue] of [
  ["displayItems", defaultPaymentItem],
  ["shippingOptions", defaultShippingOption],
]) {
  test(() => {
    const evilDetails = Object.assign({}, defaultDetails);
    evilDetails[prop] = [defaultValue];
    // Smoke Test
    try {
      new PaymentRequest(defaultMethods, evilDetails);
    } catch (err) {
      assert_unreached("failed smoke test: " + err.stack);
    }
    while (evilDetails[prop].length < ABUSIVE_AMOUNT) {
      evilDetails[prop] = evilDetails[prop].concat(evilDetails[prop]);
    }
    // Now, construct with evil items!
    try {
      new PaymentRequest(defaultMethods, evilDetails);
    } catch (err) {
      assert_equals(err.name, "TypeError", "must be a TypeError");
    }
  }, `Don't crash if details.${prop} has an abusive number of items`);
}

test(() => {
  const evilDetails = Object.assign({}, defaultDetails);
  const evilShippingOption = Object.assign({}, defaultShippingOption);
  evilDetails.shippingOptions = [evilShippingOption];
  // Smoke Test
  try {
    new PaymentRequest(defaultMethods, evilDetails);
  } catch (err) {
    assert_unreached("failed smoke test: " + err.stack);
  }
  // Now, we make the label super large;
  evilShippingOption.label = "l".repeat(ABUSIVE_AMOUNT);
  try {
    new PaymentRequest(defaultMethods, evilDetails);
  } catch (err) {
    assert_equals(err.name, "TypeError", "must be a TypeError");
  }
}, "Don't crash if PaymentShippingOptions.label is an abusive length");

test(() => {
  const evilDetails = Object.assign({}, defaultDetails);
  const evilShippingOption = Object.assign({}, defaultShippingOption);
  evilDetails.shippingOptions = [evilShippingOption];
  // Smoke Test
  try {
    new PaymentRequest(defaultMethods, evilDetails);
  } catch (err) {
    assert_unreached("failed smoke test: " + err.stack);
  }
  // Now, we make use of evilAmount;
  evilShippingOption.amount = evilAmount;
  try {
    new PaymentRequest(defaultMethods, evilDetails);
  } catch (err) {
    assert_equals(err.name, "TypeError", "must be a TypeError");
  }
}, "Don't crash if the PaymentShippingOptions.amount.value is an abusive length");

test(() => {
  const evilDetails = Object.assign({}, defaultDetails);
  const evilDisplayItem = Object.assign({}, defaultPaymentItem);
  evilDetails.displayItems = [evilDisplayItem];
  // Smoke Test
  try {
    new PaymentRequest(defaultMethods, evilDetails);
  } catch (err) {
    assert_unreached("failed smoke test: " + err.stack);
  }
  // Now, we make the label super large;
  evilDisplayItem.label = "l".repeat(ABUSIVE_AMOUNT);
  try {
    new PaymentRequest(defaultMethods, evilDetails);
  } catch (err) {
    assert_equals(err.name, "TypeError", "must be a TypeError");
  }
}, "Don't crash if PaymentItem.label is an abusive length");
</script>