Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 6483b29

Browse files
committed
Used application/json for session POST
Modified CookieTokenManager to use JSON. Decoded credentials from URL before passing to CookieTokenManager. Improved documentation of characters that must be encoded.
1 parent 28fb98b commit 6483b29

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# UNRELEASED
22
- [FIXED] Expose BasePlugin.
3+
- [FIXED] Prevent double encoding of credentials passed in URL user information
4+
when using the `cookieauth` plugin.
5+
- [IMPROVED] Documented the characters that are required to be encoded in URL
6+
user information.
37
- [IMPROVED] Documented the legacy compatibility behaviour that always adds the
48
`cookieauth` plugin when using the initialization callback functionality.
59

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ var Cloudant = require('@cloudant/cloudant');
156156
var cloudant = Cloudant("http://MYUSERNAME:MYPASSWORD@localhost:5984");
157157
~~~
158158

159+
**Note**: If you pass credentials in the user information subcomponent of the URL
160+
then they must be [percent encoded](https://tools.ietf.org/html/rfc3986#section-3.2.1).
161+
Specifically the characters `: / ? # [ ] @ %` _MUST_ be precent-encoded, other
162+
characters _MAY_ be percent encoded.
163+
Credentials must not be percent encoded when passing them via other configuration
164+
options besides `url`.
165+
159166
**Note**: If you pass in a `username`, `password`, and `url` that contains
160167
credentials, the `username` and `password` will supercede the credentials within
161168
the `url`. For example, `myusername` and `mypassword` will be used in the code

lib/tokens/CookieTokenManager.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class CookieTokenManager extends TokenManager {
2929
this._client({
3030
url: this._sessionUrl,
3131
method: 'POST',
32-
form: {
32+
json: true,
33+
body: {
3334
name: this._username,
3435
password: this._password
3536
},

plugins/cookieauth.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ class CookiePlugin extends BasePlugin {
4949
client,
5050
this._jar,
5151
u.format(sessionUrl, {auth: false}),
52-
sessionUrl.username,
53-
sessionUrl.password
52+
// Extract creds from URL and decode
53+
decodeURIComponent(sessionUrl.username),
54+
decodeURIComponent(sessionUrl.password)
5455
);
5556

5657
if (cfg.autoRenew) {

test/plugins/cookieauth.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ const nock = require('../nock.js');
2121
const uuidv4 = require('uuid/v4'); // random
2222

2323
const ME = process.env.cloudant_username || 'nodejs';
24-
const PASSWORD = process.env.cloudant_password || 'sjedon';
24+
const PASSWORD = process.env.cloudant_password || 'sjedon!@#"£$%^&*()';
2525
const SERVER = process.env.SERVER_URL || `https://${ME}.cloudant.com`;
2626
const SERVER_NO_PROTOCOL = SERVER.replace(/^https?:\/\//, '');
27-
const SERVER_WITH_CREDS = `https://${ME}:${PASSWORD}@${SERVER_NO_PROTOCOL}`;
27+
const SERVER_WITH_CREDS = `https://${ME}:${encodeURIComponent(PASSWORD)}@${SERVER_NO_PROTOCOL}`;
2828
const DBNAME = `/nodejs-cloudant-${uuidv4()}`;
2929
const COOKIEAUTH_PLUGIN = [ { cookieauth: { autoRenew: false } } ];
3030

0 commit comments

Comments
 (0)