Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/openapi/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@
},
"/devices/authorize" : {
"post" : {
"description" : "The person approves in their browser. The answer is where the browser goes next: the app's loopback\naddress, with a code.",
"description" : "The person approves in their browser. The answer is where the browser goes next: the app's loopback\naddress (the desktop) or its own scheme (the phones), with a code.",
"operationId" : "authorizeDevice",
"requestBody" : {
"content" : {
Expand Down
3 changes: 2 additions & 1 deletion hperrors/errors_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var (
// ErrSyncSpaceFull refuses a write that would take the space past its limit.
ErrSyncSpaceFull = NewErr(ErrConflict, "ERR_SYNC_SPACE_FULL")

// ErrDeviceRedirectInvalid is an address that is no app's loopback listener: a code never goes anywhere else.
// ErrDeviceRedirectInvalid is an address that is neither an app's loopback listener nor the phones' redirect: a
// code never goes anywhere else.
ErrDeviceRedirectInvalid = NewErr(ErrArgumentInvalid, "ERR_DEVICE_REDIRECT_INVALID")
// ErrDeviceCodeInvalid is a code that is unknown, used already, too old, or not this app's. An app signs in again.
ErrDeviceCodeInvalid = NewErr(ErrArgumentInvalid, "ERR_DEVICE_CODE_INVALID")
Expand Down
2 changes: 1 addition & 1 deletion interface/api/handler/sessionhandler/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// AuthorizeDevice lets an app on the person's device sign in
// @Summary Lets an app on the person's device sign in
// @Description The person approves in their browser. The answer is where the browser goes next: the app's loopback
// @Description address, with a code.
// @Description address (the desktop) or its own scheme (the phones), with a code.
// @Tags Sessions
// @Produce json
// @Id authorizeDevice
Expand Down
17 changes: 12 additions & 5 deletions usecase/sessionuc/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@
)

const (
deviceSecretLen = 32

Check failure on line 30 in usecase/sessionuc/device.go

View workflow job for this annotation

GitHub Actions / build (1.27.x)

File is not properly formatted (gci)
deviceCallback = "/callback"
// The phones' redirect: the app's own scheme (RFC 8252, 7.1), which the browser sheet (iOS) or tab (Android)
// comes back to. Exactly this: the app is the only one that asks for it.
deviceAppRedirect = "com.hivepaas.shellwing://server"
deviceAuthCodeTTL = 2 * time.Minute // the app is waiting
)

// How an app signs in (RFC 8252): it opens the browser at the server, whatever sign-in the server has happens there,
// and the person approves the device. The server answers with a code, which goes to the app's loopback address, and
// the app exchanges it for tokens with the verifier only it has (RFC 7636).
// and the person approves the device. The server answers with a code, which goes to the app's loopback address (the
// desktop) or its own scheme (the phones), and the app exchanges it for tokens with the verifier only it has
// (RFC 7636). Another app that claimed the scheme could catch a code, but not use it without the verifier.
//
// Its tokens reach sync and nothing else: an admin's device cannot manage the server.

Expand All @@ -49,10 +53,13 @@
return hex.EncodeToString(sum[:])
}

// validDeviceRedirect says whether the address is an app's loopback listener, the only place a code may go. Anything
// else would let a web page have the browser hand it the code. Literal addresses, as RFC 8252 asks: a name could
// resolve elsewhere.
// validDeviceRedirect says whether the address is an app's loopback listener or the phones' redirect, the only places
// a code may go. Anything else would let a web page have the browser hand it the code. Literal addresses, as RFC 8252
// asks: a name could resolve elsewhere.
func validDeviceRedirect(raw string) bool {
if raw == deviceAppRedirect {
return true
}
parsed, err := url.Parse(raw)
if err != nil || parsed.Scheme != "http" || parsed.Path != deviceCallback || parsed.User != nil ||
parsed.RawQuery != "" || parsed.Fragment != "" {
Expand Down
16 changes: 14 additions & 2 deletions usecase/sessionuc/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import (
"github.com/stretchr/testify/assert"
)

// A code goes nowhere but an app's own loopback listener, or a web page could have the browser hand it over.
// A code goes nowhere but an app's own loopback listener or the phones' redirect, or a web page could have the browser
// hand it over.
func TestValidDeviceRedirect(t *testing.T) {
for _, good := range []string{"http://127.0.0.1:53124/callback", "http://[::1]:53124/callback"} {
for _, good := range []string{
"http://127.0.0.1:53124/callback",
"http://[::1]:53124/callback",
"com.hivepaas.shellwing://server",
} {
assert.True(t, validDeviceRedirect(good), good)
}
for _, bad := range []string{
Expand All @@ -30,6 +35,13 @@ func TestValidDeviceRedirect(t *testing.T) {
"http://10.0.0.1:53124/callback",
"javascript:alert(1)",
"http://127.0.0.1:53124//evil.example.com/callback",
"com.hivepaas.shellwing://server/",
"com.hivepaas.shellwing://server?x=1",
"com.hivepaas.shellwing://auth",
"com.hivepaas.shellwing://evil.example.com",
"com.hivepaas.shellwing.evil://server",
"COM.HIVEPAAS.SHELLWING://server",
"evil://server",
} {
assert.False(t, validDeviceRedirect(bad), bad)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Laptop, ShieldAlert } from "lucide-react";
import { Laptop, ShieldAlert, Smartphone } from "lucide-react";
import { useSearchParams } from "react-router";

import { LogoIcon } from "@assets/icons";
Expand Down Expand Up @@ -35,6 +35,8 @@ export function ConnectDeviceRoute() {
const platform = params.get("platform") ?? "";

const complete = redirectUri !== "" && codeChallenge !== "" && state !== "" && deviceName !== "";
// The phones come back to the app's own scheme; the desktop to a port of its own.
const DeviceIcon = platform === "ios" || platform === "android" ? Smartphone : Laptop;

return (
<AuthenticationLayout>
Expand All @@ -50,7 +52,7 @@ export function ConnectDeviceRoute() {
{complete ? (
<CardContent className="flex flex-col items-center gap-4 text-center">
<div className="rounded-full bg-muted p-3 text-foreground">
<Laptop className="h-6 w-6" />
<DeviceIcon className="h-6 w-6" />
</div>
<CardTitle className="text-base">Allow this device to sync?</CardTitle>
<CardDescription>
Expand Down
Loading