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

Commit

Permalink
assert data: has a png header
Browse files Browse the repository at this point in the history
  • Loading branch information
dannycoates committed Mar 31, 2017
1 parent e7f78a3 commit 3cefe43
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion server/src/servershot.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ const mozlog = require("mozlog")("servershot");

const SEARCHABLE_VERSION = 1;

const PNG_HEADER_BASE64 = "iVBORw0KGgo=";
const PNG_HEADER = Buffer.from(PNG_HEADER_BASE64, "base64");

function assertPng(dataUrl) {
const urlHeader = "data:image/png;base64,";
if (!dataUrl.startsWith(urlHeader)) {
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) {
throw new Error('invalid image');
}
const header = Buffer.from(base64Header, "base64"); // 9 bytes
if (!PNG_HEADER.equals(header.slice(0,8))) {
throw new Error('invalid png');
}
}

let ClipRewrites;

let s3bucket;
Expand Down Expand Up @@ -88,7 +108,7 @@ if (! config.useS3) {
put = (uid, body, comment) => {
return new Promise((resolve, reject) => {
s3bucket.createBucket(() => {
var params = {Key: uid, Body: body};
var params = {Key: uid, Body: body, ContentType: "image/png"};
s3bucket.upload(params, function (err, result) {
if (err) {
reject(err);
Expand Down Expand Up @@ -295,6 +315,13 @@ Shot.getRawBytesForClip = function (uid) {
exports.Shot = Shot;

class ServerClip extends AbstractShot.prototype.Clip {
constructor(shot, id, json) {
super(shot, id, json);
if (this.isDataUrl()) {
assertPng(json.image.url);
}
}

imageBinary() {
if (! (this.image && this.image.url)) {
throw new Error("Not an image clip");
Expand Down

0 comments on commit 3cefe43

Please sign in to comment.