Skip to content

Commit d963932

Browse files
caarlos0Tomer-PL
andauthored
Merge commit from fork
* sec: escape ansi sequences on user input fixes HSA-fv2r-r8mp-pg48 Signed-off-by: Carlos Alexandro Becker <[email protected]> * Apply suggestion from @Tomer-PL Co-authored-by: Tomer Fichman <[email protected]> * chore: fmt Signed-off-by: Carlos Alexandro Becker <[email protected]> --------- Signed-off-by: Carlos Alexandro Becker <[email protected]> Co-authored-by: Tomer Fichman <[email protected]>
1 parent ea8799b commit d963932

File tree

7 files changed

+22
-4
lines changed

7 files changed

+22
-4
lines changed

pkg/backend/access_token.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77

88
"github.com/charmbracelet/soft-serve/pkg/db"
99
"github.com/charmbracelet/soft-serve/pkg/proto"
10+
"github.com/charmbracelet/soft-serve/pkg/utils"
1011
)
1112

1213
// CreateAccessToken creates an access token for user.
1314
func (b *Backend) CreateAccessToken(ctx context.Context, user proto.User, name string, expiresAt time.Time) (string, error) {
1415
token := GenerateToken()
1516
tokenHash := HashToken(token)
17+
name = utils.Sanitize(name)
1618

1719
if err := b.db.TransactionContext(ctx, func(tx *db.Tx) error {
1820
_, err := b.store.CreateAccessToken(ctx, tx, name, user.ID(), tokenHash, expiresAt)

pkg/backend/repo.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ func (d *Backend) SetHidden(ctx context.Context, name string, hidden bool) error
544544
// It implements backend.Backend.
545545
func (d *Backend) SetDescription(ctx context.Context, name string, desc string) error {
546546
name = utils.SanitizeRepo(name)
547+
desc = utils.Sanitize(desc)
547548
rp := filepath.Join(d.repoPath(name))
548549

549550
// Delete cache
@@ -617,6 +618,7 @@ func (d *Backend) SetPrivate(ctx context.Context, name string, private bool) err
617618
// It implements backend.Backend.
618619
func (d *Backend) SetProjectName(ctx context.Context, repo string, name string) error {
619620
repo = utils.SanitizeRepo(repo)
621+
name = utils.Sanitize(name)
620622

621623
// Delete cache
622624
d.cache.Delete(repo)

pkg/backend/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ func (d *Backend) AddPublicKey(ctx context.Context, username string, pk ssh.Publ
279279
//
280280
// It implements backend.Backend.
281281
func (d *Backend) CreateUser(ctx context.Context, username string, opts proto.UserOptions) (proto.User, error) {
282+
username = utils.Sanitize(username)
282283
username = strings.ToLower(username)
283284
if err := utils.ValidateUsername(username); err != nil {
284285
return nil, err

pkg/backend/webhooks.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/charmbracelet/soft-serve/pkg/db/models"
1010
"github.com/charmbracelet/soft-serve/pkg/proto"
1111
"github.com/charmbracelet/soft-serve/pkg/store"
12+
"github.com/charmbracelet/soft-serve/pkg/utils"
1213
"github.com/charmbracelet/soft-serve/pkg/webhook"
1314
"github.com/google/uuid"
1415
)
@@ -17,6 +18,7 @@ import (
1718
func (b *Backend) CreateWebhook(ctx context.Context, repo proto.Repository, url string, contentType webhook.ContentType, secret string, events []webhook.Event, active bool) error {
1819
dbx := db.FromContext(ctx)
1920
datastore := store.FromContext(ctx)
21+
url = utils.Sanitize(url)
2022

2123
return dbx.TransactionContext(ctx, func(tx *db.Tx) error {
2224
lastID, err := datastore.CreateWebhook(ctx, tx, repo.ID(), url, secret, int(contentType), active)

pkg/ssh/cmd/commit.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/charmbracelet/soft-serve/pkg/backend"
1111
"github.com/charmbracelet/soft-serve/pkg/ui/common"
1212
"github.com/charmbracelet/soft-serve/pkg/ui/styles"
13+
"github.com/charmbracelet/soft-serve/pkg/utils"
1314
"github.com/spf13/cobra"
1415
)
1516

@@ -59,9 +60,9 @@ func commitCommand() *cobra.Command {
5960

6061
s := strings.Builder{}
6162
commitLine := "commit " + commitSHA
62-
authorLine := "Author: " + commit.Author.Name
63+
authorLine := "Author: " + utils.Sanitize(commit.Author.Name)
6364
dateLine := "Date: " + commit.Committer.When.UTC().Format(time.UnixDate)
64-
msgLine := strings.ReplaceAll(commit.Message, "\r\n", "\n")
65+
msgLine := strings.ReplaceAll(utils.Sanitize(commit.Message), "\r\n", "\n")
6566
statsLine := renderStats(diff, commonStyle, color)
6667
diffLine := renderDiff(patch, color)
6768

pkg/ssh/cmd/webhooks.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/charmbracelet/lipgloss/v2/table"
99
"github.com/charmbracelet/soft-serve/pkg/backend"
10+
"github.com/charmbracelet/soft-serve/pkg/utils"
1011
"github.com/charmbracelet/soft-serve/pkg/webhook"
1112
"github.com/dustin/go-humanize"
1213
"github.com/google/uuid"
@@ -69,7 +70,7 @@ func webhookListCommand() *cobra.Command {
6970

7071
table = table.Row(
7172
strconv.FormatInt(h.ID, 10),
72-
h.URL,
73+
utils.Sanitize(h.URL),
7374
strings.Join(events, ","),
7475
strconv.FormatBool(h.Active),
7576
humanize.Time(h.CreatedAt),
@@ -122,7 +123,8 @@ func webhookCreateCommand() *cobra.Command {
122123
return webhook.ErrInvalidContentType
123124
}
124125

125-
return be.CreateWebhook(ctx, repo, strings.TrimSpace(args[1]), ct, secret, evs, active)
126+
url := utils.Sanitize(args[1])
127+
return be.CreateWebhook(ctx, repo, strings.TrimSpace(url), ct, secret, evs, active)
126128
},
127129
}
128130

pkg/utils/utils.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"path"
66
"strings"
77
"unicode"
8+
9+
"github.com/charmbracelet/x/ansi"
810
)
911

1012
// SanitizeRepo returns a sanitized version of the given repository name.
1113
func SanitizeRepo(repo string) string {
14+
repo = Sanitize(repo)
1215
// We need to use an absolute path for the path to be cleaned correctly.
1316
repo = strings.TrimPrefix(repo, "/")
1417
repo = "/" + repo
@@ -20,6 +23,11 @@ func SanitizeRepo(repo string) string {
2023
return repo[1:]
2124
}
2225

26+
// Sanitize strips ANSI escape codes from the given string.
27+
func Sanitize(s string) string {
28+
return ansi.Strip(s)
29+
}
30+
2331
// ValidateUsername returns an error if any of the given usernames are invalid.
2432
func ValidateUsername(username string) error {
2533
if username == "" {

0 commit comments

Comments
 (0)