Skip to content

Commit

Permalink
Refactor code to use subtle.ConstantTimeCompare for secure comparison (
Browse files Browse the repository at this point in the history
…#466)

* Refactor code to use subtle.ConstantTimeCompare for secure comparison

* Refactor code to use subtle.ConstantTimeCompare for secure comparison
  • Loading branch information
tg123 authored Oct 21, 2024
1 parent 3087aa4 commit a3f6995
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions plugin/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
package main

import (
"bytes"
"context"
"crypto/subtle"
"encoding/base64"
"fmt"
"net"
Expand Down Expand Up @@ -211,7 +211,7 @@ func (p *plugin) findAndCreateUpstream(conn libplugin.ConnMetadata, password str
return nil, err
}

if bytes.Equal(authedPubkey.Marshal(), publicKey) {
if subtle.ConstantTimeCompare(authedPubkey.Marshal(), publicKey) == 1 {
return p.createUpstream(conn, pipe, "")
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/internal/workingdir/workingdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package workingdir

import (
"bufio"
"bytes"
"crypto/subtle"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -60,7 +60,7 @@ func (w *Workingdir) Mapkey(pub []byte) ([]byte, error) {
return nil, err
}

if bytes.Equal(authedPubkey.Marshal(), pub) {
if subtle.ConstantTimeCompare(authedPubkey.Marshal(), pub) == 1 {
log.Infof("found mapping key %v", w.fullpath(userKeyFile))
return w.Readfile(userKeyFile)
}
Expand Down
3 changes: 2 additions & 1 deletion plugin/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"context"
"crypto/subtle"
"encoding/base64"
"fmt"
"os"
Expand Down Expand Up @@ -295,7 +296,7 @@ func (p *plugin) findAndCreateUpstream(conn libplugin.ConnMetadata, password str
return nil, err
}

if bytes.Equal(authedPubkey.Marshal(), publicKey) {
if subtle.ConstantTimeCompare(authedPubkey.Marshal(), publicKey) == 1 {
return p.createUpstream(conn, pipe, "")
}
}
Expand Down
3 changes: 2 additions & 1 deletion plugin/yaml/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"bytes"
"crypto/subtle"
"encoding/base64"
"fmt"
"os"
Expand Down Expand Up @@ -329,7 +330,7 @@ func (p *plugin) findAndCreateUpstream(conn libplugin.ConnMetadata, password str
return nil, err
}

if bytes.Equal(authedPubkey.Marshal(), publicKey) {
if subtle.ConstantTimeCompare(authedPubkey.Marshal(), publicKey) == 1 {
return p.createUpstream(conn, pipe.To, "")
}
}
Expand Down

0 comments on commit a3f6995

Please sign in to comment.