Skip to content

Commit 96122b7

Browse files
authored
validate model tags on copy (ollama#1323)
1 parent 39be7fd commit 96122b7

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

server/modelpath.go

+14
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ func ParseModelPath(name string) ModelPath {
6767
return mp
6868
}
6969

70+
var errModelPathInvalid = errors.New("invalid model path")
71+
72+
func (mp ModelPath) Validate() error {
73+
if mp.Repository == "" {
74+
return fmt.Errorf("%w: model repository name is required", errModelPathInvalid)
75+
}
76+
77+
if strings.Contains(mp.Tag, ":") {
78+
return fmt.Errorf("%w: ':' (colon) is not allowed in tag names", errModelPathInvalid)
79+
}
80+
81+
return nil
82+
}
83+
7084
func (mp ModelPath) GetNamespaceRepository() string {
7185
return fmt.Sprintf("%s/%s", mp.Namespace, mp.Repository)
7286
}

server/routes.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,8 @@ func CreateModelHandler(c *gin.Context) {
416416
return
417417
}
418418

419-
if strings.Count(req.Name, ":") > 1 {
420-
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "':' (colon) is not allowed in tag names"})
419+
if err := ParseModelPath(req.Name).Validate(); err != nil {
420+
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
421421
return
422422
}
423423

@@ -645,6 +645,11 @@ func CopyModelHandler(c *gin.Context) {
645645
return
646646
}
647647

648+
if err := ParseModelPath(req.Destination).Validate(); err != nil {
649+
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
650+
return
651+
}
652+
648653
if err := CopyModel(req.Source, req.Destination); err != nil {
649654
if os.IsNotExist(err) {
650655
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("model '%s' not found", req.Source)})

0 commit comments

Comments
 (0)