Repository navigation
Description
The device sign-in from #130 works from a terminal as well as from the phone app. I built a Laravel Zero CLI against it (hampel/zero-dev-null). devnull login makes an ed25519 key, gets it approved at /mobile/authorize, and redeems the code at POST /api/v1/devices. After that, signed requests to /me, /log/* and /commits/{id}/replies work as intended.
The one step a terminal can't do well is getting the code back.
What happens today
redirect_uri has to start with devnull:// (config/mobile.php). After Approve, the browser gets a 303 to devnull://auth/callback?code=…&state=…. On a desktop nothing is registered for that scheme, so the browser drops the redirect without any message. The button goes inactive and nothing else happens. The only place the code shows up is the Location header of the POST in the developer tools' Network tab, which is where users of my CLI currently have to copy it from.
Proposal
Also accept a loopback redirect, the standard pattern for native apps (RFC 8252 §7.3): http://127.0.0.1:{port}/… or http://[::1]:{port}/…, on any port. The client listens on a random local port for the few seconds the sign-in takes, takes the code from the redirect, and shows a "you can close this tab" page. gcloud auth login and az login sign in the same way.
This needs a parsed check, not a new prefix. Adding http://127.0.0.1: to redirect_uri_prefixes isn't safe on its own, because starts_with also accepts http://127.0.0.1:[email protected]/callback. That URL's host is evil.example, and everything before the @ is just a username. Something like:
// A redirect_uri is the app's scheme, or a loopback address on any port (RFC 8252, 7.3 and 8.3).
static function (string $attribute, mixed $value, Closure $fail): void {
$uri = is_string($value) ? parse_url($value) : false;
$loopback = is_array($uri)
&& ($uri['scheme'] ?? null) === 'http'
&& in_array($uri['host'] ?? null, ['127.0.0.1', '[::1]'], true)
&& isset($uri['port'])
&& ! isset($uri['user']) && ! isset($uri['pass']) && ! isset($uri['fragment']);
if (! $loopback && ! str_starts_with((string) $value, 'devnull://')) {
$fail(__('The redirect URI must be the app\'s own or a loopback address.'));
}
}in ShowMobileAuthorizationRequest::parameterRules(), which CreateMobileAuthorizationRequest reuses. localhost is left out deliberately, as RFC 8252 §8.3 recommends: it is resolved by name, and it can resolve somewhere other than loopback.
Why it stays as safe as devnull://
- A code only ever goes to this machine. A loopback address can't send it anywhere else. Any process on the machine could listen on that port, but on a phone any app could also register
devnull://, which is why the flow already uses PKCE. - A stolen code is useless. It is single use, valid for 5 minutes, bound to the key's fingerprint and to the PKCE challenge. Redeeming it needs a signature from that key and the verifier, and neither ever leaves the client.
- The approval page doesn't change. It still shows the account and the key's fingerprint, and the user still clicks Approve.
What I checked
- The site's content security policy is only
frame-ancestors 'none'with noform-action, so nothing stops the browser following the 303 tohttp://127.0.0.1. - The rule above, run against ten URLs, accepts
devnull://…,http://127.0.0.1:53123/…andhttp://[::1]:53123/…. It refuses the@evil.exampleURL (whose host PHP parses asevil.example),localhost,https, a missing port, a fragment,http://127.0.0.1:80.evil.example/andhttp://127.0.0.1.evil.example:80/. The existingmax:255andnot_regex:/#/rules stay as they are. MobileAuthorization::redirectTo()already appendscodeandstatewith?or&as needed, so a loopback path works unchanged. So does the cancel link.
I'm happy to send a PR with tests in MobileAuthorizationControllerTest, covering what is accepted and what is refused (userinfo, localhost, https, no port, a fragment), if this direction works for you.
Activity