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

Prevent addition of labels from outside the repository or organisation in issues #14912

Merged
merged 22 commits into from
Mar 19, 2021

Conversation

zeripath
Copy link
Contributor

@zeripath zeripath commented Mar 6, 2021

It has been possible to add labels to issues that do not belong to the repository or organisation as there were insufficient checks on this process.

This PRs enforces these checks and adds comments to the basic functions involved that ensure that future users will need to check.

It adds a migration to the system that simply deletes these issue_labels and comments - however, clearly this is not ideal as we should really attempt to match the labels with labels that are available. A perhaps better migration would also add the necessary labels to the repository.

On repository transfer all organization labels and comments related will be removed. I've decided this because: a) it's simpler and b) it prevents leaking of organization labels

It adds a consistency check to find these labels.

Fix #14908

Signed-off-by: Andrew Thornton art27@cantab.net

@zeripath zeripath added type/feature Completely new functionality. Can only be merged if feature freeze is not active. type/refactoring Existing code has been cleaned up. There should be no new functionality. labels Mar 6, 2021
@zeripath zeripath added this to the 1.14.0 milestone Mar 6, 2021
@zeripath zeripath force-pushed the fix-14908-labels-not-in-repository branch from 756e98e to bc891c0 Compare March 6, 2021 18:23
@CirnoT
Copy link
Contributor

CirnoT commented Mar 7, 2021

Excuse me for sounding dumb, but what is the point of this doctor check? Neither API nor web interface endpoints gate the label added to belong to repo or org. I can send API request or change data-id in DOM to point to outside label and the Gitea will be more than happy to perform the requested action.

Sounds to me like we need to add the appropriate check in code and solve this in single-time migration, rather than in Doctor check.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 7, 2021
@zeripath
Copy link
Contributor Author

zeripath commented Mar 7, 2021

Thanks @CirnoT I had presumed that the rest of the code would be doing the checks especially given the issue report which stated that all that was needed was a db consistency check.

Clearly they were not.

This is a much larger task than first thought.

@zeripath zeripath changed the title Add outside label consistency check into doctor Refuse to create outside labels in issues Mar 7, 2021
@zeripath zeripath changed the title Refuse to create outside labels in issues Prevent addition of labels from outside the repository or organisation in issues Mar 7, 2021
@zeripath zeripath changed the title Prevent addition of labels from outside the repository or organisation in issues WIP: Prevent addition of labels from outside the repository or organisation in issues Mar 7, 2021
@zeripath zeripath added the pr/wip This PR is not ready for review label Mar 7, 2021
@noerw
Copy link
Member

noerw commented Mar 7, 2021

Excuse me for sounding dumb, but what is the point of this doctor check? Neither API nor web interface endpoints gate the label added to belong to repo or org. I can send API request or change data-id in DOM to point to outside label and the Gitea will be more than happy to perform the requested action.

Sounds to me like we need to add the appropriate check in code and solve this in single-time migration, rather than in Doctor check.

newIssue() guards against foreign labels:

gitea/models/issue.go

Lines 937 to 940 in 9b261f5

// Silently drop invalid labels.
if label.RepoID != opts.Repo.ID && label.OrgID != opts.Repo.OwnerID {
continue
}

..and newly created issues had these foreign labels attached too. That's what confused me so much when I tried to investigate a couple days ago

@zeripath
Copy link
Contributor Author

zeripath commented Mar 7, 2021

@noerw I think it depends on how these labels are being added - I suspect there is another issue here too. Hence the WIP.

I think this one needs actual testing.

Easiest way I've found to test any PR:

git fetch git@github:go-gitea/gitea refs/pull/14912/head:pr-14912
git checkout pr-14912

I have a little script:

#!/bin/sh
git fetch -f git@github.com:go-gitea/gitea refs/pull/$1/head:pr-$1
git checkout pr-$1

@lunny
Copy link
Member

lunny commented Mar 7, 2021

Should we copy or delete the org labels when transfer a repository? It's a question.

However, the transfer of repository should become a task running in background like migrations and there is a middle state BeingTransfering.

Move the refactor of Check outside the PR will make the code more clear.

@@ -632,6 +632,8 @@ func HasIssueLabel(issueID, labelID int64) bool {
return hasIssueLabel(x, issueID, labelID)
}

// newIssueLabel this function creates a new label it does not check if the label is valid for the issue
// YOU MUST CHECK THIS BEFORE THIS FUNCTION
Copy link
Member

Choose a reason for hiding this comment

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

diff --git a/models/issue_label.go b/models/issue_label.go
index 54b286fe7..2da0d2f20 100644
--- a/models/issue_label.go
+++ b/models/issue_label.go
@@ -632,7 +632,43 @@ func HasIssueLabel(issueID, labelID int64) bool {
        return hasIssueLabel(x, issueID, labelID)
 }

+// ErrAddIssueLabelNotAllowed represents the error adding the label to issue not allowed
+type ErrAddIssueLabelNotAllowed struct {
+       LabelID     int64
+       LabelRepoID int64
+       LabelOrgID  int64
+       IssueID     int64
+       IssueRepoID int64
+       IssueOrgID  int64
+}
+
+func (err ErrAddIssueLabelNotAllowed) Error() string {
+       return fmt.Sprintf("Add label %d[%d,%d] to issue/pr %d failed because it's not belong respository %d or orgnization %d",
+               err.LabelID, err.LabelRepoID, err.LabelOrgID,
+               err.IssueID,
+               err.IssueRepoID,
+               err.IssueOrgID,
+       )
+}
+
 func newIssueLabel(e *xorm.Session, issue *Issue, label *Label, doer *User) (err error) {
+       if label.RepoID > 0 && label.RepoID != issue.RepoID {
+               return ErrAddIssueLabelNotAllowed{
+                       LabelID:     label.ID,
+                       LabelRepoID: label.RepoID,
+                       LabelOrgID:  label.OrgID,
+                       IssueID:     issue.ID,
+                       IssueRepoID: issue.RepoID,
+               }
+       } else if label.OrgID > 0 && label.OrgID != issue.Repo.OwnerID {
+               return ErrAddIssueLabelNotAllowed{
+                       LabelID:     label.ID,
+                       LabelRepoID: label.RepoID,
+                       LabelOrgID:  label.OrgID,
+                       IssueID:     issue.ID,
+                       IssueOrgID:  issue.Repo.OwnerID,
+               }
+       }
        if _, err = e.Insert(&IssueLabel{
                IssueID: issue.ID,
                LabelID: label.ID,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's gonna need to be added to a lot more places as there are lots of other places we silently drop these labels.

@zeripath
Copy link
Contributor Author

zeripath commented Mar 7, 2021

Should we copy or delete the org labels when transfer a repository? It's a question.

I think delete - at least for this PR. Copying and reassigning is really complex. The only issue is the migration - perhaps the migration should be dropped.

However, the transfer of repository should become a task running in background like migrations and there is a middle state BeingTransfering.

Not this PR.

Move the refactor of Check outside the PR will make the code more clear.

Done.

…org labels on transfer

Prevent the addition of labels from outside of the repository or
organisation and remove organisation labels on transfer.

Related go-gitea#14908

Signed-off-by: Andrew Thornton <art27@cantab.net>
zeripath and others added 10 commits March 8, 2021 16:44
Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
…m:zeripath/gitea into fix-14908-labels-not-in-repository-part-1
Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
Signed-off-by: Andrew Thornton <art27@cantab.net>
@zeripath zeripath force-pushed the fix-14908-labels-not-in-repository branch from 6b67724 to ba378ec Compare March 8, 2021 22:16
zeripath added 3 commits March 9, 2021 13:15
Signed-off-by: Andrew Thornton <art27@cantab.net>
This PR adds another consistency check into doctor in order to detect
labels that have been added from outside of repositories and organisations

Fix go-gitea#14908

Signed-off-by: Andrew Thornton <art27@cantab.net>
@zeripath zeripath force-pushed the fix-14908-labels-not-in-repository branch from ba378ec to 0c8c90d Compare March 11, 2021 12:18
@zeripath zeripath changed the title WIP: Prevent addition of labels from outside the repository or organisation in issues Prevent addition of labels from outside the repository or organisation in issues Mar 11, 2021
@zeripath zeripath removed the pr/wip This PR is not ready for review label Mar 11, 2021
Signed-off-by: Andrew Thornton <art27@cantab.net>
@zeripath zeripath added type/bug and removed type/refactoring Existing code has been cleaned up. There should be no new functionality. labels Mar 11, 2021
@zeripath
Copy link
Contributor Author

zeripath commented Mar 18, 2021

conflicts fixed.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 19, 2021
@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 19, 2021
@lunny lunny merged commit dace0ce into go-gitea:master Mar 19, 2021
@zeripath zeripath deleted the fix-14908-labels-not-in-repository branch March 19, 2021 20:47
@6543 6543 mentioned this pull request Mar 22, 2021
6543 pushed a commit that referenced this pull request Mar 22, 2021
There is a serious issue with the v176 migration where there is a mistaken missing
label_id selection.

*introduced by #14912*

Signed-off-by: Andrew Thornton <art27@cantab.net>
@go-gitea go-gitea locked and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/bug type/feature Completely new functionality. Can only be merged if feature freeze is not active.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A new doctor check should be added that an issue should not be added a label belong to another repository.
7 participants