Skip to content
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

Fix repo search result #2600

Closed
wants to merge 2 commits into from
Closed

Conversation

daviian
Copy link
Member

@daviian daviian commented Sep 25, 2017

Targets #2562, #2399, #1794 and #2595 too.

fixes wrong search result.
Most of the changes are adding tests from @Morlinest

@tboerger tboerger added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Sep 25, 2017
@daviian daviian changed the title Fix repo unit permission check after ugprade from 1.1.4 to 1.2.0-rc3 Fix repo search result Sep 25, 2017
@daviian daviian force-pushed the fix-repo-units branch 2 times, most recently from 14ae44a to c35c3a9 Compare September 25, 2017 18:34
@daviian daviian changed the base branch from release/v1.2 to master September 25, 2017 18:34
@daviian daviian force-pushed the fix-repo-units branch 2 times, most recently from 4a60099 to e4f1f1e Compare September 25, 2017 19:28
@codecov-io
Copy link

codecov-io commented Sep 25, 2017

Codecov Report

Merging #2600 into master will increase coverage by 0.08%.
The diff coverage is 35.13%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #2600      +/-   ##
=========================================
+ Coverage   27.32%   27.4%   +0.08%     
=========================================
  Files          86      86              
  Lines       17143   17165      +22     
=========================================
+ Hits         4684    4704      +20     
- Misses      11781   11782       +1     
- Partials      678     679       +1
Impacted Files Coverage Δ
models/user.go 18.74% <100%> (+0.32%) ⬆️
models/repo_list.go 30.84% <22.22%> (+6.12%) ⬆️
models/org.go 69.36% <62.5%> (-0.45%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 339d7de...866c5cf. Read the comment docs.

@Morlinest
Copy link
Member

First, you are using my (older) tests, while PR with my tests is still pending (#2575). Second, you had problem with review of my changes to repo search because PR was too big but now you are doing the same. I am dividing that PR to multiple PRs one by one, now blocked only by aprooving and merging... So I don't understand (and don't like) this.

@daviian
Copy link
Member Author

daviian commented Sep 25, 2017

@Morlinest As I told you in a PM in discord. Your tests are great. And instead of pushing a whole refactoring and fix I only did a fix. Your refactoring should be added after this and all is fine.
And I even said you can take my changes I did on thursday. (which are exactly this)

@Morlinest
Copy link
Member

But why you are adding tests when I've already posted PR with tests only?

@daviian
Copy link
Member Author

daviian commented Sep 25, 2017

Because these are tests for the old code with Searcher as it is. And they are working.
The only difference is that they have the old search results. Like transitive ownership as described in your PR.
The code only removes non accessible repositories in list view,

@Morlinest
Copy link
Member

@daviian That tests contain it too. I think you missed it:

if testCase.opts.OwnerID > 0 {
	testCase.opts.Searcher = &User{ID: testCase.opts.OwnerID}
}

It should be same (old vs new version), only rewritten to testCases.

@daviian
Copy link
Member Author

daviian commented Sep 25, 2017

@Morlinest It was important for me to leave existing code as it is. Just to make it more easier to see that nothing has changed. A rewriting of tests doesn't necessarily mean that tests haven't changed.

And as you can see it was important for me to give credits to you too.
And moreover this PR doesn't make your PR's useless. They should be added nevertheless as yours make code a lot cleaner.

@Morlinest
Copy link
Member

I've created that tests, they are not in codebase, they have same results and improved. I haven't spent a little amount of time to do it. Nor first version of tests, nor second one.

@daviian
Copy link
Member Author

daviian commented Sep 25, 2017

@Morlinest Changed PR such that it totally uses your commit. And if that doesn't please you I can offer you to take my code from that PR and create your own PR. Or i remove your tests from this PR. What would you like?
I don't want anyone to be upset. Only to get 1.2 released.

@@ -46,6 +46,220 @@ func TestAPISearchRepoNotLogin(t *testing.T) {
assert.Contains(t, repo.Name, keyword)
assert.False(t, repo.Private)
}

// Should return all (max 50) public repositories
req = NewRequest(t, "GET", "/api/v1/repos/search?limit=50")
Copy link
Member

@ethantkoenig ethantkoenig Sep 25, 2017

Choose a reason for hiding this comment

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

This is a lot of duplicated code. Maybe we should add a helper function

func apiRepoSearchResult(t testing.T, params string, expectedLen int) *api.SearchResults
...
apiRepoSearchResult(t, "?limit=50", 12)

Even better, add a session parameter, so you can use it for the logged-in tests too.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ethantkoenig If you want that you can review #2575 as it rewrites tests. I wanted to change as few as possible to make reviewing easier, but if it is desired the mentioned PR does this ;-)

@@ -350,8 +350,11 @@ func GetOrgsByUserID(userID int64, showAll bool) ([]*User, error) {
return getOrgsByUserID(sess, userID, showAll)
}

func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
func getOwnedOrgsByUserID(sess *xorm.Session, userID int64, showAll bool) ([]*User, error) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: could we call it includePrivate or something similar so it is clearer what is included and unincluded when the parameter is false?

Copy link
Member Author

Choose a reason for hiding this comment

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

I called it showAll to be consistent with other methods used similarly. But I can change that at least for my PR.

assert.Len(t, repos, 3)

// Get all public repositories by name
repos, count, err = SearchRepositoryByName(&SearchRepoOptions{
Copy link
Member

Choose a reason for hiding this comment

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

This is a lot of duplicated code, maybe we should make this a table-driven test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Same as above ;-)

@Morlinest
Copy link
Member

@daviian You know I have PR for solving problems with this function :). Anyway, regardless of tests. I looked at your changes to implementation. It looks like something I was doing in first version of my PR, see dae998e. You are making same authorization decisions inside that shouldn't be there. I think more changes to current implementation with Searcher and OwnerID will not (can't) make it better.

My opinion:
Try to think about results. With your changes, if Searcher is not empty and you search for repositories of another user (OwnerID), you will get your own repositories and repositories of owned (or related) organizations + repositories owned by OwnerID. But if Searcher is empty, you will get repositories of OwnerID and repositories of organization owned by OwnerID. But I would expect to get just subset of repos from first scenario (when there is Searcher and OwnerID). In second scenario you will get some repositories from first case, but also some repositories that aren't returned in first scenario. Also it doesn't solve problem, when Searcher is set but OwnerID is not.

@daviian
Copy link
Member Author

daviian commented Sep 26, 2017

@Morlinest
First of all, I don't want to change existing search behaviour, that is way out of scope. PR should only remove repositories a user has no access to. That's why I don't break down the whole search function.
And because it shouldn't be there you should nevertheless refactor that stuff in your PR after that.
Second as I commented in code: If Searcher is not empty it means that OwnerID = SearcherID

And last but not least and as I said multiple times: This PR doesn't replace your refactoring, which includes changing search behaviour. It is only an interim step to fix permission problems.

opts.Searcher.ID, opts.Searcher.ID))
}
} else if opts.OwnerID > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Not needed. It's already inside opts.OwnerID > 0 block.

return nil, 0, fmt.Errorf("User: %v", err)
}

err = owner.GetOwnedOrganizations(false)
Copy link
Member

Choose a reason for hiding this comment

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

Authorization decision making.

if err != nil {
return nil, 0, fmt.Errorf("Organization: %v", err)
}

Copy link
Member

Choose a reason for hiding this comment

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

Missing collaborate repos.

}
searcherReposCond = searcherReposCond.Or(builder.In("owner_id", ownerIds))
Copy link
Member

Choose a reason for hiding this comment

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

Already in condition, line 157.

@daviian
Copy link
Member Author

daviian commented Sep 26, 2017

Closing PR as there exist scenarios in which this only solves a small portion of the search problem.
So I see that we don't come around a not that small refactoring of search.

But I want to point out that search should be fixed asap it is a massive security issue IMO.

@daviian daviian closed this Sep 26, 2017
@daviian daviian deleted the fix-repo-units branch September 26, 2017 20:44
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants