-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(EMS-3708): code scanning - Server-side URL redirect (#2882)
* chore(EMS-3708): fix code scanning issue with unvalidated req.originalUrl * chore(EMS-3708): code fixes * chore(EMS-3707): code fixes * chore(EMS-3707): test fix * chore(EMS-3707): console.error fix --------- Co-authored-by: Zain Kassam <[email protected]>
- Loading branch information
Showing
5 changed files
with
152 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/ui/server/helpers/is-valid-req-original-url/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import isValidReqOriginalUrl from '.'; | ||
|
||
describe('helpers/is-valid-req-original-url', () => { | ||
describe('when provided url has invalid special characters', () => { | ||
it('should return false', () => { | ||
const mockUrl = 'abc!'; | ||
|
||
const result = isValidReqOriginalUrl(mockUrl); | ||
|
||
expect(result).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('when provided url is an empty string', () => { | ||
it('should return false', () => { | ||
const mockUrl = ''; | ||
|
||
const result = isValidReqOriginalUrl(mockUrl); | ||
|
||
expect(result).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('when provided url contains a space', () => { | ||
it('should return false', () => { | ||
const mockUrl = 'abc '; | ||
|
||
const result = isValidReqOriginalUrl(mockUrl); | ||
|
||
expect(result).toEqual(false); | ||
}); | ||
}); | ||
|
||
describe('when provided url only contains letters', () => { | ||
it('should return true', () => { | ||
const mockUrl = 'abc'; | ||
|
||
const result = isValidReqOriginalUrl(mockUrl); | ||
|
||
expect(result).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('when provided url contains letters and numbers', () => { | ||
it('should return true', () => { | ||
const mockUrl = 'abc123'; | ||
|
||
const result = isValidReqOriginalUrl(mockUrl); | ||
|
||
expect(result).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('when provided url only contains numbers', () => { | ||
it('should return true', () => { | ||
const mockUrl = '123'; | ||
|
||
const result = isValidReqOriginalUrl(mockUrl); | ||
|
||
expect(result).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('when provided url contains letters, numbers, "/", "?", "&", "-" and "="', () => { | ||
it('should return true', () => { | ||
const mockUrl = '123abc/?&-='; | ||
|
||
const result = isValidReqOriginalUrl(mockUrl); | ||
|
||
expect(result).toEqual(true); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Joi from 'joi'; | ||
import { REGEX } from '../../constants'; | ||
|
||
/** | ||
* Checks if the provided original URL is valid. | ||
* checks against NUMBER_ALPHA_HYPHEN_DASH_QUESTION_EQUALS_AND regex | ||
* if error on validation, returns false | ||
* otherwise, return true | ||
* @param {string} originalUrl - The original URL to validate. | ||
* @returns {Boolean} | ||
*/ | ||
const isValidReqOriginalUrl = (originalUrl: string) => { | ||
const joiString = Joi.string(); | ||
|
||
const schema = () => joiString.regex(REGEX.VALID_REQUEST_ORIGINAL_URL).required(); | ||
|
||
const validation = schema().validate(originalUrl); | ||
|
||
return !validation.error; | ||
}; | ||
|
||
export default isValidReqOriginalUrl; |