Skip to content

Commit fad3e76

Browse files
authored
Verify background colour values are numbers (#4556)
1 parent 1f4f74e commit fad3e76

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

lib/colour.mjs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,16 @@ function _getBackgroundColourOption (value) {
146146
(is.string(value) && value.length >= 3 && value.length <= 200)
147147
) {
148148
const colour = color(value);
149-
return [
150-
colour.red(),
151-
colour.green(),
152-
colour.blue(),
153-
Math.round(colour.alpha() * 255)
154-
];
149+
const red = colour.red();
150+
const green = colour.green();
151+
const blue = colour.blue();
152+
const alpha = Math.round(colour.alpha() * 255);
153+
for (const [channel, component] of [['red', red], ['green', green], ['blue', blue], ['alpha', alpha]]) {
154+
if (!is.number(component)) {
155+
throw is.invalidParameterError(`background.${channel}`, 'number', component);
156+
}
157+
}
158+
return [red, green, blue, alpha];
155159
} else {
156160
throw is.invalidParameterError('background', 'object or string', value);
157161
}

test/unit/tint.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,20 @@ suite('Tint', () => {
105105
() => fixtures.assertMaxColourDistance(output, fixtures.expected('tint-cmyk.jpg'), maxDistance)
106106
);
107107
});
108+
109+
test('non-numeric colour component fails, identifying the channel', (t) => {
110+
t.plan(3);
111+
t.assert.throws(
112+
() => sharp().tint({ r: 'fail', g: 0, b: 0 }),
113+
/Expected number for background\.red but received NaN of type number/
114+
);
115+
t.assert.throws(
116+
() => sharp().tint({ r: NaN, g: 0, b: 0 }),
117+
/Expected number for background\.red but received NaN of type number/
118+
);
119+
t.assert.throws(
120+
() => sharp().tint({ r: 0, g: 0, b: 'fail' }),
121+
/Expected number for background\.blue but received NaN of type number/
122+
);
123+
});
108124
});

0 commit comments

Comments
 (0)