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.
package/dist/cjs/index.js CHANGED
@@ -59,7 +59,7 @@ function __awaiter(thisArg, _arguments, P, generator) {
59
59
  });
60
60
  }
61
61
 
62
- // Highest error code = DRK16
62
+ // Highest error code = DRK18
63
63
  /**
64
64
  * List of error codes built into the react kit
65
65
  * @author Gabe Abrams
@@ -71,6 +71,8 @@ var ReactKitErrorCode;
71
71
  ReactKitErrorCode["SessionExpired"] = "DRK3";
72
72
  ReactKitErrorCode["MissingParameter"] = "DRK4";
73
73
  ReactKitErrorCode["InvalidParameter"] = "DRK5";
74
+ ReactKitErrorCode["HostNotAllowed"] = "DRK17";
75
+ ReactKitErrorCode["HostBanned"] = "DRK18";
74
76
  ReactKitErrorCode["WrongCourse"] = "DRK6";
75
77
  ReactKitErrorCode["NoCACCLSendRequestFunction"] = "DRK7";
76
78
  ReactKitErrorCode["NoCACCLGetLaunchInfoFunction"] = "DRK8";
@@ -14469,6 +14471,12 @@ const parseUserAgent = (userAgent) => {
14469
14471
  * @param opts.handler function that processes the request
14470
14472
  * @param [opts.skipSessionCheck] if true, skip the session check (allow users
14471
14473
  * to not be logged in and launched via LTI)
14474
+ * @param [opts.allowedHosts] if included, only allow requests from these hosts
14475
+ * (start a hostname with a "*" to only check the end of the hostname)
14476
+ * you can include just one string instead of an array
14477
+ * @param [opts.bannedHosts] if included, do not allow requests from these hosts
14478
+ * (start a hostname with a "*" to only check the end of the hostname)
14479
+ * you can include just one string instead of an array
14472
14480
  * @param [opts.unhandledErrorMessagePrefix] if included, when an error that
14473
14481
  * is not of type ErrorWithCode is thrown, the client will receive an error
14474
14482
  * where the error message is prefixed with this string. For example,
@@ -14500,6 +14508,72 @@ const genRouteHandler = (opts) => {
14500
14508
  // Output params
14501
14509
  const output = {};
14502
14510
  /*----------------------------------------*/
14511
+ /* ----------- Hostname Check ----------- */
14512
+ /*----------------------------------------*/
14513
+ // Get hostnames
14514
+ const originURL = String(req.get('origin')
14515
+ || req.headers.origin
14516
+ || req.headers.referer);
14517
+ const originHostname = (originURL
14518
+ // Remove protocol
14519
+ .replace(/(^\w+:|^)\/\//, '')
14520
+ // Remove port
14521
+ .replace(/:\d+$/, ''));
14522
+ const serverHostname = String(req.hostname);
14523
+ // Check allowed
14524
+ if (opts.allowedHosts) {
14525
+ // Only accept requests from allowed hosts
14526
+ const allowedArray = (Array.isArray(opts.allowedHosts)
14527
+ ? opts.allowedHosts
14528
+ : [opts.allowedHosts]);
14529
+ // Check if server is localhost
14530
+ if (serverHostname === 'localhost') {
14531
+ // Allow localhost
14532
+ allowedArray.push('localhost');
14533
+ }
14534
+ // Check if current host is allowed
14535
+ const allowed = allowedArray.some((allowedHost) => {
14536
+ if (allowedHost.startsWith('*')) {
14537
+ // Check end of hostname
14538
+ return originHostname.endsWith(allowedHost.substring(1));
14539
+ }
14540
+ // Check full hostname
14541
+ return originHostname.toLowerCase() === allowedHost.toLowerCase();
14542
+ });
14543
+ // If not allowed, return error
14544
+ if (!allowed) {
14545
+ return handleError(res, {
14546
+ message: 'You are not allowed to access this endpoint.',
14547
+ code: ReactKitErrorCode$1.HostNotAllowed,
14548
+ status: 403,
14549
+ });
14550
+ }
14551
+ }
14552
+ // Check banned
14553
+ if (opts.bannedHosts) {
14554
+ // Do not allow requests from banned hosts
14555
+ const bannedArray = (Array.isArray(opts.bannedHosts)
14556
+ ? opts.bannedHosts
14557
+ : [opts.bannedHosts]);
14558
+ // Check if current host is banned
14559
+ const banned = bannedArray.some((bannedHost) => {
14560
+ if (bannedHost.startsWith('*')) {
14561
+ // Check end of hostname
14562
+ return originHostname.endsWith(bannedHost.substring(1));
14563
+ }
14564
+ // Check full hostname
14565
+ return originHostname.toLowerCase() === bannedHost.toLowerCase();
14566
+ });
14567
+ // If banned, return error
14568
+ if (banned) {
14569
+ return handleError(res, {
14570
+ message: 'You are not allowed to access this endpoint.',
14571
+ code: ReactKitErrorCode$1.HostBanned,
14572
+ status: 403,
14573
+ });
14574
+ }
14575
+ }
14576
+ /*----------------------------------------*/
14503
14577
  /* ------------ Parse Params ------------ */
14504
14578
  /*----------------------------------------*/
14505
14579
  // Process items one by one