Skip to content

Commit

Permalink
fix: server encoding attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
mjancarik committed Mar 15, 2024
1 parent 48ede61 commit 34e7115
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-parents-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ima/server": patch
---

Encode all url specific values of $IMA attributes and urlParser method throws TypeError for unsopported protocols.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('Server App Factory', () => {
$Server: {
concurrency: 1,
staticConcurrency: 100,
protocol: 'http',
cache: {
enabled: true,
},
Expand Down Expand Up @@ -314,7 +315,7 @@ describe('Server App Factory', () => {
it('should render 500 ima app page', async () => {
jest
.spyOn(router, 'route')
.mockReturnValue(Promise.reject(new Error('Error')));
.mockReturnValue(Promise.reject(new Error('Custom error messages')));

const response = await serverApp.requestHandlerMiddleware(REQ, RES);

Expand Down
8 changes: 4 additions & 4 deletions packages/server/lib/factory/__tests__/urlParserFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,10 @@ describe('urlParserFactory', () => {

it(`should always use environment.$Server.protocol, when defined for ${originalUrl}`, () => {
ENVIRONMENT.$Server = {
protocol: 'env-protocol',
protocol: 'http',
};

expect(getProtocol(originalUrl, protocol)).toBe('env-protocol:');
expect(getProtocol(originalUrl, protocol)).toBe('http:');

ENVIRONMENT.$Server = {};
});
Expand Down Expand Up @@ -372,10 +372,10 @@ describe('urlParserFactory', () => {

it(`should always use environment.$Server.protocol, when defined for header key '${headerKey}'`, () => {
ENVIRONMENT.$Server = {
protocol: 'env-protocol',
protocol: 'http',
};

expect(getHeadersProtocol(header, headerKey)).toBe('env-protocol:');
expect(getHeadersProtocol(header, headerKey)).toBe('http:');

ENVIRONMENT.$Server = {};
});
Expand Down
13 changes: 9 additions & 4 deletions packages/server/lib/factory/responseUtilsFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ module.exports = function responseUtilsFactory({ applicationFolder }) {
$IMA.SPA = ${response?.SPA ?? false};
$IMA.$PublicPath = "${process.env.IMA_PUBLIC_PATH ?? ''}";
$IMA.$RequestID = "${requestID}";
$IMA.$Language = "${settings.$Language}";
$IMA.$Language = "${
settings.$Language && encodeHTMLEntities(settings.$Language)
}";
$IMA.$Env = "${settings.$Env}";
$IMA.$Debug = ${settings.$Debug};
$IMA.$Version = "${settings.$Version}";
Expand All @@ -55,9 +57,12 @@ module.exports = function responseUtilsFactory({ applicationFolder }) {
settings.$Protocol && encodeHTMLEntities(settings.$Protocol)
}";
$IMA.$Host = "${settings.$Host && encodeHTMLEntities(settings.$Host)}";
$IMA.$Path = "${settings.$Path}";
$IMA.$Root = "${settings.$Root}";
$IMA.$LanguagePartPath = "${settings.$LanguagePartPath}";
$IMA.$Path = "${settings.$Path && encodeHTMLEntities(settings.$Path)}";
$IMA.$Root = "${settings.$Root && encodeHTMLEntities(settings.$Root)}";
$IMA.$LanguagePartPath = "${
settings.$LanguagePartPath &&
encodeHTMLEntities(settings.$LanguagePartPath)
}";
})(typeof window !== 'undefined' && window !== null ? window : global);
`;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/server/lib/factory/urlParserFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const { URL } = require('url');

const { GenericError } = require('@ima/core');

const HTTP_PROTOCOL = 'http';
const HTTPS_PROTOCOL = 'https';

module.exports = function urlParserFactory({ applicationFolder, environment }) {
const IMA_CONFIG_JS_PATH = path.resolve(applicationFolder, './ima.config.js');

Expand Down Expand Up @@ -93,7 +96,8 @@ module.exports = function urlParserFactory({ applicationFolder, environment }) {
let protocol = null;

if (httpsHeader) {
protocol = httpsHeader.toLowerCase() === 'on' ? 'https' : 'http';
protocol =
httpsHeader.toLowerCase() === 'on' ? HTTPS_PROTOCOL : HTTP_PROTOCOL;
}

return protocol;
Expand All @@ -113,6 +117,12 @@ module.exports = function urlParserFactory({ applicationFolder, environment }) {
: environment.$Server.protocol;
}

if (![HTTP_PROTOCOL, HTTPS_PROTOCOL].includes(protocol)) {
throw new TypeError(
`Invalid protocol: You set unsupported protocol "${protocol}". Allowed protocols are only ${HTTP_PROTOCOL} and ${HTTPS_PROTOCOL}. You can set protocol in environment.$Server.protocol or update proxy configuration.`
);
}

return `${protocol}:`;
}

Expand Down

0 comments on commit 34e7115

Please sign in to comment.