diff --git a/__test__/utils.test.ts b/__test__/utils.test.ts index 055474f6..d4fa69bb 100644 --- a/__test__/utils.test.ts +++ b/__test__/utils.test.ts @@ -1,7 +1,7 @@ import express from 'express'; import request from 'supertest'; import { Utilities } from '../src/utils'; -import { fakeQueryString, fakeQueryStringRes, options } from './fakeInputs'; +import { fakeQueryString, fakeQueryStringRes, options, themes } from './fakeInputs'; import { Handlers } from '../src/handlers'; import { createGraph } from '../src/createChart'; @@ -31,6 +31,7 @@ describe('Utilities Test', () => { .get('/graph?username=ashutosh00710') .expect('Content-Type', 'image/svg+xml; charset=utf-8') .expect('Cache-Control', 'public, max-age=1800') + .expect('Content-Security-Policy', "script-src 'none'; sandbox") .expect(200, done); }); }); @@ -43,10 +44,73 @@ describe('Utilities Test', () => { .get('/graph?username=') .expect('Content-Type', 'image/svg+xml; charset=utf-8') .expect('Cache-Control', 'no-store, max-age=0') + .expect('Content-Security-Policy', "script-src 'none'; sandbox") .expect(200, done); }); }); + describe('Color sanitization', () => { + it('falls back to the theme color when a color param is not valid hex', () => { + const utils = new Utilities({ + username: 'githubusername', + color: 'red} ', + point: 'red', + title_color: 'red} ', + } as any); + + expect(utils.queryOptions().colors).toEqual(themes.default); + }); + + it('accepts valid hex color params unchanged', () => { + const utils = new Utilities({ + username: 'githubusername', + color: 'abc123', + bg_color: 'fff', + } as any); + + const { colors } = utils.queryOptions(); + expect(colors.color).toBe('abc123'); + expect(colors.bgColor).toBe('fff'); + }); + + it('rejects hex-only values with a length CSS/SVG does not support', () => { + const utils = new Utilities({ + username: 'githubusername', + color: 'a', // 1 digit + bg_color: 'ab', // 2 digits + line: 'abcde', // 5 digits + point: 'abcdefa', // 7 digits + } as any); + + const { colors } = utils.queryOptions(); + expect(colors.color).toBe(themes.default.color); + expect(colors.bgColor).toBe(themes.default.bgColor); + expect(colors.lineColor).toBe(themes.default.lineColor); + expect(colors.pointColor).toBe(themes.default.pointColor); + }); + }); + + describe('Title escaping', () => { + it('escapes HTML in custom_title so it cannot break out of the SVG', async () => { + const utils = new Utilities({ + username: 'githubusername', + custom_title: '', + } as any); + + const { finalGraph } = await utils.buildGraph({ + name: 'someone', + contributions: [{ contributionCount: 1, date: '1' }], + }); + + expect(finalGraph).not.toContain('