Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(oracle): quote non-regular usernames

t8y2c6fc5cb90927
diff --git a/agents/drivers/oracle-go/main.go b/agents/drivers/oracle-go/main.go
index 8cfd6d57fd..ae14bccf99 100644
--- a/agents/drivers/oracle-go/main.go
+++ b/agents/drivers/oracle-go/main.go
@@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
+ "net"
"net/url"
"os"
"regexp"
@@ -610,6 +611,7 @@ func buildDSN(params connectParams) string {
if strings.HasPrefix(strings.ToLower(connectionString), "oracle://") {
return connectionString
}
+ username := oracleAuthUsername(params.Username)
options := parseURLParams(params.URLParams)
if params.SysDBA {
options["AUTH TYPE"] = "SYSDBA"
@@ -617,7 +619,7 @@ func buildDSN(params connectParams) string {
if jdbc := parseOracleJDBCURL(connectionString); jdbc.Kind != "" {
if jdbc.Descriptor != "" {
- return go_ora.BuildJDBC(params.Username, params.Password, jdbc.Descriptor, options)
+ return buildGoOraJDBC(username, params.Password, jdbc.Descriptor, options)
}
host := jdbc.Host
port := jdbc.Port
@@ -626,9 +628,9 @@ func buildDSN(params connectParams) string {
}
if jdbc.Kind == "sid" {
options["SID"] = jdbc.Database
- return go_ora.BuildUrl(host, port, "", params.Username, params.Password, options)
+ return buildGoOraURL(host, port, "", username, params.Password, options)
}
- return go_ora.BuildUrl(host, port, jdbc.Database, params.Username, params.Password, options)
+ return buildGoOraURL(host, port, jdbc.Database, username, params.Password, options)
}
service := strings.TrimSpace(params.Database)
@@ -639,7 +641,78 @@ func buildDSN(params connectParams) string {
if port == 0 {
port = 1521
}
- return go_ora.BuildUrl(params.Host, port, service, params.Username, params.Password, options)
+ return buildGoOraURL(params.Host, port, service, username, params.Password, options)
+}
+
+func oracleAuthUsername(username string) string {
+ if username == "" || isQuotedOracleUsername(username) || !oracleUsernameRequiresQuoting(username) {
+ return username
+ }
+ // Oracle logon accepts quoted identifiers for users that cannot be
+ // represented as regular identifiers, such as bastion usernames with ':'.
+ return `"` + strings.ReplaceAll(username, `"`, `""`) + `"`
+}
+
+func isQuotedOracleUsername(username string) bool {
+ return len(username) >= 2 && strings.HasPrefix(username, `"`) && strings.HasSuffix(username, `"`)
+}
+
+func oracleUsernameRequiresQuoting(username string) bool {
+ for index, ch := range username {
+ if index == 0 {
+ if !isAsciiLetter(ch) {
+ return true
+ }
+ continue
+ }
+ if !isAsciiLetter(ch) && !isAsciiDigit(ch) && ch != '_' && ch != '$' && ch != '#' {
+ return true
+ }
+ }
+ return false
+}
+
+func isAsciiLetter(ch rune) bool {
+ return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
+}
+
+func isAsciiDigit(ch rune) bool {
+ return ch >= '0' && ch <= '9'
+}
+
+func buildGoOraJDBC(user, password, connStr string, options map[string]string) string {
+ if options == nil {
+ options = make(map[string]string)
+ }
+ options["connStr"] = connStr
+ return buildGoOraURL("", 0, "", user, password, options)
+}
+
+func buildGoOraURL(server string, port int, service, user, password string, options map[string]string) string {
+ // go-ora v2.9.0 uses path escaping for user/password, leaving ':' unescaped.
+ // Userinfo escaping keeps usernames such as "9008888:reader" intact.
+ ret := fmt.Sprintf(
+ "oracle://%s@%s/%s",
+ url.UserPassword(user, password).String(),
+ net.JoinHostPort(server, strconv.Itoa(port)),
+ url.PathEscape(service),
+ )
+ if options != nil {
+ ret += "?"
+ for key, val := range options {
+ val = strings.TrimSpace(val)
+ for _, temp := range strings.Split(val, ",") {
+ temp = strings.TrimSpace(temp)
+ if strings.ToUpper(key) == "SERVER" {
+ ret += fmt.Sprintf("%s=%s&", key, temp)
+ } else {
+ ret += fmt.Sprintf("%s=%s&", key, url.QueryEscape(temp))
+ }
+ }
+ }
+ ret = strings.TrimRight(ret, "&")
+ }
+ return ret
}
type jdbcURLInfo struct {
diff --git a/agents/drivers/oracle-go/main_test.go b/agents/drivers/oracle-go/main_test.go
index 2ab25a77fe..b152d0c4ae 100644
--- a/agents/drivers/oracle-go/main_test.go
+++ b/agents/drivers/oracle-go/main_test.go
@@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"errors"
+ "net/url"
"os"
"strings"
"testing"
@@ -269,6 +270,66 @@ func TestBuildDSNUsesConnectionStringWhenProvided(t *testing.T) {
}
}
+func TestBuildDSNEncodesColonInCredentials(t *testing.T) {
+ dsn := buildDSN(connectParams{
+ Host: "db.example.com",
+ Port: 1521,
+ Database: "XE",
+ Username: "9008888:reader",
+ Password: "dbx:pass",
+ })
+
+ parsed, err := url.Parse(dsn)
+ if err != nil {
+ t.Fatal(err)
+ }
+ password, _ := parsed.User.Password()
+ if parsed.User.Username() != `"9008888:reader"` || password != "dbx:pass" {
+ t.Fatalf("credentials should survive URL parsing, dsn=%s username=%q password=%q", dsn, parsed.User.Username(), password)
+ }
+ if !strings.HasPrefix(parsed.User.String(), "%229008888%3Areader%22:") {
+ t.Fatalf("Oracle auth username should be quoted and escaped for non-regular identifiers, dsn=%s", dsn)
+ }
+}
+
+func TestBuildDSNEncodesColonInCredentialsFromJDBCServiceURL(t *testing.T) {
+ dsn := buildDSN(connectParams{
+ Username: "9008888:reader",
+ Password: "dbx:pass",
+ ConnectionString: "jdbc:oracle:thin:@//db.example.com:1521/XE",
+ })
+
+ parsed, err := url.Parse(dsn)
+ if err != nil {
+ t.Fatal(err)
+ }
+ password, _ := parsed.User.Password()
+ if parsed.User.Username() != `"9008888:reader"` || password != "dbx:pass" {
+ t.Fatalf("credentials should survive JDBC URL conversion, dsn=%s username=%q password=%q", dsn, parsed.User.Username(), password)
+ }
+ if parsed.Host != "db.example.com:1521" || strings.TrimPrefix(parsed.Path, "/") != "XE" {
+ t.Fatalf("JDBC host/service should survive conversion, dsn=%s", dsn)
+ }
+}
+
+func TestOracleAuthUsernameQuotesOnlyNonRegularIdentifiers(t *testing.T) {
+ tests := map[string]string{
+ "scott": "scott",
+ "test": "test",
+ "SCOTT_1": "SCOTT_1",
+ "9008888:reader": `"9008888:reader"`,
+ "abc:def": `"abc:def"`,
+ `"abc:def"`: `"abc:def"`,
+ `abc"def`: `"abc""def"`,
+ }
+
+ for input, want := range tests {
+ if got := oracleAuthUsername(input); got != want {
+ t.Fatalf("oracleAuthUsername(%q) = %q, want %q", input, got, want)
+ }
+ }
+}
+
func TestBuildDSNUsesJdbcServiceHostAndPort(t *testing.T) {
dsn := buildDSN(connectParams{
Host: "127.0.0.1",