Go Mysql Empty Password In Dsn

Description

This detector identifies Go applications using empty passwords in MySQL database connection strings. Empty passwords create a significant security vulnerability as they allow unauthorized access to the database without authentication, potentially exposing sensitive data and allowing malicious operations.

Weakness:

363 - Weak credential policy - Password strength

Category: Unexpected Injection

Detection Strategy

    The detector only analyzes Go source files that are not test files

    The code must import both the database/sql package and a MySQL driver package

    The detector looks for calls to the sql.Open() function (or aliased equivalent)

    It examines the second argument (index 1) of sql.Open() which contains the database connection string

    The connection string is analyzed to determine if it contains an empty password field in the MySQL DSN format

    A vulnerability is reported when sql.Open() is called with a connection string that has an empty password

Vulnerable code example

package main

import (
	"database/sql"
	"github.com/go-sql-driver/mysql"
	_ "github.com/go-sql-driver/mysql"
)
...

✅ Secure code example

package main

import (
	"database/sql"
	"os"
	"github.com/go-sql-driver/mysql"
	_ "github.com/go-sql-driver/mysql"
)...