URL Redacted Hides Password

Printing a credential-bearing URL leaks the password; url.URL.Redacted swaps it for xxxx while User stays usable inside the process.

GoSecurity Boundaries

Code

gosnippet
20 lines
package main

import (
	"fmt"
	"net/url"
)

func main() {
	endpoint, err := url.Parse("postgres://admin:s3cret@db.internal:5432/app?sslmode=require")
	if err != nil {
		panic(err)
	}

	// 直接打整条 URL 会把密码带进日志:Redacted() 把密码替换成 xxxx
	fmt.Println("redacted:", endpoint.Redacted())

	// User 信息在进程内照常可用,泄露面只出现在编码边界
	fmt.Println("user:", endpoint.User.Username())
	fmt.Println("host:", endpoint.Host)
}

The movements of the program

URL Redacted Hides Password keeps everything in the function main.

Every err != nil branch (1 site) calls panic(err) — the run stops at the first failure instead of continuing in a broken state.

Watch out for

  • Failures abort via panic(err) — nothing is returned for the caller to check, and nothing in the snippet recovers.

Applying it

The demo output is labeled (redacted:, user:, host:), so the output doubles as a step-by-step trace of what the file did.

Statements that matter

if err != nil {
	panic(err)
}

Interactive Tools for This Topic

FAQ

Does the snippet panic on failure?
It stops. The one `if err != nil` branch ends in `panic(err)` — nothing continues past a failed call.
What does URL Redacted Hides Password print when run?
Execute the whole file; each printed line is prefixed with a label like `redacted:`, `user:`, `host:`.