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

Check permissions on repo lookup #2358

Merged
merged 4 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions server/api/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -277,21 +276,7 @@ func ChownRepo(c *gin.Context) {
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
// @Param repo_full_name path string true "the repository full-name / slug"
func LookupRepo(c *gin.Context) {
_store := store.FromContext(c)
repoFullName := strings.TrimLeft(c.Param("repo_full_name"), "/")

repo, err := _store.GetRepoName(repoFullName)
if err != nil {
if errors.Is(err, types.RecordNotExist) {
c.AbortWithStatus(http.StatusNotFound)
return
}

_ = c.AbortWithError(http.StatusInternalServerError, err)
return
}

c.JSON(http.StatusOK, repo)
c.JSON(http.StatusOK, session.Repo(c))
}

// GetRepo
Expand Down
2 changes: 1 addition & 1 deletion server/router/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func apiRoutes(e *gin.RouterGroup) {
}
}

apiBase.GET("/repos/lookup/*repo_full_name", api.LookupRepo) // TODO: check if this public route is a security issue
apiBase.GET("/repos/lookup/*repo_full_name", session.SetRepo(), session.SetPerm(), session.MustPull, api.LookupRepo)
apiBase.POST("/repos", session.MustUser(), api.PostRepo)
repoBase := apiBase.Group("/repos/:repo_id")
{
Expand Down
18 changes: 7 additions & 11 deletions server/router/middleware/session/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"
"net/http"
"strconv"
"strings"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -45,11 +46,10 @@ func Repo(c *gin.Context) *model.Repo {
func SetRepo() gin.HandlerFunc {
return func(c *gin.Context) {
var (
_store = store.FromContext(c)
owner = c.Param("owner")
name = c.Param("name")
_repoID = c.Param("repo_id")
user = User(c)
_store = store.FromContext(c)
fullName = strings.TrimLeft(c.Param("repo_full_name"), "/")
_repoID = c.Param("repo_id")
user = User(c)
)

var repo *model.Repo
Expand All @@ -63,7 +63,7 @@ func SetRepo() gin.HandlerFunc {
}
repo, err = _store.GetRepo(repoID)
} else {
repo, err = _store.GetRepoName(owner + "/" + name)
repo, err = _store.GetRepoName(fullName)
}

if repo != nil {
Expand All @@ -73,11 +73,7 @@ func SetRepo() gin.HandlerFunc {
}

// debugging
log.Debug().Msgf("Cannot find repository %s/%s. %s",
owner,
name,
err.Error(),
)
log.Debug().Err(err).Msgf("Cannot find repository %s.", fullName)

if user == nil {
c.AbortWithStatus(http.StatusUnauthorized)
Expand Down