-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add checking for proper header characters.
Add module for doing things with headers. Fix up tests that catch errors.
- Loading branch information
1 parent
3f031ed
commit e27d468
Showing
8 changed files
with
140 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var conf = require('../conf'); | ||
|
||
exports.addInfoHeaders = function(options, info) { | ||
var MAX_INFO_HEADERS = conf.MAX_INFO_HEADERS; | ||
var invalidKeys = []; | ||
if (info) { | ||
var keys = Object.keys(info); | ||
|
||
if (keys.length > MAX_INFO_HEADERS) { | ||
throw new Error('Too many info headers: maximum of ' + MAX_INFO_HEADERS + ' allowed'); | ||
} | ||
|
||
keys.forEach(addInfoHeader); | ||
|
||
if (invalidKeys.length) { | ||
throw new Error('Info header keys contain invalid characters: ' + invalidKeys.join(' ')); | ||
} | ||
} | ||
|
||
function isValidHeader(header) { | ||
return /^[a-z0-9\-]+$/i.test(header); | ||
} | ||
|
||
function addInfoHeader(infoKey) { | ||
if (isValidHeader(infoKey)) { | ||
var key = 'X-Bz-Info-' + infoKey; | ||
options.headers[key] = encodeURIComponent(info[infoKey]); | ||
} else { | ||
return invalidKeys.push(infoKey); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
var expect = require('expect.js'); | ||
|
||
var headers = require('../../../lib/headers'); | ||
|
||
describe('headers', function() { | ||
|
||
var options; | ||
var info; | ||
var err; | ||
|
||
beforeEach(function() { | ||
options = { | ||
headers: {} | ||
}; | ||
|
||
info = null; | ||
|
||
err = { | ||
message: '' | ||
}; | ||
}); | ||
|
||
describe('addInfoHeaders', function() { | ||
|
||
it('should add the provided key/values to the headers properties of the options argument', function() { | ||
info = { | ||
foo: 'bar', | ||
unicorns: 'rainbows' | ||
}; | ||
|
||
headers.addInfoHeaders(options, info); | ||
|
||
expect(options.headers).to.eql({ | ||
'X-Bz-Info-foo': 'bar', | ||
'X-Bz-Info-unicorns': 'rainbows' | ||
}); | ||
}); | ||
|
||
it('should throw an error when too many info headers are added', function() { | ||
info = { | ||
a: 'foo', | ||
b: 'foo', | ||
c: 'foo', | ||
d: 'foo', | ||
e: 'foo', | ||
f: 'foo', | ||
g: 'foo', | ||
h: 'foo', | ||
i: 'foo', | ||
j: 'foo', | ||
k: 'foo' | ||
}; | ||
|
||
try { | ||
headers.addInfoHeaders(options, info); | ||
} catch (e) { | ||
err = e; | ||
} | ||
expect(err.message).to.be('Too many info headers: maximum of 10 allowed'); | ||
}); | ||
|
||
it('should throw an error with a list of keys that have invalid characters', function() { | ||
info = { | ||
'abcsABC-!@#$_': 'woh bad key', | ||
'defgDEF-)(*&^:"': 'very bad key' | ||
}; | ||
|
||
try { | ||
headers.addInfoHeaders(options, info); | ||
} catch (e) { | ||
err = e; | ||
} | ||
expect(err.message).to.be('Info header keys contain invalid characters: abcsABC-!@#$_ defgDEF-)(*&^:"'); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters