Skip to content

Commit

Permalink
validate model path
Browse files Browse the repository at this point in the history
  • Loading branch information
mxyng committed Aug 28, 2024
1 parent 6c1c1ad commit d9d50c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
18 changes: 5 additions & 13 deletions server/modelpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,6 @@ func ParseModelPath(name string) ModelPath {

var errModelPathInvalid = errors.New("invalid model path")

func (mp ModelPath) Validate() error {
if mp.Repository == "" {
return fmt.Errorf("%w: model repository name is required", errModelPathInvalid)
}

if strings.Contains(mp.Tag, ":") {
return fmt.Errorf("%w: ':' (colon) is not allowed in tag names", errModelPathInvalid)
}

return nil
}

func (mp ModelPath) GetNamespaceRepository() string {
return fmt.Sprintf("%s/%s", mp.Namespace, mp.Repository)
}
Expand All @@ -105,7 +93,11 @@ func (mp ModelPath) GetShortTagname() string {

// GetManifestPath returns the path to the manifest file for the given model path, it is up to the caller to create the directory if it does not exist.
func (mp ModelPath) GetManifestPath() (string, error) {
return filepath.Join(envconfig.Models(), "manifests", mp.Registry, mp.Namespace, mp.Repository, mp.Tag), nil
if p := filepath.Join(mp.Registry, mp.Namespace, mp.Repository, mp.Tag); filepath.IsLocal(p) {
return filepath.Join(envconfig.Models(), "manifests", p), nil
}

return "", errModelPathInvalid
}

func (mp ModelPath) BaseURL() *url.URL {
Expand Down
8 changes: 8 additions & 0 deletions server/modelpath_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"errors"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -154,3 +155,10 @@ func TestParseModelPath(t *testing.T) {
})
}
}

func TestInsecureModelpath(t *testing.T) {
mp := ParseModelPath("../../..:something")
if _, err := mp.GetManifestPath(); !errors.Is(err, errModelPathInvalid) {
t.Errorf("expected error: %v", err)
}
}

0 comments on commit d9d50c4

Please sign in to comment.