Skip to content
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

Do not clobber the "tcp" dialer for MySQL #5681

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/js/libs/mysql/mysql_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ func BuildDSN(opts MySQLOptions) (string, error) {
if opts.Protocol == "" {
opts.Protocol = "tcp"
}
// We're going to use a custom dialer when creating MySQL connections, so if we've been
// given "tcp" as the protocol, then quietly switch it to "nucleitcp", which we have
// already registered.
if opts.Protocol == "tcp" {
opts.Protocol = "nucleitcp"
}
if opts.DbName == "" {
opts.DbName = "/"
} else {
Expand Down
12 changes: 10 additions & 2 deletions pkg/protocols/common/protocolstate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,16 @@ func Init(options *types.Options) error {
}
Dialer = dialer

// override dialer in mysql
mysql.RegisterDialContext("tcp", func(ctx context.Context, addr string) (net.Conn, error) {
// Set a custom dialer for the "nucleitcp" protocol. This is just plain TCP, but it's registered
// with a different name so that we do not clobber the "tcp" dialer in the event that nuclei is
// being included as a package in another application.
mysql.RegisterDialContext("nucleitcp", func(ctx context.Context, addr string) (net.Conn, error) {
// Because we're not using the default TCP workflow, quietly add the default port
// number if no port number was specified.
if _, _, err := net.SplitHostPort(addr); err != nil {
addr += ":3306"
}

Comment on lines +157 to +166
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Add test coverage for the MySQL nucleitcp protocol implementation

The custom MySQL TCP dialer implementation lacks test coverage. Please add unit tests to verify:

  • Custom dialer registration with "nucleitcp" protocol
  • Default port (3306) addition when port is not specified
  • Connection establishment behavior
🔗 Analysis chain

Verify test coverage for the new protocol.

Let's ensure there are adequate tests for the new "nucleitcp" protocol functionality.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for test coverage of the new protocol
echo "Checking for MySQL protocol tests:"
rg -g '*_test.go' 'nucleitcp|RegisterDialContext'

echo -e "\nChecking for MySQL connection tests:"
rg -g '*_test.go' 'mysql.*Protocol.*=.*"tcp"'

Length of output: 267


Script:

#!/bin/bash
# Find MySQL-related test files
echo "MySQL test files:"
fd -g '*mysql*_test.go'

echo -e "\nTest files in protocol state directory:"
fd -g '*_test.go' pkg/protocols/common/protocolstate/

echo -e "\nChecking for TCP connection tests:"
rg -g '*_test.go' -A 5 'func.*Test.*TCP|func.*Test.*Connection' pkg/protocols/common/

echo -e "\nChecking for integration tests:"
fd -g '*integration*test.go'

Length of output: 545

return Dialer.Dial(ctx, "tcp", addr)
})

Expand Down
Loading