Skip to content

Commit 4e3a24e

Browse files
committed
fix: check subdomain earlier for /apps
1 parent c495ccf commit 4e3a24e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* This middleware checks the subdomain, and if the subdomain doesn't
3+
* match it calls `next('route')` to skip the current route.
4+
* Be sure to use this before any middleware that might erroneously
5+
* block the request.
6+
*
7+
* @param {string|string[]} allowedSubdomains - The subdomain to allow;
8+
* if an array, any of the subdomains in the array will be allowed.
9+
*
10+
* @returns {function} - An express middleware function
11+
*/
12+
const subdomain = allowedSubdomains => {
13+
if ( ! Array.isArray(allowedSubdomains) ) {
14+
allowedSubdomains = [allowedSubdomains];
15+
}
16+
return async (req, res, next) => {
17+
// Note: at the time of implementing this, there is a config
18+
// option called `experimental_no_subdomain` that is designed
19+
// to lie and tell us the subdomain is `api` when it's not.
20+
const actual_subdomain = require('../helpers').subdomain(req);
21+
if ( ! allowedSubdomains.includes(actual_subdomain) ) {
22+
next('route');
23+
}
24+
};
25+
}
26+
27+
module.exports = subdomain;

packages/backend/src/routers/apps.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ const auth = require('../middleware/auth.js');
2323
const config = require('../config');
2424
const { app_name_exists, refresh_apps_cache, chkperm, convert_path_to_fsentry, get_app } = require('../helpers');
2525
const { DB_WRITE, DB_READ } = require('../services/database/consts.js');
26+
const subdomain = require('../middleware/subdomain.js');
2627

2728
// -----------------------------------------------------------------------//
2829
// GET /apps
2930
// -----------------------------------------------------------------------//
30-
router.get('/apps', auth, express.json({limit: '50mb'}), async (req, res, next)=>{
31-
// check subdomain
32-
if(require('../helpers').subdomain(req) !== 'api')
33-
next();
31+
router.get('/apps',
32+
subdomain('api'),
33+
auth, express.json({limit: '50mb'}), async (req, res, next)=>{
34+
// /!\ open brace on end of previous line
3435

3536
// check if user is verified
3637
if((config.strict_email_verification_required || req.user.requires_email_confirmation) && !req.user.email_confirmed)
@@ -88,7 +89,11 @@ router.get('/apps', auth, express.json({limit: '50mb'}), async (req, res, next)=
8889
// -----------------------------------------------------------------------//
8990
// GET /apps/:name(s)
9091
// -----------------------------------------------------------------------//
91-
router.get('/apps/:name', auth, express.json({limit: '50mb'}), async (req, res, next)=>{
92+
router.get('/apps/:name',
93+
subdomain('api'),
94+
auth, express.json({limit: '50mb'}), async (req, res, next)=>{
95+
// /!\ open brace on end of previous line
96+
9297
// check subdomain
9398
if(require('../helpers').subdomain(req) !== 'api')
9499
next();

0 commit comments

Comments
 (0)