maskUrlCredentials

Masks the credentials in every URL and connection string in a string, replacing each one with *****.

It masks two forms:

  • Credentials in a URL authority (scheme://user:pass@host). The credentials in "https://user:pass@host.com" become *****:*****, and in a log line holding several URLs each one is masked, whatever separates them. Only an @ inside an authority (between :// and the first /, ?, #, whitespace, or the next ://) separates credentials, so an @ in the path, query, or fragment is left alone. Credentials must be percent-encoded as RFC 3986 requires: an unencoded /, ?, # or space in a password ends the authority early.

  • A password query parameter or property, the key in any case, as JDBC URLs carry it: ?password=s3cret or &password=s3cret becomes password=*****, as does ;password=s3cret in a ;-separated URL such as SQL Server's. The key must follow ?, & or ; directly and be followed by = directly. A value after ? or & ends at the next & or whitespace, and one after ; at the next ; or whitespace or, when it starts with {, after the closing } (SQL Server's escape, in which }} stands for }). So an unencoded space in a password, an & in a query value or a ; in an unbraced property value ends it early. A quote does not end a value, since lenient drivers accept one in a password, so in a quoted or JSON URL the mask runs on past the closing quote.

Other forms are left as they are: the Oracle user/pass@host form, JSON-escaped (https:\/\/) and percent-encoded URLs, other keys (such as pwd, sslpassword, tokens and API keys), and a password key that starts the string or follows whitespace, as in a libpq host=db password=s3cret string. Mask those before logging.

The string is scanned once, so the time taken is linear in its length.

Return

the string with masked credentials, or the original string if no credentials are present