On this page
Secrets in URLs in shell scripts can leak even when the request uses HTTPS. A token embedded in a query string may be copied into shell history, exposed in a process listing, or recorded by systems that log request URLs. The safest approach is to keep credentials out of URLs when the service supports another authentication method.
This guide explains where leaks occur, shows an illustrative header-based alternative, and provides a response checklist for a URL that has already been shared.
Key Takeaways
- Prefer a supported authorization header or another service-approved authentication mechanism over a query-string credential.
- Moving a secret into a header does not automatically protect it: command arguments, shell tracing, and request logging can still expose it.
- Obtain secrets at runtime from an appropriate secret store or a protected prompt, rather than hard-coding them in scripts.
- Treat an accidentally shared credential as exposed: revoke or rotate it, investigate its use, and remove copies where possible.
Why URL secrets spread
Consider a fictional URL such as https://api.example.test/data?token=example-token. The token is part of the URL, so any system that records the full URL may also record the token.
Common exposure paths include:
- Shell history: Typing a command containing the URL directly into an interactive shell can save it in history. History behavior depends on the shell and its configuration.
- Process listings: Passing the URL as a command-line argument can make it visible to users or monitoring agents able to inspect process arguments while the command runs.
- Logs and monitoring: Clients, proxies, gateways, web servers, tracing systems, and error reports may capture URLs. A failed request can also place a URL in diagnostic output.
- Source control and collaboration: A URL in a script, ticket, chat message, or code review can be copied and retained long after its original use.
HTTPS protects the request in transit between the client and its TLS endpoint; it does not prevent the client or an endpoint, proxy, or logging system from recording the URL. Header values can be logged too, so they require their own controls.
Advertisement
Use an authorization header when the service supports it
Many services accept credentials in an authorization header, but authentication schemes differ. Check the service's documentation before changing a request. A service that requires a signed URL or a query parameter may not accept a bearer token in a header.
Avoid simply replacing a URL token with curl -H "Authorization: Bearer $TOKEN": the expanded header is still a command-line argument and may be visible in process listings.
The following illustrative Bash example reads a token from an interactive prompt and supplies a curl configuration through standard input instead of placing the token in curl's arguments. It assumes the service accepts a bearer token and the installed curl supports configuration from standard input. The example was not executed in this environment; check the documentation and test it against your own curl and service before use.
#!/usr/bin/env bash
set -o pipefail
set +x
trap 'unset API_TOKEN' EXIT
if ! IFS= read -r -s -p 'API token: ' API_TOKEN; then
printf '\nCould not read token.\n' >&2
exit 1
fi
printf '\n' >&2
# Restrict the value so it cannot inject curl configuration lines.
if [[ ! $API_TOKEN =~ ^[A-Za-z0-9._~+/-]+=*$ ]]; then
printf 'Token has an unsupported format.\n' >&2
exit 1
fi
if ! printf 'header = "Authorization: Bearer %s"\nurl = "https://api.example.test/v1/items"\n' "$API_TOKEN" |
curl --config - --fail --silent --show-error; then
printf 'Request failed.\n' >&2
exit 1
fi
Save the script locally and run it with Bash from an interactive terminal. The .test hostname is fictional; replace the endpoint only with one you are authorized to call. The prompt avoids typing the token as a shell command, and set +x disables shell tracing in the script. The format check deliberately accepts only a restricted token character set; do not remove it without addressing curl configuration escaping for the credential format your service uses.
This reduces some exposure, not all of it. The token exists in process memory and travels through a local pipe; sufficiently privileged local users or compromised processes may still access it. The server or intermediaries may log headers. The response is written to standard output, so protect it if it can contain sensitive data. The script does not follow redirects; if you add redirect handling, review where credentials can be sent and test the behavior for your client and service.
Use a secret store for automation
For unattended jobs, retrieve a short-lived credential at runtime from your organization's secret manager or use a service-supported workload identity mechanism. The exact commands and authentication flow depend on the platform; a secret store is a source for the credential, not a substitute for sending it through a supported authentication channel.
Give the job only the permissions it needs, restrict access to the secret, and ensure the retrieval command does not print the value into job logs. Environment variables can be convenient but are not universally private: diagnostic dumps, child processes, and privileged inspection may expose them. Avoid assuming that a masked CI log or an encrypted secret store prevents every downstream leak.
If the service requires a query-string credential, use a short-lived, narrowly scoped value where available. Limit who can see client, proxy, and server logs; configure URL redaction where possible; and avoid logging the full URL. Signed URLs may need their complete query string to work, so redact copies intended for diagnostics rather than altering the URL sent to the service.
Advertisement
If a secret URL was committed or shared
Do not paste a live credential or full secret-bearing URL into an online parser, ticket, or chat to investigate it. Work with an authorized security or incident-response team and follow your organization's process.
- Revoke or rotate the credential promptly. Removing the URL from a file does not invalidate a token already copied elsewhere. Check whether related signed URLs or sessions also need invalidation.
- Assess scope and activity. Identify what the credential could access and review available service audit logs for unauthorized use. Preserve evidence according to your incident process without spreading the secret further.
- Remove exposed copies where feasible. Replace the URL in the script and redact it from accessible tickets, logs, artifacts, and documentation. Coordinate any source-control history cleanup with repository owners; clones, caches, backups, and existing log exports may still contain it.
- Replace and deploy safely. Provision a scoped replacement, deliver it through an approved secret mechanism, and update affected jobs. Confirm the old credential is no longer accepted and that legitimate automation works with the replacement.
- Prevent recurrence. Review logging and tracing settings, add suitable secret-scanning checks, and document the supported authentication method. Verify redaction in a controlled test using fictional values.
Common Mistakes
- Assuming HTTPS makes a URL safe to log. Encryption in transit does not erase copies at either endpoint.
- Moving the token to a command-line header and declaring it hidden. Expanded arguments may still be inspectable.
- Enabling shell tracing or verbose HTTP diagnostics around secrets. These can print commands, headers, or URLs to logs.
- Deleting a committed URL without rotating the credential. History rewriting cannot guarantee that every copy is gone.
- Treating a parser as a security control. Parsing a URL does not remove secrets from logs or validate where a server-side request may safely go.
Best Practices
Use the authentication method the service supports with the least practical exposure. Keep credentials out of scripts and command arguments, restrict their scope and lifetime, and verify that logs and monitoring systems redact sensitive fields. Test failure paths as well as successful requests: errors often trigger additional diagnostics.
The requested OWASP guidance on sensitive information in URLs could not be consulted in this environment. Consult the current guidance and your service's documentation when reviewing a production implementation; the example above is illustrative and has not been tested here.
Conclusion
Keep secrets out of URLs whenever the service permits it, and protect the alternative credential path too. If a secret-bearing URL has already escaped, rotate the credential first and then work through the copies and logs it may have reached.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.