Skip to content

Commit

Permalink
use PathEscape
Browse files Browse the repository at this point in the history
  • Loading branch information
neolynx committed Oct 10, 2024
1 parent 0403480 commit 83e9c0b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
14 changes: 7 additions & 7 deletions api/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aptly-dev/aptly/deb"
"github.com/aptly-dev/aptly/pgp"
"github.com/aptly-dev/aptly/task"
"github.com/aptly-dev/aptly/utils"

Check failure on line 12 in api/publish.go

View workflow job for this annotation

GitHub Actions / Test (Ubuntu 22.04)

"github.com/aptly-dev/aptly/utils" imported and not used
"github.com/gin-gonic/gin"
)

Expand Down Expand Up @@ -43,11 +44,10 @@ func getSigner(options *SigningOptions) (pgp.Signer, error) {
return signer, nil
}

// Replace '_' with '/' and double '__' with single '_', remove leading '/', remove '..'
func parseEscapedPath(path string) string {
// Replace '_' with '/' and double '__' with single '_', pathEscape
func slashEscape(path string) string {
result := strings.Replace(strings.Replace(path, "_", "/", -1), "//", "_", -1)
result = strings.Replace(result, "..", "", -1)
result = strings.TrimPrefix(result, "/")
result = PathEscape(result)

Check failure on line 50 in api/publish.go

View workflow job for this annotation

GitHub Actions / Test (Ubuntu 22.04)

undefined: PathEscape
if result == "" {
result = "."
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func apiPublishList(c *gin.Context) {

// POST /publish/:prefix
func apiPublishRepoOrSnapshot(c *gin.Context) {
param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)

var b struct {
Expand Down Expand Up @@ -250,7 +250,7 @@ func apiPublishRepoOrSnapshot(c *gin.Context) {

// PUT /publish/:prefix/:distribution
func apiPublishUpdateSwitch(c *gin.Context) {
param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := c.Params.ByName("distribution")

Expand Down Expand Up @@ -375,7 +375,7 @@ func apiPublishDrop(c *gin.Context) {
force := c.Request.URL.Query().Get("force") == "1"
skipCleanup := c.Request.URL.Query().Get("SkipCleanup") == "1"

param := parseEscapedPath(c.Params.ByName("prefix"))
param := slashEscape(c.Params.ByName("prefix"))
storage, prefix := deb.ParsePrefix(param)
distribution := c.Params.ByName("distribution")

Expand Down
2 changes: 1 addition & 1 deletion api/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func apiReposIncludePackageFromDir(c *gin.Context) {

var sources []string
var taskName string
dirParam := c.Params.ByName("dir")
dirParam := PathEscape(c.Params.ByName("dir"))

Check failure on line 623 in api/repos.go

View workflow job for this annotation

GitHub Actions / Test (Ubuntu 22.04)

undefined: PathEscape
fileParam := c.Params.ByName("file")
if fileParam != "" && !verifyPath(fileParam) {
AbortWithJSONError(c, 400, fmt.Errorf("wrong file"))
Expand Down
8 changes: 8 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package utils
import (
"fmt"
"os"
"strings"

"golang.org/x/sys/unix"
)
Expand All @@ -22,3 +23,10 @@ func DirIsAccessible(filename string) error {
}
return nil
}

// Remove leading '/', remove '..'
func PathEscape(path string) (result string) {
result = strings.Replace(path, "..", "", -1)
result = strings.TrimPrefix(result, "/")
return
}

0 comments on commit 83e9c0b

Please sign in to comment.