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.
Code
package main
import (
"fmt"
"net/url"
)
func main() {
endpoint, err := url.Parse("postgres://admin:[email protected]: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
Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.Password Entropy EstimatorEstimate a password's alphabet size and Shannon entropy in bits, then translate that into offline crack times at realistic guessing rates.Password Strength MeterScore a password from 0 to 100 based on length and character classes, and get clear suggestions to make it stronger. Everything runs locally.
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:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.