Skip to content

Commit eb7ae88

Browse files
authored
Merge branch 'main' into pprof-labels-stacks
2 parents 4de8423 + 43332a4 commit eb7ae88

File tree

18 files changed

+134
-205
lines changed

18 files changed

+134
-205
lines changed

docker/root/etc/s6/openssh/setup

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ if [ -d /etc/ssh ]; then
4949
SSH_DSA_CERT="${SSH_DSA_CERT:+"HostCertificate "}${SSH_DSA_CERT}" \
5050
SSH_MAX_STARTUPS="${SSH_MAX_STARTUPS:+"MaxStartups "}${SSH_MAX_STARTUPS}" \
5151
SSH_MAX_SESSIONS="${SSH_MAX_SESSIONS:+"MaxSessions "}${SSH_MAX_SESSIONS}" \
52+
SSH_LOG_LEVEL=${SSH_LOG_LEVEL:-"INFO"} \
5253
envsubst < /etc/templates/sshd_config > /etc/ssh/sshd_config
5354

5455
chmod 0644 /etc/ssh/sshd_config

docker/root/etc/templates/sshd_config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ListenAddress ::
88
${SSH_MAX_STARTUPS}
99
${SSH_MAX_SESSIONS}
1010

11-
LogLevel INFO
11+
LogLevel ${SSH_LOG_LEVEL}
1212

1313
HostKey /data/ssh/ssh_host_ed25519_key
1414
${SSH_ED25519_CERT}

integrations/user_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,41 @@ func TestRenameReservedUsername(t *testing.T) {
7575
defer prepareTestEnv(t)()
7676

7777
reservedUsernames := []string{
78+
".",
79+
"..",
80+
".well-known",
7881
"admin",
7982
"api",
83+
"assets",
8084
"attachments",
85+
"avatar",
8186
"avatars",
87+
"captcha",
88+
"commits",
89+
"debug",
90+
"error",
8291
"explore",
83-
"help",
84-
"install",
92+
"favicon.ico",
93+
"ghost",
8594
"issues",
8695
"login",
96+
"manifest.json",
8797
"metrics",
98+
"milestones",
99+
"new",
88100
"notifications",
89101
"org",
90102
"pulls",
103+
"raw",
91104
"repo",
92-
"template",
93-
"user",
105+
"repo-avatars",
106+
"robots.txt",
94107
"search",
108+
"serviceworker.js",
109+
"ssh_info",
110+
"swagger.v1.json",
111+
"user",
112+
"v2",
95113
}
96114

97115
session := loginUser(t, "user2")

models/migrations/v180.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func removeCredentials(payload string) (string, error) {
112112

113113
opts.AuthPassword = ""
114114
opts.AuthToken = ""
115-
opts.CloneAddr = util.NewStringURLSanitizer(opts.CloneAddr, true).Replace(opts.CloneAddr)
115+
opts.CloneAddr = util.SanitizeCredentialURLs(opts.CloneAddr)
116116

117117
confBytes, err := json.Marshal(opts)
118118
if err != nil {

models/task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func FinishMigrateTask(task *Task) error {
245245
}
246246
conf.AuthPassword = ""
247247
conf.AuthToken = ""
248-
conf.CloneAddr = util.NewStringURLSanitizer(conf.CloneAddr, true).Replace(conf.CloneAddr)
248+
conf.CloneAddr = util.SanitizeCredentialURLs(conf.CloneAddr)
249249
conf.AuthPasswordEncrypted = ""
250250
conf.AuthTokenEncrypted = ""
251251
conf.CloneAddrEncrypted = ""

models/user/user.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ var (
576576
"api",
577577
"assets",
578578
"attachments",
579+
"avatar",
579580
"avatars",
580581
"captcha",
581582
"commits",
@@ -584,26 +585,23 @@ var (
584585
"explore",
585586
"favicon.ico",
586587
"ghost",
587-
"help",
588-
"install",
589588
"issues",
590-
"less",
591589
"login",
592590
"manifest.json",
593591
"metrics",
594592
"milestones",
595593
"new",
596594
"notifications",
597595
"org",
598-
"plugins",
599596
"pulls",
600597
"raw",
601598
"repo",
599+
"repo-avatars",
602600
"robots.txt",
603601
"search",
604602
"serviceworker.js",
605-
"stars",
606-
"template",
603+
"ssh_info",
604+
"swagger.v1.json",
607605
"user",
608606
"v2",
609607
}

modules/git/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (c *Command) RunWithContext(rc *RunContext) error {
154154
args = make([]string, len(c.args))
155155
copy(args, c.args)
156156
for _, urlArgIndex := range argSensitiveURLIndexes {
157-
args[urlArgIndex] = util.NewStringURLSanitizer(args[urlArgIndex], true).Replace(args[urlArgIndex])
157+
args[urlArgIndex] = util.SanitizeCredentialURLs(args[urlArgIndex])
158158
}
159159
}
160160
desc = fmt.Sprintf("%s %s [repo_path: %s]", c.name, strings.Join(args, " "), rc.Dir)

modules/git/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func CloneWithArgs(ctx context.Context, from, to string, args []string, opts Clo
156156
cmd.AddArguments("--", from, to)
157157

158158
if strings.Contains(from, "://") && strings.Contains(from, "@") {
159-
cmd.SetDescription(fmt.Sprintf("clone branch %s from %s to %s (shared: %t, mirror: %t, depth: %d)", opts.Branch, util.NewStringURLSanitizer(from, true).Replace(from), to, opts.Shared, opts.Mirror, opts.Depth))
159+
cmd.SetDescription(fmt.Sprintf("clone branch %s from %s to %s (shared: %t, mirror: %t, depth: %d)", opts.Branch, util.SanitizeCredentialURLs(from), to, opts.Shared, opts.Mirror, opts.Depth))
160160
} else {
161161
cmd.SetDescription(fmt.Sprintf("clone branch %s from %s to %s (shared: %t, mirror: %t, depth: %d)", opts.Branch, from, to, opts.Shared, opts.Mirror, opts.Depth))
162162
}
@@ -209,7 +209,7 @@ func Push(ctx context.Context, repoPath string, opts PushOptions) error {
209209
cmd.AddArguments(opts.Branch)
210210
}
211211
if strings.Contains(opts.Remote, "://") && strings.Contains(opts.Remote, "@") {
212-
cmd.SetDescription(fmt.Sprintf("push branch %s to %s (force: %t, mirror: %t)", opts.Branch, util.NewStringURLSanitizer(opts.Remote, true).Replace(opts.Remote), opts.Force, opts.Mirror))
212+
cmd.SetDescription(fmt.Sprintf("push branch %s to %s (force: %t, mirror: %t)", opts.Branch, util.SanitizeCredentialURLs(opts.Remote), opts.Force, opts.Mirror))
213213
} else {
214214
cmd.SetDescription(fmt.Sprintf("push branch %s to %s (force: %t, mirror: %t)", opts.Branch, opts.Remote, opts.Force, opts.Mirror))
215215
}

modules/util/sanitize.go

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,71 @@
55
package util
66

77
import (
8-
"net/url"
9-
"strings"
10-
)
8+
"bytes"
9+
"unicode"
1110

12-
const (
13-
userPlaceholder = "sanitized-credential"
14-
unparsableURL = "(unparsable url)"
11+
"github.com/yuin/goldmark/util"
1512
)
1613

1714
type sanitizedError struct {
18-
err error
19-
replacer *strings.Replacer
15+
err error
2016
}
2117

2218
func (err sanitizedError) Error() string {
23-
return err.replacer.Replace(err.err.Error())
19+
return SanitizeCredentialURLs(err.err.Error())
2420
}
2521

26-
// NewSanitizedError wraps an error and replaces all old, new string pairs in the message text.
27-
func NewSanitizedError(err error, oldnew ...string) error {
28-
return sanitizedError{err: err, replacer: strings.NewReplacer(oldnew...)}
22+
func (err sanitizedError) Unwrap() error {
23+
return err.err
2924
}
3025

31-
// NewURLSanitizedError wraps an error and replaces the url credential or removes them.
32-
func NewURLSanitizedError(err error, u *url.URL, usePlaceholder bool) error {
33-
return sanitizedError{err: err, replacer: NewURLSanitizer(u, usePlaceholder)}
26+
// SanitizeErrorCredentialURLs wraps the error and make sure the returned error message doesn't contain sensitive credentials in URLs
27+
func SanitizeErrorCredentialURLs(err error) error {
28+
return sanitizedError{err: err}
3429
}
3530

36-
// NewStringURLSanitizedError wraps an error and replaces the url credential or removes them.
37-
// If the url can't get parsed it gets replaced with a placeholder string.
38-
func NewStringURLSanitizedError(err error, unsanitizedURL string, usePlaceholder bool) error {
39-
return sanitizedError{err: err, replacer: NewStringURLSanitizer(unsanitizedURL, usePlaceholder)}
40-
}
31+
const userPlaceholder = "sanitized-credential"
4132

42-
// NewURLSanitizer creates a replacer for the url with the credential sanitized or removed.
43-
func NewURLSanitizer(u *url.URL, usePlaceholder bool) *strings.Replacer {
44-
old := u.String()
33+
var schemeSep = []byte("://")
4534

46-
if u.User != nil && usePlaceholder {
47-
u.User = url.User(userPlaceholder)
48-
} else {
49-
u.User = nil
35+
// SanitizeCredentialURLs remove all credentials in URLs (starting with "scheme://") for the input string: "https://user:[email protected]" => "https://[email protected]"
36+
func SanitizeCredentialURLs(s string) string {
37+
bs := util.StringToReadOnlyBytes(s)
38+
schemeSepPos := bytes.Index(bs, schemeSep)
39+
if schemeSepPos == -1 || bytes.IndexByte(bs[schemeSepPos:], '@') == -1 {
40+
return s // fast return if there is no URL scheme or no userinfo
5041
}
51-
return strings.NewReplacer(old, u.String())
52-
}
53-
54-
// NewStringURLSanitizer creates a replacer for the url with the credential sanitized or removed.
55-
// If the url can't get parsed it gets replaced with a placeholder string
56-
func NewStringURLSanitizer(unsanitizedURL string, usePlaceholder bool) *strings.Replacer {
57-
u, err := url.Parse(unsanitizedURL)
58-
if err != nil {
59-
// don't log the error, since it might contain unsanitized URL.
60-
return strings.NewReplacer(unsanitizedURL, unparsableURL)
42+
out := make([]byte, 0, len(bs)+len(userPlaceholder))
43+
for schemeSepPos != -1 {
44+
schemeSepPos += 3 // skip the "://"
45+
sepAtPos := -1 // the possible '@' position: "https://foo@[^here]host"
46+
sepEndPos := schemeSepPos // the possible end position: "The https://host[^here] in log for test"
47+
sepLoop:
48+
for ; sepEndPos < len(bs); sepEndPos++ {
49+
c := bs[sepEndPos]
50+
if ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9') {
51+
continue
52+
}
53+
switch c {
54+
case '@':
55+
sepAtPos = sepEndPos
56+
case '-', '.', '_', '~', '!', '$', '&', '\'', '(', ')', '*', '+', ',', ';', '=', ':', '%':
57+
continue // due to RFC 3986, userinfo can contain - . _ ~ ! $ & ' ( ) * + , ; = : and any percent-encoded chars
58+
default:
59+
break sepLoop // if it is an invalid char for URL (eg: space, '/', and others), stop the loop
60+
}
61+
}
62+
// if there is '@', and the string is like "s://u@h", then hide the "u" part
63+
if sepAtPos != -1 && (schemeSepPos >= 4 && unicode.IsLetter(rune(bs[schemeSepPos-4]))) && sepAtPos-schemeSepPos > 0 && sepEndPos-sepAtPos > 0 {
64+
out = append(out, bs[:schemeSepPos]...)
65+
out = append(out, userPlaceholder...)
66+
out = append(out, bs[sepAtPos:sepEndPos]...)
67+
} else {
68+
out = append(out, bs[:sepEndPos]...)
69+
}
70+
bs = bs[sepEndPos:]
71+
schemeSepPos = bytes.Index(bs, schemeSep)
6172
}
62-
return NewURLSanitizer(u, usePlaceholder)
73+
out = append(out, bs...)
74+
return util.BytesToReadOnlyString(out)
6375
}

0 commit comments

Comments
 (0)