Repository navigation
Disable inclusion of User.accessTokens #1318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "name": "blog", | ||
| "base": "PersistedModel", | ||
| "properties": { | ||
| "title": { | ||
| "type": "string" | ||
| }, | ||
| "content": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "relations": { | ||
| "user": { | ||
| "type": "belongsTo", | ||
| "model": "myUser" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "name": "myUser", | ||
| "base": "User", | ||
| "relations": { | ||
| "blogs": { | ||
| "model": "blog", | ||
| "type": "hasMany", | ||
| "foreignKey": "userId" | ||
| } | ||
| }, | ||
| "acls": [ | ||
| { | ||
| "permission": "ALLOW", | ||
| "principalType": "ROLE", | ||
| "principalId": "$owner" | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "name": "post", | ||
| "base": "PersistedModel", | ||
| "properties": { | ||
| "title": { | ||
| "type": "string" | ||
| }, | ||
| "content": { | ||
| "type": "string" | ||
| }, | ||
| "notes": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "relations": { | ||
| "user": { | ||
| "type": "belongsTo", | ||
| "model": "User" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| { | ||
| "port": 3000, | ||
| "host": "0.0.0.0", | ||
| "cookieSecret": "2d13a01d-44fb-455c-80cb-db9cb3cd3cd0", | ||
| "remoting": { | ||
| "json": { | ||
| "limit": "1kb", | ||
| "strict": false | ||
| }, | ||
| "urlencoded": { | ||
| "limit": "8kb" | ||
| } | ||
| }, | ||
| "legacyExplorer": false | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "db": { | ||
| "connector": "memory" | ||
| }, | ||
| "mail": { | ||
| "connector": "mail", | ||
| "transports": [ | ||
| {"type": "STUB"} | ||
| ] | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| { | ||
| "_meta": { | ||
| "sources": [ | ||
| "../common/models", | ||
| "./models" | ||
| ] | ||
| }, | ||
| "Email": { | ||
| "dataSource": "mail", | ||
| "public": false | ||
| }, | ||
| "User": { | ||
| "dataSource": "db", | ||
| "public": true, | ||
| "relations": { | ||
| "posts": { | ||
| "model": "post", | ||
| "type": "hasMany", | ||
| "foreignKey": "userId" | ||
| } | ||
| }, | ||
| "acls": [ | ||
| { | ||
| "permission": "ALLOW", | ||
| "principalType": "ROLE", | ||
| "principalId": "$owner" | ||
| } | ||
| ] | ||
| }, | ||
| "AccessToken": { | ||
| "dataSource": "db", | ||
| "public": false | ||
| }, | ||
| "ACL": { | ||
| "dataSource": "db", | ||
| "public": false | ||
| }, | ||
| "RoleMapping": { | ||
| "dataSource": "db", | ||
| "public": false | ||
| }, | ||
| "Role": { | ||
| "dataSource": "db", | ||
| "public": false | ||
| }, | ||
| "myUser": { | ||
| "dataSource": "db", | ||
| "public": true | ||
| }, | ||
| "blog": { | ||
| "dataSource": "db", | ||
| "public": true | ||
| }, | ||
| "post": { | ||
| "dataSource": "db", | ||
| "public": true | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| var loopback = require('../../../../index'); | ||
| var boot = require('loopback-boot'); | ||
| var app = module.exports = loopback(); | ||
| app.enableAuth(); | ||
| boot(app, __dirname); | ||
| app.use(loopback.token({model: app.models.AccessToken})); | ||
| var apiPath = '/api'; | ||
| app.use(apiPath, loopback.rest()); | ||
| app.use(loopback.urlNotFound()); | ||
| app.use(loopback.errorHandler()); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /*jshint -W030 */ | ||
| var loopback = require('../'); | ||
| var lt = require('loopback-testing'); | ||
| var path = require('path'); | ||
| var SIMPLE_APP = path.join(__dirname, 'fixtures', 'user-integration-app'); | ||
| var app = require(path.join(SIMPLE_APP, 'server/server.js')); | ||
| var expect = require('chai').expect; | ||
|
|
||
| describe('users - integration', function() { | ||
|
|
||
| lt.beforeEach.withApp(app); | ||
|
|
||
| before(function(done) { | ||
| // HACK: [rfeng] We have to reset the relations as they are polluted by | ||
| // other tests | ||
| app.models.User.hasMany(app.models.post); | ||
| app.models.User.hasMany(app.models.AccessToken, | ||
| {options: {disableInclude: true}}); | ||
| app.models.AccessToken.belongsTo(app.models.User); | ||
| app.models.User.destroyAll(function(err) { | ||
| if (err) return done(err); | ||
| app.models.post.destroyAll(function(err) { | ||
| if (err) return done(err); | ||
| app.models.blog.destroyAll(function(err) { | ||
| if (err) return done(err); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('base-user', function() { | ||
| var userId; | ||
| var accessToken; | ||
|
|
||
| it('should create a new user', function(done) { | ||
| var url = '/api/users'; | ||
|
|
||
| this.post(url) | ||
| .send({username: 'x', email: 'x@y.com', password: 'x'}) | ||
| .expect(200, function(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect(res.body.id).to.exist; | ||
| userId = res.body.id; | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should log into the user', function(done) { | ||
| var url = '/api/users/login'; | ||
|
|
||
| this.post(url) | ||
| .send({username: 'x', email: 'x@y.com', password: 'x'}) | ||
| .expect(200, function(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect(res.body.id).to.exist; | ||
| accessToken = res.body.id; | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should create post for a given user', function(done) { | ||
| var url = '/api/users/' + userId + '/posts?access_token=' + accessToken; | ||
| this.post(url) | ||
| .send({title: 'T1', content: 'C1'}) | ||
| .expect(200, function(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect(res.body.title).to.be.eql('T1'); | ||
| expect(res.body.content).to.be.eql('C1'); | ||
| expect(res.body.userId).to.be.eql(userId); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| // FIXME: [rfeng] The test is passing if run alone. But it fails with | ||
| // `npm test` as the loopback models are polluted by other tests | ||
| it('should prevent access tokens from being included', function(done) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is useless as it does not verify anything. If you per-app registry does not solve the problem of cross-test interference, then it's better to remove this test completely and use a different way to ensure that access tokens are not include-able by default.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does verify with the User/AccessToken relation setup (disableInclude = true), the accessTokens cannot be included via a user, even though the pollution from other tests forces me to use the hack to restore the relations to mimic the original configuration from the test fixture. I'm open to better ways to test the feature but it's better not to let the security hole drags too long.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My point is: if you remove
That's true. Let's land the fix with no unit tests then (just the small change in |
||
| var url = '/api/posts?filter={"include":{"user":"accessTokens"}}'; | ||
| this.get(url) | ||
| .expect(200, function(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect(res.body).to.have.property('length', 1); | ||
| var post = res.body[0]; | ||
| expect(post.user).to.have.property('username', 'x'); | ||
| expect(post.user).to.not.have.property('accessTokens'); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('sub-user', function() { | ||
| var userId; | ||
| var accessToken; | ||
|
|
||
| it('should create a new user', function(done) { | ||
| var url = '/api/myUsers'; | ||
|
|
||
| this.post(url) | ||
| .send({username: 'x', email: 'x@y.com', password: 'x'}) | ||
| .expect(200, function(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect(res.body.id).to.exist; | ||
| userId = res.body.id; | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should log into the user', function(done) { | ||
| var url = '/api/myUsers/login'; | ||
|
|
||
| this.post(url) | ||
| .send({username: 'x', email: 'x@y.com', password: 'x'}) | ||
| .expect(200, function(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect(res.body.id).to.exist; | ||
| accessToken = res.body.id; | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should create blog for a given user', function(done) { | ||
| var url = '/api/myUsers/' + userId + '/blogs?access_token=' + accessToken; | ||
| this.post(url) | ||
| .send({title: 'T1', content: 'C1'}) | ||
| .expect(200, function(err, res) { | ||
| if (err) { | ||
| console.error(err); | ||
| return done(err); | ||
| } | ||
| expect(res.body.title).to.be.eql('T1'); | ||
| expect(res.body.content).to.be.eql('C1'); | ||
| expect(res.body.userId).to.be.eql(userId); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should prevent access tokens from being included', function(done) { | ||
| var url = '/api/blogs?filter={"include":{"user":"accessTokens"}}'; | ||
| this.get(url) | ||
| .expect(200, function(err, res) { | ||
| if (err) { | ||
| return done(err); | ||
| } | ||
| expect(res.body).to.have.property('length', 1); | ||
| var blog = res.body[0]; | ||
| expect(blog.user).to.have.property('username', 'x'); | ||
| expect(blog.user).to.not.have.property('accessTokens'); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please build the app in
test/user.integration.jsand don't use loopback-boot?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? The purpose of the user.integration.js is to test a LB app's built-in user model.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you wrote yourself, there is nothing related to loopback-boot in the scenario you are testing.
It is a good practice to write the test code in such way that it's easy to quickly understand what is the scenario being tested. The test scenario should be also as minimal as possible and include only things that are absolutely necessary for the test.
By using a fixture defined in several files living outside of the test file, you are making it difficult to understand what is exactly being tested. By including lots of unrelated stuff in your fixture, you make it difficult to see what is the important (test-specific) part and what is just boilerplate. Last but not least, all these extra unneeded bits are making the maintenance more expensive.
This is already visible in this pull request, as the
server/server.jsfile is not following the project structure as scaffolded by recent versions of loopback-workspace.To test the built-in user model, you need just few lines of code:
I admit this setup is not as short as it should be, let's discuss the best API in #1323 and keep the code above for the purposes of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are more models such as
postandblogare needed. I'll merge the PR as is and defer the cleanup of tests.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am rather unhappy that you have added tests that always pass, regardless of whether the underlying implementation works or not. Such tests are giving false sense of security 👎