Skip to content

Commit ad39640

Browse files
VJ-yadavVijay Yadav
andcommitted
fix: address CodeRabbit review — try-catch for malformed URIs, add / and ? tests
- Wrap decodeURIComponent(user) in try-catch to handle malformed percent sequences gracefully (falls back to encoding raw value) - Add tests for / and ? in passwords - Add test for malformed percent sequence in username - Use test-prefixed values in all connection strings to avoid GitGuardian false positives Co-Authored-By: Vijay Yadav <vijay@studentsucceed.com>
1 parent aabcd73 commit ad39640

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

packages/drivers/src/normalize.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,15 @@ export function sanitizeConnectionString(connectionString: string): string {
158158
const needsEncoding = /[@:#/?[\]%]/.test(password)
159159
if (!needsEncoding) return connectionString
160160

161-
// Re-encode both user and password to be safe
162-
const encodedUser = encodeURIComponent(decodeURIComponent(user))
161+
// Re-encode both user and password to be safe.
162+
// decodeURIComponent can throw on malformed percent sequences — fall back to
163+
// encoding the raw value if that happens.
164+
let encodedUser: string
165+
try {
166+
encodedUser = encodeURIComponent(decodeURIComponent(user))
167+
} catch {
168+
encodedUser = encodeURIComponent(user)
169+
}
163170
const encodedPassword = encodeURIComponent(password)
164171

165172
return `${scheme}${encodedUser}:${encodedPassword}@${rest}`

packages/opencode/test/altimate/driver-normalize.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,25 @@ describe("sanitizeConnectionString", () => {
977977
expect(result).toBe("postgresql://testuser:t%40st%23v%3Aal@localhost:5432/testdb")
978978
})
979979

980+
test("encodes / in password", () => {
981+
const input = "postgresql://testuser:test/val@localhost:5432/testdb"
982+
const result = sanitizeConnectionString(input)
983+
expect(result).toBe("postgresql://testuser:test%2Fval@localhost:5432/testdb")
984+
})
985+
986+
test("encodes ? in password", () => {
987+
const input = "postgresql://testuser:test?val@localhost:5432/testdb"
988+
const result = sanitizeConnectionString(input)
989+
expect(result).toBe("postgresql://testuser:test%3Fval@localhost:5432/testdb")
990+
})
991+
992+
test("handles malformed percent sequence in username gracefully", () => {
993+
const input = "postgresql://bad%ZZuser:t@st@localhost:5432/testdb"
994+
const result = sanitizeConnectionString(input)
995+
// Should not throw — falls back to encoding the raw username
996+
expect(result).toContain("@localhost:5432/testdb")
997+
})
998+
980999
test("leaves already-encoded passwords untouched", () => {
9811000
const input = "postgresql://testuser:t%40st%23val@localhost:5432/testdb"
9821001
expect(sanitizeConnectionString(input)).toBe(input)

0 commit comments

Comments
 (0)