dce-reactkit 3.9.2 → 3.9.3

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.
@@ -9,6 +9,12 @@ import LogFunction from '../types/LogFunction';
9
9
  * @param opts.handler function that processes the request
10
10
  * @param [opts.skipSessionCheck] if true, skip the session check (allow users
11
11
  * to not be logged in and launched via LTI)
12
+ * @param [opts.allowedHosts] if included, only allow requests from these hosts
13
+ * (start a hostname with a "*" to only check the end of the hostname)
14
+ * you can include just one string instead of an array
15
+ * @param [opts.bannedHosts] if included, do not allow requests from these hosts
16
+ * (start a hostname with a "*" to only check the end of the hostname)
17
+ * you can include just one string instead of an array
12
18
  * @param [opts.unhandledErrorMessagePrefix] if included, when an error that
13
19
  * is not of type ErrorWithCode is thrown, the client will receive an error
14
20
  * where the error message is prefixed with this string. For example,
@@ -63,6 +69,8 @@ declare const genRouteHandler: (opts: {
63
69
  logServerEvent: LogFunction;
64
70
  }) => any;
65
71
  skipSessionCheck?: boolean | undefined;
72
+ allowedHosts?: string | string[] | undefined;
73
+ bannedHosts?: string | string[] | undefined;
66
74
  unhandledErrorMessagePrefix?: string | undefined;
67
75
  }) => (req: any, res: any, next: () => void) => Promise<undefined>;
68
76
  export default genRouteHandler;
@@ -8,6 +8,8 @@ declare enum ReactKitErrorCode {
8
8
  SessionExpired = "DRK3",
9
9
  MissingParameter = "DRK4",
10
10
  InvalidParameter = "DRK5",
11
+ HostNotAllowed = "DRK17",
12
+ HostBanned = "DRK18",
11
13
  WrongCourse = "DRK6",
12
14
  NoCACCLSendRequestFunction = "DRK7",
13
15
  NoCACCLGetLaunchInfoFunction = "DRK8",
package/dist/esm/index.js CHANGED
@@ -31,7 +31,7 @@ function __awaiter(thisArg, _arguments, P, generator) {
31
31
  });
32
32
  }
33
33
 
34
- // Highest error code = DRK16
34
+ // Highest error code = DRK18
35
35
  /**
36
36
  * List of error codes built into the react kit
37
37
  * @author Gabe Abrams
@@ -43,6 +43,8 @@ var ReactKitErrorCode;
43
43
  ReactKitErrorCode["SessionExpired"] = "DRK3";
44
44
  ReactKitErrorCode["MissingParameter"] = "DRK4";
45
45
  ReactKitErrorCode["InvalidParameter"] = "DRK5";
46
+ ReactKitErrorCode["HostNotAllowed"] = "DRK17";
47
+ ReactKitErrorCode["HostBanned"] = "DRK18";
46
48
  ReactKitErrorCode["WrongCourse"] = "DRK6";
47
49
  ReactKitErrorCode["NoCACCLSendRequestFunction"] = "DRK7";
48
50
  ReactKitErrorCode["NoCACCLGetLaunchInfoFunction"] = "DRK8";
@@ -14441,6 +14443,12 @@ const parseUserAgent = (userAgent) => {
14441
14443
  * @param opts.handler function that processes the request
14442
14444
  * @param [opts.skipSessionCheck] if true, skip the session check (allow users
14443
14445
  * to not be logged in and launched via LTI)
14446
+ * @param [opts.allowedHosts] if included, only allow requests from these hosts
14447
+ * (start a hostname with a "*" to only check the end of the hostname)
14448
+ * you can include just one string instead of an array
14449
+ * @param [opts.bannedHosts] if included, do not allow requests from these hosts
14450
+ * (start a hostname with a "*" to only check the end of the hostname)
14451
+ * you can include just one string instead of an array
14444
14452
  * @param [opts.unhandledErrorMessagePrefix] if included, when an error that
14445
14453
  * is not of type ErrorWithCode is thrown, the client will receive an error
14446
14454
  * where the error message is prefixed with this string. For example,
@@ -14472,6 +14480,72 @@ const genRouteHandler = (opts) => {
14472
14480
  // Output params
14473
14481
  const output = {};
14474
14482
  /*----------------------------------------*/
14483
+ /* ----------- Hostname Check ----------- */
14484
+ /*----------------------------------------*/
14485
+ // Get hostnames
14486
+ const originURL = String(req.get('origin')
14487
+ || req.headers.origin
14488
+ || req.headers.referer);
14489
+ const originHostname = (originURL
14490
+ // Remove protocol
14491
+ .replace(/(^\w+:|^)\/\//, '')
14492
+ // Remove port
14493
+ .replace(/:\d+$/, ''));
14494
+ const serverHostname = String(req.hostname);
14495
+ // Check allowed
14496
+ if (opts.allowedHosts) {
14497
+ // Only accept requests from allowed hosts
14498
+ const allowedArray = (Array.isArray(opts.allowedHosts)
14499
+ ? opts.allowedHosts
14500
+ : [opts.allowedHosts]);
14501
+ // Check if server is localhost
14502
+ if (serverHostname === 'localhost') {
14503
+ // Allow localhost
14504
+ allowedArray.push('localhost');
14505
+ }
14506
+ // Check if current host is allowed
14507
+ const allowed = allowedArray.some((allowedHost) => {
14508
+ if (allowedHost.startsWith('*')) {
14509
+ // Check end of hostname
14510
+ return originHostname.endsWith(allowedHost.substring(1));
14511
+ }
14512
+ // Check full hostname
14513
+ return originHostname.toLowerCase() === allowedHost.toLowerCase();
14514
+ });
14515
+ // If not allowed, return error
14516
+ if (!allowed) {
14517
+ return handleError(res, {
14518
+ message: 'You are not allowed to access this endpoint.',
14519
+ code: ReactKitErrorCode$1.HostNotAllowed,
14520
+ status: 403,
14521
+ });
14522
+ }
14523
+ }
14524
+ // Check banned
14525
+ if (opts.bannedHosts) {
14526
+ // Do not allow requests from banned hosts
14527
+ const bannedArray = (Array.isArray(opts.bannedHosts)
14528
+ ? opts.bannedHosts
14529
+ : [opts.bannedHosts]);
14530
+ // Check if current host is banned
14531
+ const banned = bannedArray.some((bannedHost) => {
14532
+ if (bannedHost.startsWith('*')) {
14533
+ // Check end of hostname
14534
+ return originHostname.endsWith(bannedHost.substring(1));
14535
+ }
14536
+ // Check full hostname
14537
+ return originHostname.toLowerCase() === bannedHost.toLowerCase();
14538
+ });
14539
+ // If banned, return error
14540
+ if (banned) {
14541
+ return handleError(res, {
14542
+ message: 'You are not allowed to access this endpoint.',
14543
+ code: ReactKitErrorCode$1.HostBanned,
14544
+ status: 403,
14545
+ });
14546
+ }
14547
+ }
14548
+ /*----------------------------------------*/
14475
14549
  /* ------------ Parse Params ------------ */
14476
14550
  /*----------------------------------------*/
14477
14551
  // Process items one by one