-
Notifications
You must be signed in to change notification settings - Fork 212
/
validators.js
595 lines (528 loc) · 19.4 KB
/
validators.js
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/* eslint-disable no-useless-escape,no-control-regex */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict';
const { URL } = require('url');
const punycode = require('punycode.js');
const isA = require('@hapi/joi');
// Match any non-empty hex-encoded string.
const HEX_STRING = /^(?:[a-fA-F0-9]{2})+$/;
module.exports.HEX_STRING = HEX_STRING;
module.exports.BASE_36 = /^[a-zA-Z0-9]*$/;
// RFC 4648, section 5
module.exports.URL_SAFE_BASE_64 = /^[A-Za-z0-9_-]+$/;
// RFC 7636, section 4.1
module.exports.PKCE_CODE_VERIFIER = /^[A-Za-z0-9-\._~]{43,128}$/;
// Crude phone number validation. The handler code does it more thoroughly.
exports.E164_NUMBER = /^\+[1-9]\d{1,14}$/;
exports.DIGITS = /^[0-9]+$/;
exports.DEVICE_COMMAND_NAME = /^[a-zA-Z0-9._\/\-:]{1,100}$/;
exports.IP_ADDRESS = isA.string().ip();
// Match display-safe unicode characters.
// We're pretty liberal with what's allowed in a unicode string,
// but we exclude the following classes of characters:
//
// \u0000-\u001F - C0 (ascii) control characters
// \u007F - ascii DEL character
// \u0080-\u009F - C1 (ansi escape) control characters
// \u2028-\u2029 - unicode line/paragraph separator
// \uD800-\uDFFF - non-BMP surrogate pairs
// \uE000-\uF8FF - BMP private use area
// \uFFF9-\uFFFC - unicode specials prior to the replacement character
// \uFFFE-\uFFFF - unicode this-is-not-a-character specials
//
// Note that the unicode replacement character \uFFFD is explicitly allowed,
// and clients may use it to replace other disallowed characters.
//
// We might tweak this list in future.
const DISPLAY_SAFE_UNICODE =
/^(?:[^\u0000-\u001F\u007F\u0080-\u009F\u2028-\u2029\uD800-\uDFFF\uE000-\uF8FF\uFFF9-\uFFFC\uFFFE-\uFFFF])*$/;
module.exports.DISPLAY_SAFE_UNICODE = DISPLAY_SAFE_UNICODE;
// Similar display-safe match but includes non-BMP characters
const DISPLAY_SAFE_UNICODE_WITH_NON_BMP =
/^(?:[^\u0000-\u001F\u007F\u0080-\u009F\u2028-\u2029\uE000-\uF8FF\uFFF9-\uFFFC\uFFFE-\uFFFF])*$/;
module.exports.DISPLAY_SAFE_UNICODE_WITH_NON_BMP =
DISPLAY_SAFE_UNICODE_WITH_NON_BMP;
// Bearer auth header regex
const BEARER_AUTH_REGEX = /^Bearer\s+([a-z0-9+\/]+)$/i;
module.exports.BEARER_AUTH_REGEX = BEARER_AUTH_REGEX;
// Joi validator to match any valid email address.
// This is different to Joi's builtin email validator, and
// requires a custom validation function.
// The custom validators below need to either return the value
// or create an error object using `createError`.
// see examples here: https://github.com/hapijs/joi/blob/master/lib/string.js
module.exports.email = function () {
const email = isA.string().max(255).regex(DISPLAY_SAFE_UNICODE);
// Imma add a custom test to this Joi object using internal
// properties because I can't find a nice API to do that.
email._tests.push({
func: function (value, state, options) {
if (value !== undefined && value !== null) {
if (module.exports.isValidEmailAddress(value)) {
return value;
}
}
return email.createError('string.base', { value }, state, options);
},
});
return email;
};
module.exports.service = isA
.string()
.max(16)
.regex(/^[a-zA-Z0-9\-]*$/);
module.exports.hexString = isA.string().regex(HEX_STRING);
module.exports.clientId = module.exports.hexString.length(16);
module.exports.clientSecret = module.exports.hexString;
module.exports.idToken = module.exports.jwt;
module.exports.refreshToken = module.exports.hexString.length(64);
module.exports.sessionToken = module.exports.hexString.length(64);
module.exports.sessionTokenId = module.exports.hexString.length(64);
module.exports.authorizationCode = module.exports.hexString.length(64);
// Note that the empty string is a valid scope value (meaning "no permissions").
const scope = isA
.string()
.max(256)
.regex(/^[a-zA-Z0-9 _\/.:-]*$/)
.allow('');
module.exports.scope = scope;
module.exports.assertion = isA
.string()
.min(50)
.max(10240)
.regex(/^[a-zA-Z0-9_\-\.~=]+$/);
module.exports.pkceCodeChallengeMethod = isA.string().valid('S256');
module.exports.pkceCodeChallenge = isA
.string()
.length(43)
.regex(module.exports.URL_SAFE_BASE_64);
module.exports.pkceCodeVerifier = isA
.string()
.min(43)
.max(128)
.regex(module.exports.PKCE_CODE_VERIFIER);
module.exports.jwe = isA
.string()
.max(1024)
// JWE token format: 'protectedheader.encryptedkey.iv.cyphertext.authenticationtag'
.regex(
/^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/
);
module.exports.jwt = isA
.string()
.max(1024)
// JWT format: 'header.payload.signature'
.regex(/^([a-zA-Z0-9\-_]+)\.([a-zA-Z0-9\-_]+)\.([a-zA-Z0-9\-_]+)$/);
module.exports.accessToken = isA
.alternatives()
.try([module.exports.hexString.length(64), module.exports.jwt]);
// Function to validate an email address.
//
// Uses regexes based on the ones in fxa-email-service, tweaked slightly
// because Node's support for unicode regexes is hidden behind a harmony
// flag. As soon as we have default support for unicode regexes, we should
// be able to just use the regex from there directly (and ditch the punycode
// transformation).
//
// https://github.com/mozilla/fxa-email-service/blob/6fc6c31043598b246102cd1fdd27fc325f4514fb/src/validate/mod.rs#L28-L30
const EMAIL_USER = /^[A-Z0-9.!#$%&'*+\/=?^_`{|}~-]{1,64}$/i;
const EMAIL_DOMAIN =
/^[A-Z0-9](?:[A-Z0-9-]{0,253}[A-Z0-9])?(?:\.[A-Z0-9](?:[A-Z0-9-]{0,253}[A-Z0-9])?)+$/i;
module.exports.isValidEmailAddress = function (value) {
if (!value) {
return false;
}
const parts = value.split('@');
if (parts.length !== 2 || parts[1].length > 255) {
return false;
}
if (!EMAIL_USER.test(punycode.toASCII(parts[0]))) {
return false;
}
if (!EMAIL_DOMAIN.test(punycode.toASCII(parts[1]))) {
return false;
}
return true;
};
module.exports.redirectTo = function redirectTo(base) {
const validator = isA.string().max(512);
let hostnameRegex = null;
if (base) {
hostnameRegex = new RegExp(`(?:\\.|^)${base.replace('.', '\\.')}$`);
}
validator._tests.push({
func: (value, state, options) => {
if (value !== undefined && value !== null) {
if (isValidUrl(value, hostnameRegex)) {
return value;
}
}
return validator.createError('string.base', { value }, state, options);
},
});
return validator;
};
module.exports.url = function url(options) {
const validator = isA.string().uri(options);
validator._tests.push({
func: (value, state, options) => {
if (value !== undefined && value !== null) {
if (isValidUrl(value)) {
return value;
}
}
return validator.createError('string.base', { value }, state, options);
},
});
return validator;
};
// resourceUrls must *not* contain a hash fragment.
// See https://tools.ietf.org/html/draft-ietf-oauth-resource-indicators-02#section-2
module.exports.resourceUrl = module.exports.url().regex(/#/, { invert: true });
module.exports.pushCallbackUrl = function pushUrl(options) {
const validator = isA.string().uri(options);
validator._tests.push({
func: (value, state, options) => {
if (value !== undefined && value !== null) {
let normalizedValue = value;
// Fx Desktop registers https push urls with a :443 which causes `isValidUrl`
// to fail because the :443 is expected to have been normalized away.
if (/^https:\/\/[a-zA-Z0-9._-]+(:443)($|\/)/.test(value)) {
normalizedValue = value.replace(':443', '');
}
if (isValidUrl(normalizedValue)) {
return value;
}
}
return validator.createError('string.base', { value }, state, options);
},
});
return validator;
};
function isValidUrl(url, hostnameRegex) {
let parsed;
try {
parsed = new URL(url);
} catch (err) {
return false;
}
if (hostnameRegex && !hostnameRegex.test(parsed.hostname)) {
return false;
}
if (!/^https?:$/.test(parsed.protocol)) {
return false;
}
// Reject anything that won't round-trip unambiguously
// through a parse. This puts the onus on the requestor
// to e.g. escape special characters, normalize ports, etc.
// The only trick here is that `new URL()` will add a trailing
// slash if there's no path component, which is why we also
// compare to `origin` below.
if (parsed.href !== url && parsed.origin !== url) {
return false;
}
return parsed.href;
}
module.exports.verificationMethod = isA.string().valid([
'email', // Verification by email link
'email-otp', // Verification by email otp code using account long code (`emailCode`) as secret
'email-2fa', // Verification by email code using randomly generated code (used in login flow)
'email-captcha', // Verification by email code using randomly generated code (used in unblock flow)
'totp-2fa', // Verification by TOTP authenticator device code, secret is randomly generated
]);
module.exports.authPW = isA.string().length(64).regex(HEX_STRING).required();
module.exports.wrapKb = isA.string().length(64).regex(HEX_STRING);
module.exports.recoveryKeyId = isA.string().regex(HEX_STRING).max(32);
module.exports.recoveryData = isA
.string()
.regex(/[a-zA-Z0-9.]/)
.max(1024)
.required();
module.exports.stripePaymentMethodId = isA.string().max(30);
module.exports.paypalPaymentToken = isA.string().max(30);
module.exports.subscriptionsSubscriptionId = isA.string().max(255);
module.exports.subscriptionsPlanId = isA.string().max(255);
module.exports.subscriptionsProductId = isA.string().max(255);
module.exports.subscriptionsProductName = isA.string().max(255);
module.exports.subscriptionsPaymentToken = isA.string().max(255);
module.exports.subscriptionPaymentCountryCode = isA
.string()
.length(2)
.allow(null);
// This is fxa-auth-db-mysql's perspective on an active subscription
module.exports.activeSubscriptionValidator = isA.object({
uid: isA.string().required(),
subscriptionId: module.exports.subscriptionsSubscriptionId.required(),
productId: module.exports.subscriptionsProductId.required(),
createdAt: isA.number().required(),
cancelledAt: isA.alternatives(isA.number(), isA.any().allow(null)),
});
module.exports.subscriptionsSetupIntent = isA
.object({
client_secret: isA.string().required(),
})
.unknown(true);
// This is a Stripe subscription object with latest_invoice.payment_intent expanded
module.exports.subscriptionsSubscriptionExpandedValidator = isA
.object({
id: isA.string().required(),
object: isA.string().allow('subscription').required(),
latest_invoice: isA
.object({
id: isA.string().required(),
object: isA.string().allow('invoice').required(),
payment_intent: isA
.object({
id: isA.string().required(),
object: isA.string().allow('payment_intent').required(),
client_secret: isA.string().required(),
})
.unknown(true)
.required(),
})
.unknown(true)
.required(),
})
.unknown(true);
module.exports.subscriptionsInvoicePIExpandedValidator = isA
.object({
id: isA.string().required(),
object: isA.string().allow('invoice').required(),
payment_intent: isA
.object({
id: isA.string().required(),
object: isA.string().allow('payment_intent').required(),
client_secret: isA.string().required(),
})
.unknown(true)
.required(),
})
.unknown(true);
// This is subhub's perspective on an active subscription
module.exports.subscriptionsSubscriptionValidator = isA.object({
created: isA.number().required(),
current_period_end: isA.number().required(),
current_period_start: isA.number().required(),
cancel_at_period_end: isA.boolean().required(),
end_at: isA.alternatives(isA.number(), isA.any().allow(null)),
failure_code: isA.string().optional(),
failure_message: isA.string().optional(),
latest_invoice: isA.string().required(),
plan_id: module.exports.subscriptionsPlanId.required(),
product_id: module.exports.subscriptionsProductId.required(),
product_name: isA.string().required(),
status: isA.string().required(),
subscription_id: module.exports.subscriptionsSubscriptionId.required(),
});
// This is support-panel's perspective on a subscription
module.exports.subscriptionsSubscriptionSupportValidator = isA.object({
created: isA.number().required(),
current_period_end: isA.number().required(),
current_period_start: isA.number().required(),
plan_changed: isA.alternatives(isA.number(), isA.any().allow(null)),
previous_product: isA.alternatives(isA.string(), isA.any().allow(null)),
product_name: isA.string().required(),
status: isA.string().required(),
subscription_id: module.exports.subscriptionsSubscriptionId.required(),
});
module.exports.subscriptionsSubscriptionListValidator = isA.object({
subscriptions: isA
.array()
.items(module.exports.subscriptionsSubscriptionValidator),
});
// https://mana.mozilla.org/wiki/pages/viewpage.action?spaceKey=COPS&title=SP+Tiered+Product+Support#SPTieredProductSupport-MetadataAgreements
// Trying to be a bit flexible in validation here:
// - subhub may not yet be including product / plan metadata in responses
// - metadata can contain arbitrary keys that we don't expect (e.g. used by other systems)
// - but we can make a good effort at validating what we expect to see when we see it
module.exports.subscriptionPlanMetadataValidator = isA.object().unknown(true);
const capabilitiesClientIdPattern = /^capabilities/;
const legalResourceDomainPattern =
/^https:\/\/accounts-static\.cdn\.mozilla\.net\/legal\/(.*)/;
module.exports.subscriptionProductMetadataBaseValidator = isA
.object({
webIconURL: isA.string().uri().required(),
upgradeCTA: isA.string().optional(),
downloadURL: isA.string().uri().required(),
appStoreLink: isA.string().uri().optional(),
playStoreLink: isA.string().uri().optional(),
productSet: isA.string().optional(),
productOrder: isA.number().optional(),
'product:termsOfServiceDownloadURL': isA
.string()
.regex(legalResourceDomainPattern)
.required(),
'product:termsOfServiceURL': isA.string().uri().required(),
'product:privacyNoticeDownloadURL': isA
.string()
.regex(legalResourceDomainPattern)
.optional(),
'product:privacyNoticeURL': isA.string().uri().required(),
})
.pattern(capabilitiesClientIdPattern, isA.string(), {
fallthrough: true,
})
.unknown(true);
module.exports.subscriptionProductMetadataValidator = {
validate: function (metadata) {
const hasCapability = Object.keys(metadata).some((k) =>
capabilitiesClientIdPattern.test(k)
);
if (!hasCapability) {
return {
error: 'Capability missing from metadata',
};
}
return module.exports.subscriptionProductMetadataBaseValidator.validate(
metadata
);
},
};
module.exports.subscriptionsPlanValidator = isA.object({
plan_id: module.exports.subscriptionsPlanId.required(),
plan_metadata: module.exports.subscriptionPlanMetadataValidator.optional(),
product_id: module.exports.subscriptionsProductId.required(),
product_name: isA.string().required(),
plan_name: isA.string().allow('').optional(),
product_metadata:
module.exports.subscriptionProductMetadataBaseValidator.optional(),
interval: isA.string().required(),
interval_count: isA.number().required(),
amount: isA.number().required(),
currency: isA.string().required(),
});
module.exports.subscriptionsCustomerValidator = isA.object({
billing_name: isA
.alternatives(isA.string(), isA.any().allow(null))
.optional(),
exp_month: isA.number().optional(),
exp_year: isA.number().optional(),
last4: isA.string().optional(),
payment_provider: isA.string().optional(),
payment_type: isA.string().optional(),
paypal_payment_error: isA.string().optional(),
brand: isA.string().optional(),
billing_agreement_id: isA
.alternatives(isA.string(), isA.any().allow(null))
.optional(),
subscriptions: isA
.array()
.items(module.exports.subscriptionsSubscriptionValidator)
.optional(),
});
module.exports.subscriptionsStripeIntentValidator = isA
.object({
client_secret: isA.string().optional(),
created: isA.number().required(),
payment_method: isA
.alternatives(isA.string(), isA.object({}).unknown(true))
.optional()
.allow(null),
source: isA.alternatives().when('payment_method', {
// cannot be that strict here since this validator is used in two routes
is: null,
then: isA.string().optional(),
otherwise: isA.any().optional().allow(null),
}),
status: isA.string().required(),
})
.unknown(true);
module.exports.subscriptionsStripeSourceValidator = isA
.object({
id: isA.string().required(),
object: isA.string().required(),
brand: isA.string().optional(),
exp_month: isA.string().optional(),
exp_year: isA.string().optional(),
last4: isA.string().optional(),
})
.unknown(true);
module.exports.subscriptionsStripeInvoiceValidator = isA
.object({
id: isA.string().required(),
payment_intent: isA
.alternatives(
isA.string().allow(null),
module.exports.subscriptionsStripeIntentValidator
)
.optional(),
})
.unknown(true);
module.exports.subscriptionsStripePriceValidator = isA
.object({
id: isA.string().required(),
})
.unknown(true);
module.exports.subscriptionsStripeSubscriptionItemValidator = isA
.object({
id: isA.string().required(),
created: isA.number().required(),
price: module.exports.subscriptionsStripePriceValidator.required(),
})
.unknown(true);
module.exports.subscriptionsStripeSubscriptionValidator = isA
.object({
id: isA.string().required(),
cancel_at: isA.alternatives(isA.number(), isA.any().valid(null)),
canceled_at: isA.alternatives(isA.number(), isA.any().valid(null)),
cancel_at_period_end: isA.bool().required(),
created: isA.number().required(),
current_period_end: isA.number().required(),
current_period_start: isA.number().required(),
ended_at: isA.alternatives(isA.number(), isA.any().valid(null)),
items: isA
.object({
data: isA
.array()
.items(module.exports.subscriptionsStripeSubscriptionItemValidator)
.required(),
})
.unknown(true)
.optional(),
latest_invoice: isA
.alternatives(
isA.string(),
module.exports.subscriptionsStripeInvoiceValidator
)
.optional(),
status: isA.string().required(),
})
.unknown(true);
module.exports.subscriptionsStripeCustomerValidator = isA
.object({
invoices_settings: isA
.object({
default_payment_method: isA.string().optional(),
})
.unknown(true)
.optional(),
subscriptions: isA
.object({
data: isA
.array()
.items(module.exports.subscriptionsStripeSubscriptionValidator)
.required(),
})
.unknown(true)
.optional(),
})
.unknown(true);
module.exports.ppidSeed = isA.number().integer().min(0).max(1024);
module.exports.scopes = isA.array().items(scope).default([]).optional();
module.exports.newsletters = isA
.array()
.items(
isA
.string()
.valid(
'firefox-accounts-journey',
'knowledge-is-power',
'take-action-for-the-internet',
'test-pilot',
'mozilla-and-you'
)
)
.default([])
.optional();