Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.

Commit 2689890

Browse files
authored
fix: Sort express mountpoints to avoid requests getting hijacked (#133)
- This commit reverse-sorts the mountpoints added to express so that shorter mountpoints such as '/' do not hijack traffic meant for other mountpoints e.g. '/api' or '/log'. This is not seen when running the dev server, as the dev server config does not enable the `client` module. Contributes to: #42 Signed-off-by: Andrew Borley <[email protected]>
1 parent 2578b77 commit 2689890

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

server/core/app.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,21 @@ export const returnExpress: (
7575
next();
7676
});
7777

78-
Object.entries(routingTable).forEach(
79-
([moduleName, { mountPoint, routerForModule }]) =>
78+
Object.entries(routingTable)
79+
.map(([moduleName, { mountPoint, routerForModule }]) => ({
80+
moduleName,
81+
mountPoint,
82+
routerForModule,
83+
}))
84+
.sort(({ mountPoint: mountPointA }, { mountPoint: mountPointB }) => {
85+
// Sort mountpoints in reverse order, so that the shortest mountpoints are added last
86+
// and do not route traffic meant for other endpoints
87+
return mountPointA < mountPointB ? 1 : mountPointA > mountPointB ? -1 : 0;
88+
})
89+
.forEach(({ moduleName, mountPoint, routerForModule }) => {
90+
logger.debug(
91+
`Setting up app.use('${mountPoint}') for module '${moduleName}'`
92+
);
8093
app.use(`${mountPoint}`, (req, res, next) => {
8194
// add logger for this module
8295
res.locals.strimziuicontext = {
@@ -91,8 +104,8 @@ export const returnExpress: (
91104
moduleName
92105
);
93106
isEnabled ? routerForModule(req, res, next) : next(); // if enabled, call the router for the module so it can handle the request. Else, call the next module
94-
})
95-
);
107+
});
108+
});
96109

97110
return app;
98111
};

utils/dev_config/server.e2e.config.js

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ module.exports = {
1616
...serverCertificates,
1717
},
1818
},
19+
logging: {
20+
level: 'debug',
21+
prettyPrint: {
22+
translateTime: true,
23+
},
24+
},
1925
proxy: {
2026
...mockadminServer,
2127
transport: {

0 commit comments

Comments
 (0)