Skip to content

Instantly share code, notes, and snippets.

@felixge
Created May 20, 2011 18:07
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save felixge/983440 to your computer and use it in GitHub Desktop.
Save felixge/983440 to your computer and use it in GitHub Desktop.
On demand image resizing with node.js in
// Usage: http://localhost:8080/image.jpg/100x50
var http = require('http');
var spawn = require('child_process').spawn;
http.createServer(function(req, res) {
var params = req.url.split('/');
var convert = spawn('convert', [params[1], '-resize', params[2], '-']);
res.writeHead(200, {'Content-Type': 'image/jpeg'});
convert.stdout.pipe(res);
}).listen(8080);
// If you like this, consider attending our Node.js Workshop on June 10th in Cologne:
// http://nodecologne.eventbrite.com/
@tylerbenson
Copy link

This seems kind of unsafe passing raw parameters directly into a shell command. Am I missing something? Can extra parameters be passed in to call something else?

@Qard
Copy link

Qard commented May 20, 2011

As far as I know about how child_process works, this could only ever pass arguments to the "convert" command. Stuff like " && rm -rf /" shouldn't work.

@felixge
Copy link
Author

felixge commented May 20, 2011

@tylerbenson Sure you would certainly want to have some validation in there if you want to use this in production : ). But @Qard is right, I'd be impressed if you can exploit this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment