Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix range handling for oversized and out of range situations #373

Merged
merged 3 commits into from
Jan 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/controllers.js
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ module.exports = function(rootDirectory, logger, indexDocument, errorDocument) {
"/" +
object.size
);
res.header("Content-Length", object.range.end + 1 - object.range.start);
res.header("Content-Length", object.range.end - object.range.start);
}

res.status(status);
@@ -439,6 +439,12 @@ module.exports = function(rootDirectory, logger, indexDocument, errorDocument) {
if (end) options.end = parseInt(end);
}
store.getObject(req.params.bucket, keyName, options, (err, object) => {
if (err === "ERANGE") {
res.header("Content-Range", "bytes */" + object);

return res.status(416).end();
}

if (!object) {
if (!indexDocument) return errorResponse(req, res, keyName);
keyName = path.posix.join(keyName, indexDocument);
11 changes: 10 additions & 1 deletion lib/stores/filesystem.js
Original file line number Diff line number Diff line change
@@ -253,9 +253,18 @@ class FilesystemStore {
if (err) return done(err.code === "ENOENT" ? null : err);
const object = new S3Object(bucket, key, content, metadata);
if (options && (options.start || options.end)) {
const bytesRead = content.bytesRead;
const totalBytes = metadata["content-length"];
if (
bytesRead === 0 &&
(options && options.start && options.start >= totalBytes)
) {
return done("ERANGE", totalBytes);
}

object.range = {
start: options.start || 0,
end: options.end || object.size - 1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this broke the range response header in 2.2.8, but looks like it's fixed on master.

end: options.start || 0 + bytesRead
};
}
return done(null, object);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@
"request-promise-native": "1.0.5"
},
"engines": {
"node": ">=6.0.0"
"node": ">=6.4.0"
},
"bugs": {
"url": "https://github.com/jamhall/s3rver/issues"
57 changes: 57 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -605,6 +605,63 @@ describe("S3rver Tests", function() {
expect(res.headers).to.have.property("content-length", "100");
});

it("out of bounds range requests should return 416", function*() {
const file = path.join(__dirname, "resources/image0.jpg");
const filesize = fs.statSync(file).size;
yield s3Client
.putObject({
Bucket: buckets[0],
Key: "image",
Body: yield fs.readFile(file),
ContentType: "image/jpeg"
})
.promise();
const url = s3Client.getSignedUrl("getObject", {
Bucket: buckets[0],
Key: "image"
});

try {
yield request({
url,
headers: { range: "bytes=65536-66000" },
resolveWithFullResponse: true
});
} catch (err) {
expect(err.statusCode).to.equal(416);
expect(err.response.headers).to.have.property(
"content-range",
`bytes */${filesize}`
);
}
});

it("partial out of bounds range requests should return actual length of returned data", function*() {
const file = path.join(__dirname, "resources/image0.jpg");
const filesize = fs.statSync(file).size;
yield s3Client
.putObject({
Bucket: buckets[0],
Key: "image",
Body: yield fs.readFile(file),
ContentType: "image/jpeg"
})
.promise();
const url = s3Client.getSignedUrl("getObject", {
Bucket: buckets[0],
Key: "image"
});
const res = yield request({
url,
headers: { range: "bytes=0-100000" },
resolveWithFullResponse: true
});
expect(res.statusCode).to.equal(206);
expect(res.headers).to.have.property("content-range");
expect(res.headers).to.have.property("accept-ranges");
expect(res.headers).to.have.property("content-length", filesize.toString());
});

it("should get image metadata from a bucket using HEAD method", function*() {
const file = path.join(__dirname, "resources/image0.jpg");
const fileContent = yield fs.readFile(file);