-
Notifications
You must be signed in to change notification settings - Fork 340
ci(codeql): skip warning in the test file #13406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Signed-off-by: Lukasz Dziedziak <lukidzi@gmail.com>
Reviewer Checklist🔍 Each of these sections need to be checked by the reviewer of the PR 🔍:
|
@@ -74,14 +74,14 @@ | |||
Auth: []ssh.AuthMethod{ | |||
ssh.PublicKeys(signer), | |||
}, | |||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //#nosec G106 // skip for tests | |||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // lgtm[go/insecure-hostkeycallback] //#nosec G106 // skip for tests |
Check failure
Code scanning / CodeQL
Use of insecure HostKeyCallback implementation High test
this source
Copilot Autofix
AI 3 days ago
Copilot could not generate an autofix suggestion
Copilot could not generate an autofix suggestion for this alert. Try pushing a new commit or if the problem persists contact support.
} | ||
|
||
client, err = ssh.Dial("tcp", net.JoinHostPort(s.RemoteHost.Address, | ||
strconv.Itoa(s.RemoteHost.Port)), configCfg) | ||
} else { | ||
client, err = ssh.Dial("tcp", net.JoinHostPort("localhost", s.SshPort), &ssh.ClientConfig{ | ||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //#nosec G106 // skip for tests // lgtm [go/insecure-hostkeycallback] | ||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // lgtm[go/insecure-hostkeycallback] //#nosec G106 // skip for tests |
Check failure
Code scanning / CodeQL
Use of insecure HostKeyCallback implementation High test
this source
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 3 days ago
To fix the problem, we need to replace the insecure ssh.InsecureIgnoreHostKey()
with a secure host key callback implementation. The best way to do this is to use the ssh.FixedHostKey
function, which validates the host key against a predefined allow list. This ensures that only trusted host keys are accepted, mitigating the risk of MitM attacks.
- Read the allowed host key from a file (e.g.,
allowed_hostkey.pub
). - Parse the public key using
ssh.ParsePublicKey
. - Use the parsed public key with
ssh.FixedHostKey
to create a secureHostKeyCallback
.
-
Copy modified lines R77-R90 -
Copy modified lines R97-R110
@@ -76,3 +76,16 @@ | ||
}, | ||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // lgtm[go/insecure-hostkeycallback] //#nosec G106 // skip for tests | ||
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { | ||
allowedKey, err := ioutil.ReadFile("allowed_hostkey.pub") | ||
if err != nil { | ||
return fmt.Errorf("failed to read allowed host key: %w", err) | ||
} | ||
parsedKey, err := ssh.ParsePublicKey(allowedKey) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse allowed host key: %w", err) | ||
} | ||
if bytes.Equal(key.Marshal(), parsedKey.Marshal()) { | ||
return nil | ||
} | ||
return fmt.Errorf("host key verification failed") | ||
}, | ||
} | ||
@@ -83,3 +96,16 @@ | ||
client, err = ssh.Dial("tcp", net.JoinHostPort("localhost", s.SshPort), &ssh.ClientConfig{ | ||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), // lgtm[go/insecure-hostkeycallback] //#nosec G106 // skip for tests | ||
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { | ||
allowedKey, err := ioutil.ReadFile("allowed_hostkey.pub") | ||
if err != nil { | ||
return fmt.Errorf("failed to read allowed host key: %w", err) | ||
} | ||
parsedKey, err := ssh.ParsePublicKey(allowedKey) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse allowed host key: %w", err) | ||
} | ||
if bytes.Equal(key.Marshal(), parsedKey.Marshal()) { | ||
return nil | ||
} | ||
return fmt.Errorf("host key verification failed") | ||
}, | ||
User: "root", |
Motivation
We can see security issues but they are not, since we use these files only in tests.
Implementation information
Ignore this validation in specific line
Supporting documentation