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

Conversation

doug-threatmate
Copy link
Contributor

@doug-threatmate doug-threatmate commented Oct 1, 2024

Proposed changes

This introduces a "nucleitcp" protocol that Nuclei will use when making MySQL connections as part of its templates.

Previously, this would register (and de-register!) a custom "tcp" dialer, and that applied globally, so any piece of software that used a MySQL database and included nuclei in SDK mode would have its database connections ripped out from under it due to the dialer hijacking.

By using "nucleitcp" as the protocol, we are free to do whatever we want with the dialer and not impact any other packages.

Within our BuildDSN function, we quietly replace the protocol to "nucleitcp" if it was "tcp", so nuclei developers don't have to do anything special to use this functionality; it will always do it.

Closes #5854

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Summary by CodeRabbit

  • New Features

    • Introduced a custom "nucleitcp" protocol for MySQL connections
    • Added automatic port handling for MySQL dialer
  • Improvements

    • Enhanced MySQL connection configuration to support custom dialer
    • Maintained backward compatibility with existing TCP protocol

This introduces a "nucleitcp" protocol that Nuclei will use when
making MySQL connections as part of its templates.

Previously, this would register (and de-register!) a custom "tcp"
dialer, and that applied globally, so any piece of software that
used a MySQL database and included nuclei in SDK mode would have
its database connections ripped out from under it due to the dialer
hijacking.

By using "nucleitcp" as the protocol, we are free to do whatever
we want with the dialer and not impact any other packages.

Within our `BuildDSN` function, we quietly replace the protocol to
"nucleitcp" if it was "tcp", so nuclei developers don't have to do
anything special to use this functionality; it will always do it.
@auto-assign auto-assign bot requested a review from dogancanbakir October 1, 2024 14:23
@GeorginaReeder
Copy link

Thanks so much for your contribution @doug-threatmate ! :)

@dogancanbakir
Copy link
Member

Thanks for the PR! Couldn't do it, could you do a rebase?

Copy link
Member

@ehsandeep ehsandeep left a comment

Choose a reason for hiding this comment

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

@doug-threatmate can you rebase this from dev to ensure build pipeline is working?

Copy link
Contributor

coderabbitai bot commented Jan 14, 2025

Walkthrough

The pull request introduces modifications to the MySQL connection handling in two files. In mysql_private.go, the BuildDSN function now changes the "tcp" protocol to "nucleitcp" when constructing the connection string. Correspondingly, in protocolstate/state.go, the MySQL dialer registration is updated to use the "nucleitcp" protocol instead of "tcp", with an added feature to automatically append the default MySQL port (3306) if not specified.

Changes

File Change Summary
pkg/js/libs/mysql/mysql_private.go Modified BuildDSN to replace "tcp" protocol with "nucleitcp"
pkg/protocols/common/protocolstate/state.go Updated MySQL dialer registration from "tcp" to "nucleitcp" with port handling logic

Assessment against linked issues

Objective Addressed Explanation
Do not clobber the "tcp" dialer for MySQL [#5854]

Poem

🐰 A MySQL connection tale so bright,
Nuclei's dialer takes gentle flight
"tcp" remains pure and clean
"nucleitcp" joins the scene
A protocol dance of pure delight! 🌐

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@doug-threatmate
Copy link
Contributor Author

Merged and pushed.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 08c46ff and dbc9f8f.

📒 Files selected for processing (2)
  • pkg/js/libs/mysql/mysql_private.go (1 hunks)
  • pkg/protocols/common/protocolstate/state.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Tests (macOS-latest)
  • GitHub Check: Tests (windows-latest)
  • GitHub Check: Tests (ubuntu-latest)
🔇 Additional comments (4)
pkg/js/libs/mysql/mysql_private.go (2)

49-54: LGTM! Protocol switching implementation looks good.

The implementation correctly switches from "tcp" to "nucleitcp" protocol while maintaining backward compatibility. This change aligns with the PR objective of preventing interference with other packages using MySQL in SDK mode.


49-54: Verify impact on existing MySQL connections.

Let's ensure this change doesn't affect other protocol types and existing MySQL connections.

✅ Verification successful

Protocol switch to "nucleitcp" is safe and intentional

The change is a deliberate design choice to prevent interference with the default TCP dialer when nuclei is used as a package in other applications. The switch is transparent to users, only affects TCP connections, and has proper custom dialer registration.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for MySQL connection strings in the codebase to verify impact
echo "Checking MySQL DSN usage patterns:"
rg -g '!{vendor,.*_test.go}' '(?i)mysql:\/\/|mysql.*DSN'

echo -e "\nChecking for other protocol types in MySQL connections:"
rg -g '!{vendor,.*_test.go}' 'Protocol.*=.*"(?!tcp|nucleitcp)"'

Length of output: 682


Script:

#!/bin/bash
# Search for MySQL protocol usage
echo "Checking MySQL protocol usage:"
rg -A 3 'Protocol.*=.*"' pkg/

echo -e "\nChecking nucleitcp references:"
rg -A 3 'nucleitcp'

echo -e "\nChecking MySQL connection related files:"
fd -e go mysql

Length of output: 2926

pkg/protocols/common/protocolstate/state.go (2)

157-166: LGTM! Custom dialer registration with default port handling.

The implementation correctly:

  1. Registers "nucleitcp" protocol to avoid interfering with the default "tcp" dialer
  2. Adds default MySQL port (3306) when not specified
  3. Uses the existing Dialer for TCP connections

The comments clearly explain the rationale behind using a different protocol name, which helps with future maintenance.


157-166: Verify thread safety of dialer registration.

Let's ensure the dialer registration is thread-safe and won't cause race conditions.

✅ Verification successful

The dialer registration implementation is thread-safe.

The registration occurs once during package initialization, uses a unique protocol name "nucleitcp", and shows no evidence of concurrent access or runtime modifications.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for concurrent access to mysql.RegisterDialContext
echo "Checking for concurrent dialer registration:"
rg -A 5 'RegisterDialContext.*nucleitcp' 

echo -e "\nChecking for other goroutines that might access the dialer:"
rg 'go\s+func.*mysql.*Dial'

Length of output: 877


Error: No shell scripts found


Script:

#!/bin/bash
# Check for any other mysql package imports and initialization
echo "Checking for mysql package imports and initialization:"
rg -g '!vendor/*' -B 2 -A 2 'mysql\.'

echo -e "\nChecking for any dynamic protocol registration:"
rg -g '!vendor/*' 'Register.*Context.*mysql'

Length of output: 9109

Comment on lines +157 to +166
// 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"
}

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

Copy link
Member

@ehsandeep ehsandeep left a comment

Choose a reason for hiding this comment

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

failing test related to offlinehttp is unrelated.

@ehsandeep ehsandeep merged commit e4a0b8e into projectdiscovery:dev Jan 14, 2025
17 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Do not clobber the "tcp" dialer for MySQL
5 participants