Skip to content

Commit f62b8ba

Browse files
committed
chore: clean up params in svg query
1 parent 65b0eca commit f62b8ba

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/http/helpers/query.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Basic cleanup for parameters
3+
*/
4+
export function cleanupQueryValue(value: string | undefined) {
5+
return value ? value.replace(/['"<>&]/g, '') : undefined;
6+
}

src/http/responses/css.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getStoredIconsData } from '../../data/icon-set/utils/get-icons.js';
66
import { iconSets } from '../../data/icon-sets.js';
77
import { paramToBoolean } from '../../misc/bool.js';
88
import { errorText } from '../helpers/errors.js';
9+
import { cleanupQueryValue } from '../helpers/query.js';
910

1011
/**
1112
* Check selector for weird stuff
@@ -57,7 +58,7 @@ export function generateIconsStyleResponse(prefix: string, query: FastifyRequest
5758

5859
// 'color': string
5960
// Sets color for monotone images
60-
const color = qOptions.color;
61+
const color = cleanupQueryValue(qOptions.color);
6162
if (typeof color === 'string' && stringToColor(color)) {
6263
options.color = color;
6364
}
@@ -98,23 +99,23 @@ export function generateIconsStyleResponse(prefix: string, query: FastifyRequest
9899
// 'commonSelector': string
99100
// Common selector for all requested icons
100101
// Alias: 'common'
101-
const commonSelector = qOptions.commonSelector || q.common;
102+
const commonSelector = cleanupQueryValue(qOptions.commonSelector || q.common);
102103
if (checkSelector(commonSelector)) {
103104
options.commonSelector = commonSelector;
104105
}
105106

106107
// 'iconSelector': string
107108
// Icon selector
108109
// Alias: 'selector'
109-
const iconSelector = qOptions.iconSelector || q.selector;
110+
const iconSelector = cleanupQueryValue(qOptions.iconSelector || q.selector);
110111
if (checkSelector(iconSelector)) {
111112
options.iconSelector = iconSelector;
112113
}
113114

114115
// 'overrideSelector': string
115116
// Selector for rules in icon that override common rules
116117
// Alias: 'override'
117-
const overrideSelector = qOptions.overrideSelector || q.override;
118+
const overrideSelector = cleanupQueryValue(qOptions.overrideSelector || q.override);
118119
if (checkSelector(overrideSelector)) {
119120
options.overrideSelector = overrideSelector;
120121
}

src/http/responses/svg.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { FastifyReply, FastifyRequest } from 'fastify';
88
import { getStoredIconData } from '../../data/icon-set/utils/get-icon.js';
99
import { iconSets } from '../../data/icon-sets.js';
1010
import { errorText } from '../helpers/errors.js';
11+
import { cleanupQueryValue } from '../helpers/query.js';
1112

1213
/**
1314
* Generate SVG
@@ -43,8 +44,8 @@ export function generateSVGResponse(prefix: string, name: string, query: Fastify
4344
const customisations: IconifyIconCustomisations = {};
4445

4546
// Dimensions
46-
customisations.width = q.width || defaultIconCustomisations.width;
47-
customisations.height = q.height || defaultIconCustomisations.height;
47+
customisations.width = cleanupQueryValue(q.width) || defaultIconCustomisations.width;
48+
customisations.height = cleanupQueryValue(q.height) || defaultIconCustomisations.height;
4849

4950
// Rotation
5051
customisations.rotate = q.rotate ? rotateFromString(q.rotate, 0) : 0;
@@ -75,7 +76,7 @@ export function generateSVGResponse(prefix: string, name: string, query: Fastify
7576
let html = iconToHTML(body, svg.attributes);
7677

7778
// Change color
78-
const color = q.color;
79+
const color = cleanupQueryValue(q.color);
7980
if (color && html.indexOf('currentColor') !== -1 && color.indexOf('"') === -1) {
8081
html = html.split('currentColor').join(color);
8182
}

0 commit comments

Comments
 (0)