@@ -13,7 +13,6 @@ import (
13
13
"errors"
14
14
"fmt"
15
15
"io"
16
- "maps"
17
16
"net"
18
17
"os"
19
18
"os/exec"
@@ -49,6 +48,10 @@ import (
49
48
// Then sessionHandler should only use the "verified keyID" from the original ssh conn, but not the ctx one.
50
49
// Otherwise, if a user provides 2 keys A (a correct one) and B (public key matches but no private key),
51
50
// then only A succeeds to authenticate, sessionHandler will see B's keyID
51
+ //
52
+ // After x/crypto >= 0.31.0 (fix CVE-2024-45337), the PublicKeyCallback will be called again for the verified key,
53
+ // it mitigates the misuse for most cases, it's still good for us to make sure we don't rely on that mitigation
54
+ // and do not misuse the PublicKeyCallback: we should only use the verified keyID from the verified ssh conn.
52
55
53
56
const giteaPermissionExtensionKeyID = "gitea-perm-ext-key-id"
54
57
@@ -100,8 +103,8 @@ func ptr[T any](intf any) *T {
100
103
func sessionHandler (session ssh.Session ) {
101
104
// here can't use session.Permissions() because it only uses the value from ctx, which might not be the authenticated one.
102
105
// so we must use the original ssh conn, which always contains the correct (verified) keyID.
103
- sshConn := ptr [sessionPartial ](session )
104
- keyID := sshConn .conn .Permissions .Extensions [giteaPermissionExtensionKeyID ]
106
+ sshSession := ptr [sessionPartial ](session )
107
+ keyID := sshSession .conn .Permissions .Extensions [giteaPermissionExtensionKeyID ]
105
108
106
109
command := session .RawCommand ()
107
110
@@ -210,10 +213,7 @@ func publicKeyHandler(ctx ssh.Context, key ssh.PublicKey) bool {
210
213
211
214
// first, reset the ctx permissions (just like https://github.com/gliderlabs/ssh/pull/243 does)
212
215
// it shouldn't be reused across different ssh conn (sessions), each pub key should have its own "Permissions"
213
- oldCtxPerm := ctx .Permissions ().Permissions
214
216
ctx .Permissions ().Permissions = & gossh.Permissions {}
215
- ctx .Permissions ().Permissions .CriticalOptions = maps .Clone (oldCtxPerm .CriticalOptions )
216
-
217
217
setPermExt := func (keyID int64 ) {
218
218
ctx .Permissions ().Permissions .Extensions = map [string ]string {
219
219
giteaPermissionExtensionKeyID : fmt .Sprint (keyID ),
0 commit comments