Skip to content

Commit

Permalink
Only base64 encode auth header if it matches un-encoded Basic auth pa…
Browse files Browse the repository at this point in the history
…ttern.

Closes #50
Closes #51
  • Loading branch information
Eric Mrak committed Mar 18, 2016
1 parent 9216aff commit b912119
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/models/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,17 @@ function purifyHeaders(incoming) {
}

function purifyAuthorization(headers) {
var auth;
var auth, userpass;

if (headers == null || headers.authorization == null) { return headers; }

auth = headers.authorization || '';
if (!/:/.test(auth)) { return headers; }

headers.authorization = 'Basic ' + new Buffer(auth).toString('base64');
if (/^Basic .+:.+$/.test(auth)) {
userpass = auth.substr(6);
headers.authorization = 'Basic ' + new Buffer(userpass).toString('base64');
}

return headers;
}

Expand Down
2 changes: 1 addition & 1 deletion test/data/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
method: POST
post: some=data
headers:
authorization: stubby:passwordZ0r
authorization: Basic stubby:passwordZ0r
response:
status: 201
headers:
Expand Down
2 changes: 1 addition & 1 deletion test/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ describe('Endpoint', function () {
var actual, expected;
expected = 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=';
this.data.request.headers = {
authorization: 'username:password'
authorization: 'Basic username:password'
};

actual = new Endpoint(this.data);
Expand Down

0 comments on commit b912119

Please sign in to comment.