Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Add more structured error messages to bad image URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
ianb committed Jun 2, 2017
1 parent c44d66b commit 308913b
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/src/servershot.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ const PNG_HEADER = Buffer.from(PNG_HEADER_BASE64, "base64");
function assertPng(dataUrl) {
const urlHeader = "data:image/png;base64,";
if (!dataUrl.startsWith(urlHeader)) {
mozlog.warn("invalid-data-url", {msg: "Invalid data: URL submitted", prefix: dataUrl.substr(0, urlHeader.length + 10)});
throw new Error('invalid data url');
}
// only decode enough to get the header
// we're lucky that 9 bytes is exactly 12 base64 characters
const base64Header = dataUrl.substr(urlHeader.length, PNG_HEADER_BASE64.length);
if (base64Header.length < PNG_HEADER_BASE64.length) {
mozlog.warn("invalid-data-image", {msg: "Invalid PNG image submitted", prefix: dataUrl.substr(0, urlHeader.length + PNG_HEADER_BASE64.length)});
throw new Error('invalid image');
}
const header = Buffer.from(base64Header, "base64"); // 9 bytes
if (!PNG_HEADER.equals(header.slice(0, 8))) {
mozlog.warn("invalid-data-image-decoded", {msg: "Invalid PNG image (after base64 decoding)"});
throw new Error('invalid png');
}
}
Expand Down

0 comments on commit 308913b

Please sign in to comment.