Basic Auth: what the header really contains

Base64 makes it look unreadable, but a Basic Auth header decodes in a second. What to keep in mind to use it safely.

What the header contains

HTTP “Basic” authentication, described in RFC 7617, is the simplest on the web. With every request, the client sends a header of this form:

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

That string is neither a token nor a signature: it is the username and password, joined by a colon, then encoded in Base64. The example above, taken from the RFC itself, decodes like this:

$ echo 'QWxhZGRpbjpvcGVuIHNlc2FtZQ==' | base64 -d
Aladdin:open sesame

In PowerShell, the same operation reads:

[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('QWxhZGRpbjpvcGVuIHNlc2FtZQ=='))

The username is Aladdin, the password open sesame. Nothing else is needed to read them.

Base64 is encoding, not encryption

Base64 is used to carry arbitrary bytes as text, with a 64-character alphabet that is safe in an HTTP header. It has no key: anyone can decode it, in either direction, in a fraction of a second. Its unreadable look is misleading.

The consequence is simple and important: the header is the credential. Anyone who sees it can log in as you, for as long as the password stays the same. And a header easily ends up where you don't expect it:

  • a curl command pasted into a ticket or a conversation;
  • a screenshot of the browser's developer tools;
  • the logs of a proxy, a gateway or an application that records incoming headers;
  • a HAR file attached to a bug report.

The right habit: treat an Authorization header exactly like the password it contains.

How the server asks for it

When a resource is protected, the server answers a request without credentials with a 401 Unauthorized status and a header announcing the expected scheme:

HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Administration", charset="UTF-8"

The browser then shows its login prompt, and automatically sends the Authorization header again with every request to the same area, usually until the browser is closed. The realm names the protected area. The charset="UTF-8" parameter, introduced by RFC 7617, tells the client how to encode non-ASCII characters.

Two details of the format are worth knowing: the username cannot contain a colon, since it is the separator; the password can, because the server splits at the first colon it finds.

Accented characters and UTF-8 encoding

Before being encoded in Base64, the username and password are converted to bytes, and not all clients use the same character table. RFC 7617 gives the example of the user test with the password 123£:

UTF-8    : dGVzdDoxMjPCow==
Latin-1  : dGVzdDoxMjOj

The two headers are different, and a server expecting one rejects the other without a clear error message. The kit's Basic Auth header generator always encodes in UTF-8, as the RFC recommends. If a password with accented characters is rejected even though it is correct, encoding is the first suspect; a random password made of ASCII characters avoids the issue.

Using it with curl

curl builds the header itself with the -u option:

# Username and password on the command line
curl -u alice:password https://api.example.com/report

# Username only: curl prompts for the password without displaying it
curl -u alice https://api.example.com/report

# Header already computed
curl -H "Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==" https://api.example.com/report

The first form is convenient, but the password stays in your shell history and may show up in the process list. The second asks for it on screen; in a script, a protected .netrc file (the --netrc option) or an environment variable keeps it out of the code. To compute the header yourself, use printf, which does not add a trailing newline, unlike echo:

printf '%s' 'alice:password' | base64

Using it safely

Basic Auth is perfectly suited to an internal API, a staging environment or a webhook, provided you follow a few rules:

  • HTTPS, always: TLS is what encrypts the header in transit. Over a plain connection, any intermediary can read it.
  • One account per client or integration: you can revoke a partner's access without touching the others, and the logs show who did what.
  • A long, random password, since nobody has to remember it.
  • Outside the code and the Git repository: in environment variables or platform secrets, like a secret key.
  • Logs that mask the Authorization header.

To protect an entire directory of a site with Apache or Nginx, the guide on how to password protect a directory covers the configuration in detail.

Sharing Basic Auth credentials

Handing over access to an API means handing over a username and password, or the header that contains them, which amounts to the same thing. With “Send with onlyU”, the generator passes the username and password to onlyU tab to tab in your browser, without putting them in an address or routing them through the kit's server. onlyU encrypts them in your browser and creates a link your recipient opens only once, valid for 24 hours, after which the link disappears. It is free, with no sign-up.

The credentials remain valid after reading: if you have the slightest doubt about their exposure, change the password on the server, and the old header stops working immediately.