Skip to content

Commit

Permalink
Merge pull request #12237 from bbc/1475-reduce-injection-attack
Browse files Browse the repository at this point in the history
    WSTEAM1-1475  Reduce risk of injection attack via page url
  • Loading branch information
HarveyPeachey authored Dec 16, 2024
2 parents 407d290 + 4b194bb commit 44ce62e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
31 changes: 31 additions & 0 deletions ws-nextjs-app/utilities/pageRequests/getPageData.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fetchPageData from '#app/routes/utils/fetchPageData';
import * as getToggles from '#app/lib/utilities/getToggles';
import * as fetchDataFromBFF from '#app/routes/utils/fetchDataFromBFF';
import getPageData from './getPageData';

const agent = { cert: 'cert', ca: 'ca', key: 'key' };
Expand Down Expand Up @@ -43,6 +44,36 @@ describe('getPageData', () => {
expect(actualToggles).toStrictEqual(toggleResponse);
});

it('Cleans malicious query parameters', async () => {
const fetchDataResponse = { title: 'UGC Form Title!' };

const toggleResponse = {
toggles: { testToggle: { enabled: true } },
};

jest.spyOn(fetchPageData, 'default').mockResolvedValue({
status: 200,
json: { data: fetchDataResponse },
});

jest.spyOn(getToggles, 'default').mockResolvedValue(toggleResponse);

const fetchDataFromBFFSpy = jest.spyOn(fetchDataFromBFF, 'default');

await getPageData({
id: 'u50853489',
service: 'mundo',
variant: undefined,
rendererEnv: 'live&evilParam=evil',
resolvedUrl: '/mundo/send/u50853489',
pageType: 'ugcForm',
});

expect(fetchDataFromBFFSpy.mock.calls[0][0].pathname).toEqual(
'u50853489?renderer_env=live',
);
});

it('Returns page data and status 404 for an invalid page', async () => {
const errorMessage = 'Something went wrong!';
const toggleResponse = {
Expand Down
6 changes: 5 additions & 1 deletion ws-nextjs-app/utilities/pageRequests/getPageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ const getPageData = async ({
resolvedUrl,
pageType,
}: PageDataParams) => {
const pathname = `${id}${rendererEnv ? `?renderer_env=${rendererEnv}` : ''}`;
const path = `${id}${rendererEnv ? `?renderer_env=${rendererEnv}` : ''}`;
const url = new URL(path, 'https://www.bbc.com');
const rendererEnvironment = url.searchParams.get('renderer_env');
const pathname = `${id}${rendererEnvironment ? `?renderer_env=${rendererEnvironment}` : ''}`;

let message;
let status;
let json;
Expand Down

0 comments on commit 44ce62e

Please sign in to comment.