From 762e26713c4c00b0983c53142b5b0035ec4fdc85 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Wed, 15 Apr 2015 09:21:31 -0700 Subject: [PATCH] Make `bunyan` defensive on `res.header=null`. Fixes #244. Also add test case for this and a couple recent `bunyan` crashers. --- CHANGES.md | 2 +- bin/bunyan | 6 ++---- test/cli.test.js | 14 +++++++++----- test/corpus/{ => old-crashers}/139.log | 0 test/corpus/{ => old-crashers}/144.log | 0 test/corpus/old-crashers/233.log | 1 + test/corpus/old-crashers/242.log | 1 + test/corpus/old-crashers/244.log | 1 + test/corpus/old-crashers/README.md | 2 ++ 9 files changed, 17 insertions(+), 10 deletions(-) rename test/corpus/{ => old-crashers}/139.log (100%) rename test/corpus/{ => old-crashers}/144.log (100%) create mode 100644 test/corpus/old-crashers/233.log create mode 100644 test/corpus/old-crashers/242.log create mode 100644 test/corpus/old-crashers/244.log create mode 100644 test/corpus/old-crashers/README.md diff --git a/CHANGES.md b/CHANGES.md index 7227b22..89b921f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,7 +8,7 @@ Known issues: ## bunyan 1.3.6 (not yet released) -(nothing yet) +- [issue #244] Make `bunyan` defensive on `res.header=null`. ## bunyan 1.3.5 diff --git a/bin/bunyan b/bin/bunyan index 9078875..4d9af2a 100755 --- a/bin/bunyan +++ b/bin/bunyan @@ -929,12 +929,10 @@ function emitRecord(rec, line, opts, stylize) { // Typical JSON.stringify of a core node HttpResponse?) var headerTypes = {string: true, object: true}; var headers; - if (res.header !== undefined && headerTypes[typeof (res.header)]) { + if (res.header && headerTypes[typeof (res.header)]) { headers = res.header; delete res.header; - } else if (res.headers !== undefined && - headerTypes[typeof (res.headers)]) - { + } else if (res.headers && headerTypes[typeof (res.headers)]) { headers = res.headers; delete res.headers; } diff --git a/test/cli.test.js b/test/cli.test.js index 8b2010b..c4076a4 100644 --- a/test/cli.test.js +++ b/test/cli.test.js @@ -1,12 +1,13 @@ /* - * Copyright (c) 2012 Trent Mick. All rights reserved. + * Copyright (c) 2015 Trent Mick. All rights reserved. * * Test the `bunyan` CLI. */ var p = console.warn; -var path = require('path'); var exec = require('child_process').exec; +var fs = require('fs'); +var path = require('path'); var _ = require('util').format; var vasync = require('vasync'); @@ -401,11 +402,14 @@ test('robust req handling', function (t) { }); // Some past crashes from issues. -test('should not crash on these', function (t) { +test('should not crash on corpus/old-crashers/*.log', function (t) { + var oldCrashers = fs.readdirSync( + path.resolve(__dirname, 'corpus/old-crashers')) + .filter(function (f) { return f.slice(-4) === '.log'; }); vasync.forEachPipeline({ - inputs: ['139.log', '144.log'], + inputs: oldCrashers, func: function (logName, next) { - exec(_('%s %s/corpus/%s', BUNYAN, __dirname, logName), + exec(_('%s %s/corpus/old-crashers/%s', BUNYAN, __dirname, logName), function (err, stdout, stderr) { next(err); }); diff --git a/test/corpus/139.log b/test/corpus/old-crashers/139.log similarity index 100% rename from test/corpus/139.log rename to test/corpus/old-crashers/139.log diff --git a/test/corpus/144.log b/test/corpus/old-crashers/144.log similarity index 100% rename from test/corpus/144.log rename to test/corpus/old-crashers/144.log diff --git a/test/corpus/old-crashers/233.log b/test/corpus/old-crashers/233.log new file mode 100644 index 0000000..48c3a28 --- /dev/null +++ b/test/corpus/old-crashers/233.log @@ -0,0 +1 @@ +{"name":"server","hostname":"iZ25apt4ethZ","pid":958,"level":30,"req":{"method":"GET","url":"/ground/fetch?id=null","headers":{"host":"123.123.123.123","connection":"Keep-Alive","accept-encoding":"gzip"},"remoteAddress":"::ffff:123.123.123.123"},"res":{"headers":true,"content":{"status":0,"message":"success","messages":[]}},"msg":"Time used: 30ms","time":"2015-03-07T07:28:32.431Z","v":0} diff --git a/test/corpus/old-crashers/242.log b/test/corpus/old-crashers/242.log new file mode 100644 index 0000000..3c2e9c4 --- /dev/null +++ b/test/corpus/old-crashers/242.log @@ -0,0 +1 @@ +{"name":"AKP48","module":"Server","hostname":"AKP48.akpwebdesign.com","pid":32421,"level":60,"err":{"message":"Function.prototype.apply: Arguments list has wrong type","name":"TypeError","stack":[{},{},{},{},{},{}]},"msg":"Exception caught: TypeError: Function.prototype.apply: Arguments list has wrong type","time":"2015-04-13T04:03:46.206Z","v":0} diff --git a/test/corpus/old-crashers/244.log b/test/corpus/old-crashers/244.log new file mode 100644 index 0000000..ceb0305 --- /dev/null +++ b/test/corpus/old-crashers/244.log @@ -0,0 +1 @@ +{"name":"multichannel","hostname":"macbook-dev","pid":44973,"level":30,"res":{"statusCode":401,"header":null},"response":"{\"error\":\"InvalidCredentials\",\"description\":\"The access token provided has expired.\"}","msg":"","time":"2015-04-15T10:37:39.557Z","v":0} diff --git a/test/corpus/old-crashers/README.md b/test/corpus/old-crashers/README.md new file mode 100644 index 0000000..1627e9c --- /dev/null +++ b/test/corpus/old-crashers/README.md @@ -0,0 +1,2 @@ +Log lines that used to crash `bunyan`. +Typically the file name is the bunyan issue number.