Skip to content

Commit

Permalink
lxd: Add strict backup name requirements
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Bolton <[email protected]>
  • Loading branch information
boltmark committed Sep 4, 2024
1 parent ccbbddd commit 2ac18b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
14 changes: 13 additions & 1 deletion lxd/storage/backend_lxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"slices"
"strings"
"sync"
Expand Down Expand Up @@ -7844,8 +7845,19 @@ func (b *lxdBackend) CreateBucketFromBackup(srcBackup backup.Info, srcData io.Re
revert := revert.New()
defer revert.Fail()

// Validate the name
srcBackupName := srcBackup.Name
match, err := regexp.MatchString(`^[a-z0-9][\-a-z0-9]{2,62}$`, srcBackupName)
if err != nil {
return err
}

if !match {
return fmt.Errorf("Backup name must be between 3 and 63 lowercase letters, numbers, or hyphens and must start with a letter or number")
}

bucketRequest := api.StorageBucketsPost{
Name: srcBackup.Name,
Name: srcBackupName,
StorageBucketPut: srcBackup.Config.Bucket.Writable(),
}

Expand Down
13 changes: 10 additions & 3 deletions lxd/storage_buckets_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -564,8 +565,14 @@ func storagePoolBucketBackupPost(d *Daemon, r *http.Request) response.Response {
}

// Validate the name
if strings.Contains(req.Name, "/") {
return response.BadRequest(fmt.Errorf("Backup names may not contain slashes"))
newBackupName := req.Name
match, err := regexp.MatchString(`^[a-z0-9][\-a-z0-9]{2,62}$`, newBackupName)
if err != nil {
return response.BadRequest(err)
}

if !match {
return response.BadRequest(fmt.Errorf("Backup name must be between 3 and 63 lowercase letters, numbers, or hyphens and must start with a letter or number"))
}

oldName := bucketName + shared.SnapshotDelimiter + backupName
Expand All @@ -575,7 +582,7 @@ func storagePoolBucketBackupPost(d *Daemon, r *http.Request) response.Response {
return response.SmartError(err)
}

newName := backup.BucketName() + shared.SnapshotDelimiter + req.Name
newName := backup.BucketName() + shared.SnapshotDelimiter + newBackupName

rename := func(op *operations.Operation) error {
err := backup.Rename(newName)
Expand Down

0 comments on commit 2ac18b6

Please sign in to comment.