ftlabs / screens

A way to distribute websites to multiple displays
12 stars 5 forks source link

isImage check can crash server if stream tries to write after it's been closed #7

Closed seanmtracey closed 8 years ago

seanmtracey commented 8 years ago

When we enter a URL that we can't immediately transform, we check whether or not it's an image by going to the URL, grabbing the first 8 bytes and checking the value of those bytes against a table of values that indicate an image and it's type (PNG/JPG etc.).

After we grab the first few bytes of the request, we close the stream so we don't download the entire file. Sometimes, when running this check, after we close the stream, a write event occurs which tries to write to a stream that doesn't exist anymore, which crashes the server

transform: unknown so checking isImage, url= http://ftlabs-six-degrees.herokuapp.com/graph.html
events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: write after end
    at writeAfterEnd (_stream_writable.js:159:12)
    at Gunzip.Writable.write (_stream_writable.js:204:5)
    at PassThrough.ondata (_stream_readable.js:528:20)
    at emitOne (events.js:77:13)
    at PassThrough.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at PassThrough.Readable.push (_stream_readable.js:110:10)
    at PassThrough.Transform.push (_stream_transform.js:128:32)
    at afterTransform (_stream_transform.js:76:12)
    at TransformState.afterTransform (_stream_transform.js:54:12)
seanmtracey commented 8 years ago

Commit 2c042d5 doesn't resolve the issue :(

To reproduce the crash,

  1. Run Screens locally
  2. Go to carousel generator
  3. Copy the link for one of the demo carousels
  4. Open the link in a new tab The server should have crashed with the error shown above