Skip to content

Commit e5a5e2c

Browse files
committed
Tighten 'extract' parameter validation lovell#158
1 parent 797d503 commit e5a5e2c

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ Sharp.prototype.extract = function(topOffset, leftOffset, width, height) {
142142
var suffix = this.options.width === -1 && this.options.height === -1 ? 'Pre' : 'Post';
143143
var values = arguments;
144144
['topOffset', 'leftOffset', 'width', 'height'].forEach(function(name, index) {
145-
this.options[name + suffix] = values[index];
145+
if (typeof values[index] === 'number' && !Number.isNaN(values[index]) && (values[index] % 1 === 0) && values[index] >= 0) {
146+
this.options[name + suffix] = values[index];
147+
} else {
148+
throw new Error('Non-integer value for ' + name + ' of ' + values[index]);
149+
}
146150
}.bind(this));
147151
// Ensure existing rotation occurs before pre-resize extraction
148152
if (suffix === 'Pre' && this.options.angle !== 0) {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sharp",
3-
"version": "0.9.0",
3+
"version": "0.9.1",
44
"author": "Lovell Fuller <npm@lovell.info>",
55
"contributors": [
66
"Pierre Inglebert <pierre.inglebert@gmail.com>",
@@ -34,7 +34,7 @@
3434
"vips"
3535
],
3636
"dependencies": {
37-
"bluebird": "^2.8.2",
37+
"bluebird": "^2.9.3",
3838
"color": "^0.7.3",
3939
"nan": "^1.5.1",
4040
"semver": "^4.2.0"

test/unit/extract.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,62 @@ describe('Partial image extraction', function() {
116116
});
117117
});
118118

119+
describe('Invalid parameters', function() {
120+
121+
it('Undefined', function(done) {
122+
var isValid = true;
123+
try {
124+
sharp(fixtures.inputJpg).extract();
125+
} catch (err) {
126+
isValid = false;
127+
}
128+
assert.strictEqual(false, isValid);
129+
done();
130+
});
131+
132+
it('String top', function(done) {
133+
var isValid = true;
134+
try {
135+
sharp(fixtures.inputJpg).extract('spoons', 10, 10, 10);
136+
} catch (err) {
137+
isValid = false;
138+
}
139+
assert.strictEqual(false, isValid);
140+
done();
141+
});
142+
143+
it('Non-integral left', function(done) {
144+
var isValid = true;
145+
try {
146+
sharp(fixtures.inputJpg).extract(10, 10.2, 10, 10);
147+
} catch (err) {
148+
isValid = false;
149+
}
150+
assert.strictEqual(false, isValid);
151+
done();
152+
});
153+
154+
it('Negative width - negative', function(done) {
155+
var isValid = true;
156+
try {
157+
sharp(fixtures.inputJpg).extract(10, 10, -10, 10);
158+
} catch (err) {
159+
isValid = false;
160+
}
161+
assert.strictEqual(false, isValid);
162+
done();
163+
});
164+
165+
it('Null height', function(done) {
166+
var isValid = true;
167+
try {
168+
sharp(fixtures.inputJpg).extract(10, 10, 10, null);
169+
} catch (err) {
170+
isValid = false;
171+
}
172+
assert.strictEqual(false, isValid);
173+
done();
174+
});
175+
176+
});
119177
});

0 commit comments

Comments
 (0)