Skip to content

Commit b1ea26a

Browse files
committed
fix(putUser): ensure reserved words are enforced in metadata
BREAKING CHANGE: In both signUp and putUser, '_id', '_rev', 'name', 'type', 'roles', 'password', 'password_scheme', 'iterations', 'derived_key', 'salt' are now all reserved words, and 'metadata' is not a reserved word anymore.
1 parent 67b68a9 commit b1ea26a

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

src/index.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,23 @@ function wrapError(callback) {
2222
}
2323

2424
function putUser(db, user, opts, callback) {
25-
var reservedWords = ['name', 'password', 'roles', 'type', 'salt', 'metadata'];
25+
var reservedWords = [
26+
'_id',
27+
'_rev',
28+
'name',
29+
'type',
30+
'roles',
31+
'password',
32+
'password_scheme',
33+
'iterations',
34+
'derived_key',
35+
'salt'
36+
];
37+
2638
if (opts.metadata) {
2739
for (var key in opts.metadata) {
28-
if (opts.hasOwnProperty(key)) {
29-
if (reservedWords.indexOf(key) !== -1 || key.startsWith('_')) {
30-
return callback(new AuthError('cannot use reserved word in metadata: "' + key + '"'));
31-
}
40+
if (opts.metadata.hasOwnProperty(key) && reservedWords.indexOf(key) !== -1) {
41+
return callback(new AuthError('cannot use reserved word in metadata: "' + key + '"'));
3242
}
3343
}
3444
user = assign(user, opts.metadata);

test/test.js

+69-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ testCases.forEach(function (testCase) {
1919
describe('authentication-' + testCase, function () {
2020

2121
var dbName = testCase === 'normal' ?
22-
'http://localhost:5984/testdb' :
23-
'http://localhost:5984/testdb/'; // trailing slash
22+
'http://localhost:5984/testdb' :
23+
'http://localhost:5984/testdb/'; // trailing slash
2424

2525
var db;
2626

@@ -141,6 +141,73 @@ testCases.forEach(function (testCase) {
141141
});
142142
});
143143

144+
var reservedWords = [
145+
'_id',
146+
'_rev',
147+
'name',
148+
'type',
149+
'roles',
150+
'password',
151+
'password_scheme',
152+
'iterations',
153+
'derived_key',
154+
'salt'
155+
];
156+
157+
reservedWords.forEach(function (key) {
158+
it('Test changing metadata using reserved word "' + key + '"', function () {
159+
return db.signup('robin', 'dickgrayson').then(function (res) {
160+
res.ok.should.equal(true);
161+
return db.login('robin', 'dickgrayson');
162+
}).then(function () {
163+
return db.getUser('robin').then(function (user) {
164+
var metadata = {};
165+
metadata[key] = 'test';
166+
return db.putUser('robin', {metadata: metadata}).then(function (res) {
167+
res.ok.should.not.equal(true);
168+
}).catch(function (err) {
169+
should.exist(err);
170+
err.status.should.equal(400);
171+
err.name.should.equal('authentication_error');
172+
err.message.should.equal('cannot use reserved word in metadata: "' + key + '"');
173+
err.error.should.equal(true);
174+
175+
if (key === 'password') {
176+
return db.login('robin', 'dickgrayson').then(function (res) {
177+
res.ok.should.equal(true);
178+
}).catch(function (err) {
179+
should.not.exist(err);
180+
});
181+
} else {
182+
return db.getUser('robin').then(function (changedUser) {
183+
changedUser[key].should.deep.equal(user[key]);
184+
}).catch(function (err) {
185+
should.not.exist(err);
186+
});
187+
}
188+
});
189+
});
190+
});
191+
});
192+
});
193+
194+
it('Test changing metadata using non-reserved word "metadata"', function () {
195+
var metadata = {test: 'test'};
196+
return db.signup('robin', 'dickgrayson').then(function (res) {
197+
res.ok.should.equal(true);
198+
return db.login('robin', 'dickgrayson');
199+
}).then(function () {
200+
return db.putUser('robin', {metadata: {metadata: metadata}});
201+
}).then(function (res) {
202+
res.ok.should.equal(true);
203+
return db.getUser('robin');
204+
}).then(function (changedUser) {
205+
changedUser.metadata.should.deep.equal(metadata);
206+
}).catch(function (err) {
207+
should.not.exist(err);
208+
});
209+
});
210+
144211
it('Test wrong user for getUser', function () {
145212
return db.signup('robin', 'dickgrayson').then(function (res) {
146213
return db.signup('aquaman', 'sleeps_with_fishes');

0 commit comments

Comments
 (0)