Skip to content

fix(model): apply scopes properly, closes #9087 - #9252

Closed
yocontra wants to merge 3 commits into
sequelize:masterfrom
yocontra:fix-scopes
Closed

yocontra wants to merge 3 commits into
sequelize:masterfrom
yocontra:fix-scopes

Conversation

@yocontra

Copy link
Copy Markdown
Contributor

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Information: #9087 (comment)

I would recommend backporting this to v4 as well since this is a security issue. This would be hard/impossible to exploit in a targeted way so I don't think it deserves a CVE or private disclosure, but this could be a bad leak of sensitive fields for some people.

@sushantdhiman

Copy link
Copy Markdown
Contributor

Closing this PR, Added #9087 to v5 milestone so this and other related scope issues will also be a focus of v5 release

@yocontra

Copy link
Copy Markdown
Contributor Author

@sushantdhiman What was wrong with the PR? I think it solved the issue pretty simply.

@sushantdhiman

Copy link
Copy Markdown
Contributor

Hmm, ok lets merge this as it solves scoped attributes issue at least. We can fix scopes later on

@sushantdhiman sushantdhiman reopened this May 30, 2018
});
});

it('should be applied before query options for attributes exclude', function() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its worth adding a test for case when scope is trying to exclude some attribute but actual call is trying to include it. Which again comes to a discussion matter, I think scope should still exclude that attribute

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sushantdhiman The test below this one does that (should be applied before query options for attributes include)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lowAccess is only setting attributes to ['other_value', 'access_level'],, its not excluding email, so shouldn't include at method level should be able to inject email to attributes?

I think it would make sense if scope say exclude something then finder method should not be allowed to include that attribute, if scopes are just specifying attributes or using include, then finder method should be able to expand that list of attributes

@yocontra yocontra May 31, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sushantdhiman I think I see what you're asking for, I'll add that test but it does function like that in both cases - includes only applies to the sum output of the previous scope, so in both cases you can't include an attribute if the scope had it in exclude or it wasn't in attributes.

@yocontra

Copy link
Copy Markdown
Contributor Author

@sushantdhiman Any update on this?

@papb

papb commented Jul 17, 2018

Copy link
Copy Markdown
Member

Hello @contra, thank you again for taking a look in my issue. Unfortunately, this PR does not solve it.

Looks like you're fixing something else with this PR though, so that's great :) Please just remove the "closes 9087" thing from the PR / commit, since it actually does not close it at all...

If you disagree and/or would like further clarifications, let me know.

@sushantdhiman

Copy link
Copy Markdown
Contributor

@contra I would like to wait for formal api discussion about scopes first, I don't use scopes in my own projects so I am not sure what works best here. Hopefully #9087 will result in an API proposal that works best for everyone, till then I am holding off any changes related to scope system

@yocontra

yocontra commented Jul 17, 2018 •

Copy link
Copy Markdown
Contributor Author

@papb Can you clarify how this doesn't fix #9087? If you could provide a test case that fails that would be ideal. I can make sure to include the test in the PR and solve that as well, if it isn't already.

@papb

papb commented Jul 18, 2018

Copy link
Copy Markdown
Member

@contra Sure. The very first comment of the issue itself, for example, still behaves exactly as I posted.

Converting my post to a test is fairly straightforward:

beforeEach(function() {
    this.Foo = this.sequelize.define('foo', { name: Sequelize.STRING }, { timestamps: false });
    this.Bar = this.sequelize.define('bar', { name: Sequelize.STRING }, { timestamps: false });
    this.Foo.hasMany(this.Bar, { foreignKey: 'fooId' });
    this.Foo.addScope('testScope', {
        include: [{
            model: this.Bar,
            where: {
                name: 'bar1'
            }
        }]
    });
});

it('should merge scope with findAll options correctly', function() {
    const self = this;
    return this.sequelize.sync({ force: true })
        .then(() => self.Foo.create({ name: 'foo1' }))
        .then(foo1 => this.Bar.create({ name: 'bar1', fooId: foo1.id }))
        .then(() => self.Foo.create({ name: 'foo2' }))
        .then(foo2 => this.Bar.create({ name: 'bar2', fooId: foo2.id }))
        .then(() => self.Foo.scope('testScope').findAll({
            include: [{
                model: this.Bar,
                attributes: { exclude: ['name'] }
            }]
        }))
        .then(result => {
            result = result.map(x => x.toJSON());
            expect(result).to.deep.equal([
                {
                    'id': 1,
                    'name': 'foo1',
                    'bars': [
                        {
                            'id': 1,
                            'fooId': 1
                        }
                    ]
                }
            ]);
    });
});

Also, my other comment with extra examples is also not covered so far. There I show three situations, which I call situations A, B and C, that should all have the same output (the output from A) but currently have different outputs. Another great test to be included would be a test checking those exact three situations for the same output (the one from A).

It would be great if you could add those tests to your PR and make them pass! Thank you very much once more for taking the time. If you need further clarifications, let me know.

@yocontra

Copy link
Copy Markdown
Contributor Author

@papb Thanks for those - will add those and work on getting them to pass when I have a spare minute.

@papb papb mentioned this pull request Sep 10, 2018
4 of 5 tasks
@sushantdhiman

Copy link
Copy Markdown
Contributor

Fixed by #9087

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants