@@ -135,6 +135,7 @@ func sessionHandler(session ssh.Session) {
135
135
136
136
func publicKeyHandler (ctx ssh.Context , key ssh.PublicKey ) bool {
137
137
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 )
138
139
return false
139
140
}
140
141
@@ -145,17 +146,18 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
145
146
}
146
147
147
148
// look for the exact principal
149
+ principalLoop:
148
150
for _ , principal := range cert .ValidPrincipals {
149
151
pkey , err := models .SearchPublicKeyByContentExact (principal )
150
152
if err != nil {
153
+ if models .IsErrKeyNotExist (err ) {
154
+ log .Debug ("Principal Rejected: Unknown Principal: %s" , principal )
155
+ continue principalLoop
156
+ }
151
157
log .Error ("SearchPublicKeyByContentExact: %v" , err )
152
158
return false
153
159
}
154
160
155
- if models .IsErrKeyNotExist (err ) {
156
- continue
157
- }
158
-
159
161
c := & gossh.CertChecker {
160
162
IsUserAuthority : func (auth gossh.PublicKey ) bool {
161
163
for _ , k := range setting .SSH .TrustedUserCAKeysParsed {
@@ -170,11 +172,14 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
170
172
171
173
// check the CA of the cert
172
174
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
174
177
}
175
178
176
179
// validate the cert for this principal
177
180
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 )
178
183
return false
179
184
}
180
185
@@ -186,6 +191,10 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
186
191
187
192
pkey , err := models .SearchPublicKeyByContent (strings .TrimSpace (string (gossh .MarshalAuthorizedKey (key ))))
188
193
if err != nil {
194
+ if models .IsErrKeyNotExist (err ) {
195
+ log .Warn ("Permission Denied: Unknown public key : %s" , gossh .FingerprintSHA256 (key ))
196
+ return false
197
+ }
189
198
log .Error ("SearchPublicKeyByContent: %v Failed authentication attempt from %s" , err , ctx .RemoteAddr ())
190
199
return false
191
200
}
0 commit comments