Skip to content

Commit e46a638

Browse files
zeripath6543
andauthored
Report permissions denied in internal SSH (#13953)
This PR standardizes reporting of permission denied from the internal ssh. Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: 6543 <[email protected]>
1 parent 8e0548e commit e46a638

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

modules/ssh/ssh.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func sessionHandler(session ssh.Session) {
135135

136136
func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
137137
if ctx.User() != setting.SSH.BuiltinServerUser {
138+
log.Warn("Permission Denied: Invalid SSH username %s - must use %s for all git operations via ssh", ctx.User(), setting.SSH.BuiltinServerUser)
138139
return false
139140
}
140141

@@ -145,17 +146,18 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
145146
}
146147

147148
// look for the exact principal
149+
principalLoop:
148150
for _, principal := range cert.ValidPrincipals {
149151
pkey, err := models.SearchPublicKeyByContentExact(principal)
150152
if err != nil {
153+
if models.IsErrKeyNotExist(err) {
154+
log.Debug("Principal Rejected: Unknown Principal: %s", principal)
155+
continue principalLoop
156+
}
151157
log.Error("SearchPublicKeyByContentExact: %v", err)
152158
return false
153159
}
154160

155-
if models.IsErrKeyNotExist(err) {
156-
continue
157-
}
158-
159161
c := &gossh.CertChecker{
160162
IsUserAuthority: func(auth gossh.PublicKey) bool {
161163
for _, k := range setting.SSH.TrustedUserCAKeysParsed {
@@ -170,11 +172,14 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
170172

171173
// check the CA of the cert
172174
if !c.IsUserAuthority(cert.SignatureKey) {
173-
return false
175+
log.Debug("Principal Rejected: Untrusted Authority Signature Fingerprint %s for Principal: %s", gossh.FingerprintSHA256(cert.SignatureKey), principal)
176+
continue principalLoop
174177
}
175178

176179
// validate the cert for this principal
177180
if err := c.CheckCert(principal, cert); err != nil {
181+
// User is presenting an invalid cerficate - STOP any further processing
182+
log.Error("Permission Denied: Invalid Certificate KeyID %s with Signature Fingerprint %s presented for Principal: %s", cert.KeyId, gossh.FingerprintSHA256(cert.SignatureKey), principal)
178183
return false
179184
}
180185

@@ -186,6 +191,10 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
186191

187192
pkey, err := models.SearchPublicKeyByContent(strings.TrimSpace(string(gossh.MarshalAuthorizedKey(key))))
188193
if err != nil {
194+
if models.IsErrKeyNotExist(err) {
195+
log.Warn("Permission Denied: Unknown public key : %s", gossh.FingerprintSHA256(key))
196+
return false
197+
}
189198
log.Error("SearchPublicKeyByContent: %v Failed authentication attempt from %s", err, ctx.RemoteAddr())
190199
return false
191200
}

0 commit comments

Comments
 (0)