Add devbox socket sharing and private-link CLI (#486 A)
Complete Task 5b with local socket checks, service ACL setup, allow lists for every target kind, and owner-only list details. Reuse the registrar's allow validator and apply approved DBX-C94 through DBX-C123 copy. Targeted root, edge and devbox tests, root race tests, Darwin builds and Darwin root vet pass. Darwin edge vet retains existing readerConn test helper failures. Controller review and census approval remain pending.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+583
−38
approved_devbox_copy_test.go+4 −1devbox_cli.go+162 −23devbox_cli_test.go+6 −6devbox_identity_cli_test.go+357 −0infra/devbox/README.md+30 −0internal/devbox/registrar.go+20 −4internal/edge/devboxfront.go+3 −3internal/edge/devboxfront_test.go+1 −1
diff --git a/approved_devbox_copy_test.go b/approved_devbox_copy_test.go| index 175a2a6b6..87ac861fe 100644 |
| --- a/approved_devbox_copy_test.go |
| +++ b/approved_devbox_copy_test.go |
| @@ -34,10 +34,13 @@ var devboxCopyIDs = func() []string { |
| // devboxCopyBuilt is every deck id whose text the code or docs now carry. |
| // The task that writes a row's text adds its id here in the same commit. |
| +// Identity replaces C2/C4/C13/C26 with C94/C113/C109/C97 respectively. |
| var devboxCopyBuilt = []string{ |
| - "DBX-C1", "DBX-C2", "DBX-C3", "DBX-C4", "DBX-C5", "DBX-C6", "DBX-C7", "DBX-C8", "DBX-C9", "DBX-C10", "DBX-C11", "DBX-C12", "DBX-C13", "DBX-C14", "DBX-C15", "DBX-C16", "DBX-C17", "DBX-C18", "DBX-C19", "DBX-C20", "DBX-C21", "DBX-C22", "DBX-C23", "DBX-C24", "DBX-C25", "DBX-C26", "DBX-C27", "DBX-C28", "DBX-C29", "DBX-C30", "DBX-C31", "DBX-C32", "DBX-C33", "DBX-C34", "DBX-C35", "DBX-C36", "DBX-C37", "DBX-C38", "DBX-C39", "DBX-C40", "DBX-C41", "DBX-C42", "DBX-C43", "DBX-C44", "DBX-C45", "DBX-C46", "DBX-C47", "DBX-C48", "DBX-C49", "DBX-C50", "DBX-C51", "DBX-C52", "DBX-C53", "DBX-C54", "DBX-C55", "DBX-C56", "DBX-C57", |
| + "DBX-C1", "DBX-C3", "DBX-C5", "DBX-C6", "DBX-C7", "DBX-C8", "DBX-C9", "DBX-C10", "DBX-C11", "DBX-C12", "DBX-C14", "DBX-C15", "DBX-C16", "DBX-C17", "DBX-C18", "DBX-C19", "DBX-C20", "DBX-C21", "DBX-C22", "DBX-C23", "DBX-C24", "DBX-C25", "DBX-C27", "DBX-C28", "DBX-C29", "DBX-C30", "DBX-C31", "DBX-C32", "DBX-C33", "DBX-C34", "DBX-C35", "DBX-C36", "DBX-C37", "DBX-C38", "DBX-C39", "DBX-C40", "DBX-C41", "DBX-C42", "DBX-C43", "DBX-C44", "DBX-C45", "DBX-C46", "DBX-C47", "DBX-C48", "DBX-C49", "DBX-C50", "DBX-C51", "DBX-C52", "DBX-C53", "DBX-C54", "DBX-C55", "DBX-C56", "DBX-C57", |
| // macOS (#488) |
| "DBX-C58", "DBX-C59", "DBX-C60", "DBX-C61", "DBX-C62", "DBX-C63", "DBX-C64", "DBX-C65", "DBX-C66", "DBX-C67", "DBX-C68", "DBX-C69", "DBX-C70", "DBX-C71", "DBX-C72", "DBX-C73", "DBX-C74", "DBX-C75", "DBX-C76", "DBX-C77", "DBX-C78", "DBX-C79", "DBX-C80", "DBX-C81", "DBX-C82", "DBX-C83", "DBX-C84", "DBX-C85", "DBX-C86", "DBX-C87", "DBX-C88", "DBX-C89", "DBX-C90", "DBX-C91", "DBX-C92", "DBX-C93", |
| + // identity (#486 A) |
| + "DBX-C94", "DBX-C95", "DBX-C96", "DBX-C97", "DBX-C98", "DBX-C99", "DBX-C100", "DBX-C101", "DBX-C102", "DBX-C103", "DBX-C104", "DBX-C105", "DBX-C106", "DBX-C107", "DBX-C108", "DBX-C109", "DBX-C110", "DBX-C111", "DBX-C112", "DBX-C113", "DBX-C114", "DBX-C115", "DBX-C116", "DBX-C117", "DBX-C118", "DBX-C119", "DBX-C120", "DBX-C121", "DBX-C122", "DBX-C123", |
| } |
| func mustParseDevboxRecord(t *testing.T) []copyRow { |
diff --git a/devbox_cli.go b/devbox_cli.go| index 946b3697d..13fa2324e 100644 |
| --- a/devbox_cli.go |
| +++ b/devbox_cli.go |
| @@ -43,15 +43,15 @@ import ( |
| // Copy deck rows (DBX-Cn). Each is the row's text, byte for byte. |
| const ( |
| - devboxUsageShare = "usage: carlos devbox share [--port <port> or --dir <path>] [<name>]" // DBX-C2 |
| + devboxUsageShare = "usage: carlos devbox share [--port <port>, --socket <path> or --dir <path>] [--allow <logins>] [<name>]" // DBX-C94 |
| devboxUsageUnshare = "usage: carlos devbox unshare [<name>]" // DBX-C3 |
| - devboxUsageList = "usage: carlos devbox list" // DBX-C4 |
| + devboxUsageList = "usage: carlos devbox list [--allowed]" // DBX-C113 |
| devboxHelpShare = "share publishes https://<name>.<devbox domain> to people on the tailnet. without a name it uses your git branch" // DBX-C6 |
| devboxHelpPort = "the port your dev server listens on, on 127.0.0.1. the link follows it live" // DBX-C7 |
| devboxHelpDir = "a folder of static files to publish. the link serves a copy, and sharing again replaces it" // DBX-C8 |
| devboxHelpUnshare = "unshare removes a link you published. without a name it uses your git branch" // DBX-C9 |
| devboxHelpList = "list shows every link on this devbox, who owns it and whether it is answering" // DBX-C10 |
| - devboxListHeader = "name owner kind target age state" // DBX-C13 |
| + devboxListHeader = "name owner kind target access age state" // DBX-C109 |
| devboxListEmpty = "no links yet. publish one with: carlos devbox share --port <port> <name>" // DBX-C14 |
| devboxKindLive = "live" // DBX-C15 |
| devboxKindFolder = "folder" // DBX-C16 |
| @@ -59,13 +59,52 @@ const ( |
| devboxNotAnswering = "not answering" // DBX-C18 |
| devboxServing = "serving" // DBX-C19 |
| devboxOwnerGone = "account removed" // DBX-C20 |
| - devboxEitherFlag = "give either --port for a dev server or --dir for a folder, not both and not neither" // DBX-C26 |
| + devboxEitherFlag = "give one of --port for a dev server, --socket for a socket or --dir for a folder" // DBX-C97 |
| devboxSpoolFull = "the devbox has no room for another folder. remove one you no longer need with carlos devbox unshare, or tell whoever runs this devbox" // DBX-C30 |
| devboxSandboxHint = "if you are inside an agent sandbox, that is the likely cause: the sandbox blocks the socket. run this command outside the sandbox" // DBX-C34 |
| devboxNotUnderstood = "the devbox service answered with something this carlos does not understand. update carlos and try again" // DBX-C35 |
| devboxInternal = "the devbox service could not finish that. try again, or tell whoever runs this devbox" // DBX-C54 |
| ) |
| +// Identity and private targets (#486 A). |
| +const ( |
| + devboxHelpAllow = "tailnet logins that may open the link, separated by commas, such as alice@github. anyone else sees a page saying the link is private. without --allow, everyone on the tailnet can open it. sharing again replaces the list" // DBX-C96 |
| + devboxSocketUnsupported = "--socket works on Linux devboxes only for now. on this devbox, use --port" // DBX-C104 |
| + devboxAllowUnsupported = "--allow works on Linux devboxes only for now. on this devbox, every link is open to the tailnet" // DBX-C105 |
| + devboxKindSocket = "socket" // DBX-C110 |
| + devboxPrivateAccess = "private" // DBX-C111 |
| + devboxTailnetAccess = "tailnet" // DBX-C112 |
| + devboxHelpAllowed = "also show who can open your private links" // DBX-C114 |
| +) |
| + |
| +func devboxHelpSocket(dir string) string { |
| + return fmt.Sprintf("a Unix socket your tool listens on, inside %s. share makes it private to you and the devbox service. if your tool restarts and makes a new socket, share again", dir) // DBX-C95 |
| +} |
| +func devboxSocketPathRefusal(path, dir string) string { |
| + return fmt.Sprintf("%s is not directly inside %s. have your tool create its socket there, then share it", path, dir) // DBX-C98 |
| +} |
| +func devboxNotASocket(path string) string { |
| + return fmt.Sprintf("%s is not a socket. give the path of the Unix socket your tool listens on", path) // DBX-C99 |
| +} |
| +func devboxSocketNotYours(path string) string { |
| + return fmt.Sprintf("%s belongs to another account. you can only share a socket you created", path) // DBX-C100 |
| +} |
| +func devboxSocketUnreachable(path string) string { |
| + return fmt.Sprintf("nothing is answering on %s. start your tool, then share again", path) // DBX-C101 |
| +} |
| +func devboxSocketACLFailed(path string) string { |
| + return fmt.Sprintf("could not let the devbox service open %s. check that the socket is yours, or tell whoever runs this devbox", path) // DBX-C102 |
| +} |
| +func devboxMissingServiceUser(user string) string { |
| + return fmt.Sprintf("this machine has no devbox service user called %s. tell whoever runs this devbox", user) // DBX-C103 |
| +} |
| +func devboxBadAllow(login string) string { |
| + return fmt.Sprintf("%s is not a tailnet login. give logins such as alice@github, separated by commas, with no spaces", login) // DBX-C106 |
| +} |
| +func devboxAllowedAccess(logins string) string { |
| + return fmt.Sprintf("private: %s", logins) // DBX-C115 |
| +} |
| + |
| // serve's rows (macOS plan #488). |
| const ( |
| devboxUsageServe = "usage: carlos devbox serve [--env-file <env file>]" // DBX-C58 |
| @@ -185,10 +224,14 @@ type devboxCLI struct { |
| socket string |
| branch func() (string, error) |
| now func() time.Time |
| + goos string |
| + uid int |
| + serviceUID func() (uint32, error) |
| + grantACL func(string, uint32) error |
| } |
| func newDevboxCLI(stdout, stderr io.Writer) *devboxCLI { |
| - return &devboxCLI{stdout: stdout, stderr: stderr, socket: devboxSocketPath(), branch: gitCurrentBranch, now: time.Now} |
| + return &devboxCLI{stdout: stdout, stderr: stderr, socket: devboxSocketPath(), branch: gitCurrentBranch, now: time.Now, goos: runtime.GOOS, uid: os.Geteuid(), serviceUID: devboxServiceUID, grantACL: grantServiceACL} |
| } |
| func runDevbox(args []string) { |
| @@ -314,18 +357,46 @@ func (c *devboxCLI) share(args []string) error { |
| fs := c.flagSet("share", devboxUsageShare, devboxHelpShare) |
| port := fs.String("port", "", devboxHelpPort) |
| dir := fs.String("dir", "", devboxHelpDir) |
| + socket := fs.String("socket", "", devboxHelpSocket(devboxSocketsDir())) |
| + allowFlag := fs.String("allow", "", devboxHelpAllow) |
| pos, err := parseInterspersed(fs, args) |
| if err != nil { |
| return err |
| } |
| + portSet, dirSet, socketSet := flagGiven(fs, "port"), flagGiven(fs, "dir"), flagGiven(fs, "socket") |
| + allowSet := flagGiven(fs, "allow") |
| + if c.goos != "linux" { |
| + if socketSet { |
| + return refuseWith(devboxSocketUnsupported) |
| + } |
| + if allowSet { |
| + return refuseWith(devboxAllowUnsupported) |
| + } |
| + } |
| if len(pos) > 1 { |
| fs.Usage() |
| return errDevboxUsage |
| } |
| - portSet, dirSet := flagGiven(fs, "port"), flagGiven(fs, "dir") |
| - if portSet == dirSet { |
| + targets := 0 |
| + for _, set := range []bool{portSet, dirSet, socketSet} { |
| + if set { |
| + targets++ |
| + } |
| + } |
| + if targets != 1 { |
| return refuseWith(devboxEitherFlag) |
| } |
| + var allow []string |
| + if allowSet { |
| + var invalid *devbox.AllowError |
| + allow, err = devbox.ValidateAllow(strings.Split(*allowFlag, ",")) |
| + if err != nil { |
| + if errors.As(err, &invalid) { |
| + return refuseWith(devboxBadAllow(invalid.Login)) |
| + } |
| + return err |
| + } |
| + } |
| var portN int |
| if portSet { |
| portN, err = strconv.Atoi(*port) |
| @@ -337,21 +408,36 @@ func (c *devboxCLI) share(args []string) error { |
| if err != nil { |
| return err |
| } |
| - rc := refusalContext{name: name, port: *port, path: *dir} |
| + rc := refusalContext{name: name, port: *port, path: *dir, allow: *allowFlag} |
| var entry devbox.ListEntry |
| if portSet { |
| body, _ := json.Marshal(struct { |
| - Name string `json:"name"` |
| - Port int `json:"port"` |
| - }{name, portN}) |
| + Name string `json:"name"` |
| + Port int `json:"port"` |
| + Allow []string `json:"allow"` |
| + }{name, portN, allow}) |
| err = c.call(http.MethodPost, "/share/live", "application/json", body, rc, &entry) |
| + } else if socketSet { |
| + rc.path = *socket |
| + var path string |
| + path, err = c.prepareSocket(*socket) |
| + if err != nil { |
| + return err |
| + } |
| + body, _ := json.Marshal(struct { |
| + Name string `json:"name"` |
| + Path string `json:"path"` |
| + Allow []string `json:"allow"` |
| + }{name, path, allow}) |
| + err = c.call(http.MethodPost, "/share/socket", "application/json", body, rc, &entry) |
| } else { |
| var site []byte |
| site, err = packDevboxFolder(*dir) |
| if err != nil { |
| return err |
| } |
| - err = c.call(http.MethodPost, "/share/folder?name="+url.QueryEscape(name), "application/gzip", site, rc, &entry) |
| + query := url.Values{"name": {name}, "allow": allow} |
| + err = c.call(http.MethodPost, "/share/folder?"+query.Encode(), "application/gzip", site, rc, &entry) |
| } |
| if err != nil { |
| return err |
| @@ -437,6 +523,7 @@ func (c *devboxCLI) unshare(args []string) error { |
| func (c *devboxCLI) list(args []string) error { |
| fs := c.flagSet("list", devboxUsageList, devboxHelpList) |
| + allowed := fs.Bool("allowed", false, devboxHelpAllowed) |
| pos, err := parseInterspersed(fs, args) |
| if err != nil { |
| return err |
| @@ -461,6 +548,15 @@ func (c *devboxCLI) list(args []string) error { |
| kind, state := devboxKindLive, devboxNotAnswering |
| if e.Kind == devbox.KindFolder { |
| kind = devboxKindFolder |
| + } else if e.Kind == devbox.KindSocket { |
| + kind = devboxKindSocket |
| + } |
| + access := devboxTailnetAccess |
| + if e.Private { |
| + access = devboxPrivateAccess |
| + if *allowed && e.OwnerUID == uint32(c.uid) && len(e.Allow) > 0 { |
| + access = devboxAllowedAccess(strings.Join(e.Allow, ",")) |
| + } |
| } |
| if e.Answering { |
| state = devboxAnswering |
| @@ -468,7 +564,7 @@ func (c *devboxCLI) list(args []string) error { |
| state = devboxServing |
| } |
| } |
| - fmt.Fprintln(tw, strings.Join([]string{e.Name, devboxOwner(e), kind, e.Target, devboxAge(now.Sub(e.Created)), state}, "\t")) |
| + fmt.Fprintln(tw, strings.Join([]string{e.Name, devboxOwner(e), kind, e.Target, access, devboxAge(now.Sub(e.Created)), state}, "\t")) |
| } |
| return tw.Flush() |
| } |
| @@ -498,7 +594,7 @@ func devboxAge(d time.Duration) string { |
| // refusalContext is what a refusal's sentence fills in: the name, the port |
| // and the folder as the person typed them. |
| -type refusalContext struct{ name, port, path string } |
| +type refusalContext struct{ name, port, path, allow string } |
| // render turns a registrar refusal into its deck sentence. |
| func (rc refusalContext) render(w devbox.WireError) error { |
| @@ -520,6 +616,18 @@ func (rc refusalContext) render(w devbox.WireError) error { |
| return refuseWith(devboxNotYours(w.Owner)) |
| case devbox.CodeUnknownLink: |
| return refuseWith(devboxUnknownLink(rc.name)) |
| + case devbox.CodeSocketPath: |
| + return refuseWith(devboxSocketPathRefusal(rc.path, devboxSocketsDir())) |
| + case devbox.CodeNotASocket: |
| + return refuseWith(devboxNotASocket(rc.path)) |
| + case devbox.CodeSocketNotYours: |
| + return refuseWith(devboxSocketNotYours(rc.path)) |
| + case devbox.CodeSocketUnreachable: |
| + return refuseWith(devboxSocketUnreachable(rc.path)) |
| + case devbox.CodeSocketUnsupported: |
| + return refuseWith(devboxSocketUnsupported) |
| + case devbox.CodeBadAllow: |
| + return refuseWith(devboxBadAllow(rc.allow)) |
| case devbox.CodeInternal: |
| return refuseWith(devboxInternal) |
| } |
| @@ -679,10 +787,7 @@ func configureDevboxIdentity(goos string, r *devbox.Registrar, cfg *edge.DevboxC |
| if goos != "linux" { |
| return nil |
| } |
| - dir := os.Getenv("CARLOS_DEVBOX_SOCKETS_DIR") |
| - if dir == "" { |
| - dir = devbox.DefaultSocketsDir |
| - } |
| + dir := devboxSocketsDir() |
| if err := prepareDevboxSocketsDir(dir, os.Geteuid()); err != nil { |
| return err |
| } |
| @@ -726,12 +831,46 @@ func prepareDevboxSocketsDir(path string, uid int) error { |
| return nil |
| } |
| -func devboxServiceUID() (uint32, error) { |
| - name := os.Getenv("CARLOS_DEVBOX_SERVICE_USER") |
| - if name == "" { |
| - name = "carlos-devbox" |
| +func devboxSocketsDir() string { |
| + if dir := os.Getenv("CARLOS_DEVBOX_SOCKETS_DIR"); dir != "" { |
| + return dir |
| + } |
| + return devbox.DefaultSocketsDir |
| +} |
| + |
| +func devboxServiceUser() string { |
| + if name := os.Getenv("CARLOS_DEVBOX_SERVICE_USER"); name != "" { |
| + return name |
| } |
| - u, err := user.Lookup(name) |
| + return "carlos-devbox" |
| +} |
| + |
| +// Local checks precede the ACL write. The registrar checks the socket and |
| +// connected peer again before it registers the link. |
| +func (c *devboxCLI) prepareSocket(path string) (string, error) { |
| + clean := filepath.Clean(path) |
| + if !filepath.IsAbs(clean) || filepath.Dir(clean) != filepath.Clean(devboxSocketsDir()) { |
| + return "", refuseWith(devboxSocketPathRefusal(path, devboxSocketsDir())) |
| + } |
| + fi, err := os.Lstat(clean) |
| + if err != nil || fi.Mode()&os.ModeSocket == 0 { |
| + return "", refuseWith(devboxNotASocket(path)) |
| + } |
| + if uid, ok := fileOwner(fi); !ok || uid != c.uid { |
| + return "", refuseWith(devboxSocketNotYours(path)) |
| + } |
| + uid, err := c.serviceUID() |
| + if err != nil { |
| + return "", refuseWith(devboxMissingServiceUser(devboxServiceUser())) |
| + } |
| + if err := c.grantACL(clean, uid); err != nil { |
| + return "", refuseWith(devboxSocketACLFailed(path)) |
| + } |
| + return clean, nil |
| +} |
| + |
| +func devboxServiceUID() (uint32, error) { |
| + u, err := user.Lookup(devboxServiceUser()) |
| if err != nil { |
| return 0, err |
| } |
diff --git a/devbox_cli_test.go b/devbox_cli_test.go| index d0b3f241c..af2ef3d2a 100644 |
| --- a/devbox_cli_test.go |
| +++ b/devbox_cli_test.go |
| @@ -154,9 +154,9 @@ func TestDevboxShareNameRefusals(t *testing.T) { |
| {"port not a number", nil, []string{"share", "--port", "http", "shop"}, |
| "http is not a port. use a number from 1 to 65535. the link points at 127.0.0.1 on this machine\n"}, |
| {"neither", nil, []string{"share", "shop"}, |
| - "give either --port for a dev server or --dir for a folder, not both and not neither\n"}, |
| + "give one of --port for a dev server, --socket for a socket or --dir for a folder\n"}, |
| {"both", nil, []string{"share", "--port", "8080", "--dir", ".", "shop"}, |
| - "give either --port for a dev server or --dir for a folder, not both and not neither\n"}, |
| + "give one of --port for a dev server, --socket for a socket or --dir for a folder\n"}, |
| {"dir is a file", nil, []string{"share", "--dir", file, "shop"}, file + " is not a folder\n"}, |
| {"dir is missing", nil, []string{"share", "--dir", file + "-gone", "shop"}, file + "-gone is not a folder\n"}, |
| {"unknown command", nil, []string{"publish"}, |
| @@ -295,10 +295,10 @@ func TestDevboxListTable(t *testing.T) { |
| } |
| lines := strings.Split(strings.TrimSuffix(out, "\n"), "\n") |
| want := [][]string{ |
| - {"name", "owner", "kind", "target", "age", "state"}, |
| - {"api", "alice", "live", fmt.Sprintf("127.0.0.1:%d", upPort), "3h", "answering"}, |
| - {"docs", "alice", "folder", "3h", "serving"}, // a folder's target is empty (ruling R10) |
| - {"shop", "alice", "live", fmt.Sprintf("127.0.0.1:%d", downPort), "3h", "not", "answering"}, |
| + {"name", "owner", "kind", "target", "access", "age", "state"}, |
| + {"api", "alice", "live", fmt.Sprintf("127.0.0.1:%d", upPort), "tailnet", "3h", "answering"}, |
| + {"docs", "alice", "folder", "tailnet", "3h", "serving"}, // a folder's target is empty (ruling R10) |
| + {"shop", "alice", "live", fmt.Sprintf("127.0.0.1:%d", downPort), "tailnet", "3h", "not", "answering"}, |
| } |
| if len(lines) != len(want) { |
| t.Fatalf("list:\n%s", out) |
diff --git a/devbox_identity_cli_test.go b/devbox_identity_cli_test.go| new file mode 100644 |
| index 000000000..979b0c8dc |
| --- /dev/null |
| +++ b/devbox_identity_cli_test.go |
| @@ -0,0 +1,357 @@ |
| +//go:build linux |
| + |
| +package main |
| + |
| +import ( |
| + "net" |
| + "os" |
| + "os/user" |
| + "path/filepath" |
| + "slices" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/devbox" |
| +) |
| + |
| +func devboxSocketTarget(t *testing.T, h *devboxHarness) string { |
| + t.Helper() |
| + dir := t.TempDir() |
| + h.r.SocketsDir = dir |
| + t.Setenv("CARLOS_DEVBOX_SOCKETS_DIR", dir) |
| + u, err := user.Current() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Setenv("CARLOS_DEVBOX_SERVICE_USER", u.Username) |
| + path := filepath.Join(dir, "tool.sock") |
| + ln, err := net.Listen("unix", path) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + done := make(chan struct{}) |
| + go func() { |
| + defer close(done) |
| + for { |
| + c, err := ln.Accept() |
| + if err != nil { |
| + return |
| + } |
| + c.Close() |
| + } |
| + }() |
| + t.Cleanup(func() { ln.Close(); <-done }) |
| + return path |
| +} |
| + |
| +func TestDevboxShareSocketCLI(t *testing.T) { |
| + h := newDevboxHarness(t, map[uint32]string{me(): "alice"}) |
| + path := devboxSocketTarget(t, h) |
| + out, errOut, code := runDevboxCLI(t, h.sock, nil, "share", "--socket", path, "shop") |
| + if code != 0 || out != "https://shop.devbox.example.com\n" || errOut != "" { |
| + t.Fatalf("share = %d, %q, %q", code, out, errOut) |
| + } |
| + k, ok := h.r.Links.Get("shop") |
| + if !ok || k.Kind != devbox.KindSocket || k.Socket != path || k.OwnerUID != me() { |
| + t.Fatalf("link = %+v, %v", k, ok) |
| + } |
| + out, errOut, code = runDevboxCLI(t, h.sock, nil, "list") |
| + if code != 0 || !strings.Contains(out, path) || !strings.Contains(out, "socket") || !strings.Contains(out, "answering") { |
| + t.Fatalf("list = %d, %q, %q", code, out, errOut) |
| + } |
| +} |
| + |
| +func TestDevboxShareAllowFlag(t *testing.T) { |
| + for _, kind := range []string{"port", "dir", "socket"} { |
| + t.Run(kind, func(t *testing.T) { |
| + h := newDevboxHarness(t, map[uint32]string{me(): "alice"}) |
| + target := "8080" |
| + if kind == "dir" { |
| + target = t.TempDir() |
| + if err := os.WriteFile(filepath.Join(target, "index.html"), []byte("hi"), 0600); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if kind == "socket" { |
| + target = devboxSocketTarget(t, h) |
| + } |
| + for _, want := range [][]string{{"alice@github", "bob+dev@example.com"}, {"carol@github"}, nil} { |
| + args := []string{"share", "shop", "--" + kind, target} |
| + if want != nil { |
| + raw := strings.Join(want, ",") |
| + if len(want) == 2 { |
| + raw = "Alice@GitHub,bob+dev@example.com,ALICE@GITHUB" |
| + } |
| + args = append(args, "--allow", raw) |
| + } |
| + out, errOut, code := runDevboxCLI(t, h.sock, nil, args...) |
| + if code != 0 { |
| + t.Fatalf("share = %d, %q, %q", code, out, errOut) |
| + } |
| + k, _ := h.r.Links.Get("shop") |
| + if !slices.Equal(k.Allow, want) { |
| + t.Fatalf("stored allow = %q, want %q", k.Allow, want) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestDevboxShareAllowMalformed(t *testing.T) { |
| + for _, bad := range []string{"", "bad login", "tab\tlogin", "é@github", strings.Repeat("a", devbox.MaxAllowLogin+1)} { |
| + t.Run(bad, func(t *testing.T) { |
| + out, errOut, code := runDevboxCLI(t, "/missing", nil, "share", "--port", "8080", "--allow", "alice@github,"+bad, "shop") |
| + want := bad + " is not a tailnet login. give logins such as alice@github, separated by commas, with no spaces\n" |
| + if code != 1 || out != "" || errOut != want { |
| + t.Fatalf("share = %d, %q, %q; want %q", code, out, errOut, want) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestDevboxListAccessAndAllowed(t *testing.T) { |
| + h := newDevboxHarness(t, map[uint32]string{me(): "alice", me() + 1: "bob"}) |
| + now := time.Now() |
| + for _, link := range []devbox.Link{ |
| + {Name: "mine", OwnerUID: me(), Kind: devbox.KindLive, Port: 1, Allow: []string{"alice@github", "carol@github"}, Created: now, Updated: now}, |
| + {Name: "other", OwnerUID: me() + 1, Kind: devbox.KindLive, Port: 1, Allow: []string{"bob@github"}, Created: now, Updated: now}, |
| + {Name: "open", OwnerUID: me(), Kind: devbox.KindLive, Port: 1, Created: now, Updated: now}, |
| + } { |
| + if err := h.r.Links.Put(link); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + for _, allowed := range []bool{false, true} { |
| + args := []string{"list"} |
| + if allowed { |
| + args = append(args, "--allowed") |
| + } |
| + out, errOut, code := runDevboxCLI(t, h.sock, nil, args...) |
| + if code != 0 { |
| + t.Fatalf("list = %d, %q, %q", code, out, errOut) |
| + } |
| + lines := strings.Split(strings.TrimSpace(out), "\n") |
| + if strings.Join(strings.Fields(lines[0]), " ") != "name owner kind target access age state" { |
| + t.Fatalf("header = %q", lines[0]) |
| + } |
| + if strings.Contains(out, "bob@github") { |
| + t.Fatalf("other owner's list exposed: %q", out) |
| + } |
| + if allowed != strings.Contains(out, "private: alice@github,carol@github") { |
| + t.Fatalf("own allow list = %q", out) |
| + } |
| + if !allowed && strings.Contains(out, "@") { |
| + t.Fatalf("list exposed without --allowed: %q", out) |
| + } |
| + if !strings.Contains(lines[1], "private") || !strings.Contains(lines[2], "tailnet") || !strings.Contains(lines[3], "private") { |
| + t.Fatalf("access = %q", out) |
| + } |
| + } |
| +} |
| + |
| +func TestDevboxSocketRefusedOnDarwin(t *testing.T) { |
| + for _, args := range [][]string{ |
| + {"share", "--socket", "/missing", "shop"}, |
| + {"share", "--socket", "", "--port", "invalid", "--allow", "bad login", "Shop", "extra"}, |
| + } { |
| + mod := func(c *devboxCLI) { |
| + c.goos = "darwin" |
| + c.serviceUID = func() (uint32, error) { t.Fatal("looked up service user"); return 0, nil } |
| + c.grantACL = func(string, uint32) error { t.Fatal("wrote ACL"); return nil } |
| + } |
| + out, errOut, code := runDevboxCLI(t, "/missing", mod, args...) |
| + want := "--socket works on Linux devboxes only for now. on this devbox, use --port\n" |
| + if code != 1 || out != "" || errOut != want { |
| + t.Fatalf("share = %d, %q, %q", code, out, errOut) |
| + } |
| + } |
| +} |
| + |
| +func TestDevboxAllowRefusedOnDarwin(t *testing.T) { |
| + for _, args := range [][]string{ |
| + {"share", "--port", "8080", "--allow", "alice@github", "shop"}, |
| + {"share", "--dir", "/missing", "--allow", "", "shop"}, |
| + {"share", "--allow", "bad login", "Shop", "extra"}, |
| + } { |
| + out, errOut, code := runDevboxCLI(t, "/missing", func(c *devboxCLI) { c.goos = "darwin" }, args...) |
| + want := "--allow works on Linux devboxes only for now. on this devbox, every link is open to the tailnet\n" |
| + if code != 1 || out != "" || errOut != want { |
| + t.Fatalf("share = %d, %q, %q", code, out, errOut) |
| + } |
| + } |
| +} |
| + |
| +func TestDevboxSocketLocalRefusals(t *testing.T) { |
| + h := newDevboxHarness(t, nil) |
| + socket := devboxSocketTarget(t, h) |
| + dir := filepath.Dir(socket) |
| + plain := filepath.Join(dir, "plain") |
| + if err := os.WriteFile(plain, []byte("hi"), 0600); err != nil { |
| + t.Fatal(err) |
| + } |
| + symlink := filepath.Join(dir, "link") |
| + if err := os.Symlink(socket, symlink); err != nil { |
| + t.Fatal(err) |
| + } |
| + cases := []struct { |
| + name, path, want string |
| + other bool |
| + }{ |
| + {"outside", filepath.Join(filepath.Dir(dir), "other.sock"), "path", false}, |
| + {"nested", filepath.Join(dir, "nested", "tool.sock"), "path", false}, |
| + {"relative", "tool.sock", "path", false}, |
| + {"directory", dir, "path", false}, |
| + {"plain", plain, "socket", false}, |
| + {"symlink", symlink, "socket", false}, |
| + {"missing", filepath.Join(dir, "missing"), "socket", false}, |
| + {"owner", socket, "owner", true}, |
| + } |
| + for _, tt := range cases { |
| + t.Run(tt.name, func(t *testing.T) { |
| + mod := func(c *devboxCLI) { |
| + if tt.other { |
| + c.uid++ |
| + } |
| + c.serviceUID = func() (uint32, error) { t.Fatal("looked up service user before local refusal"); return 0, nil } |
| + c.grantACL = func(string, uint32) error { t.Fatal("wrote ACL before local refusal"); return nil } |
| + } |
| + want := tt.path + " is not directly inside " + dir + ". have your tool create its socket there, then share it" |
| + if tt.want == "socket" { |
| + want = tt.path + " is not a socket. give the path of the Unix socket your tool listens on" |
| + } |
| + if tt.want == "owner" { |
| + want = tt.path + " belongs to another account. you can only share a socket you created" |
| + } |
| + out, errOut, code := runDevboxCLI(t, h.sock, mod, "share", "--socket", tt.path, "shop") |
| + if code != 1 || out != "" || errOut != want+"\n" { |
| + t.Fatalf("share = %d, %q, %q; want %q", code, out, errOut, want) |
| + } |
| + }) |
| + } |
| + if len(h.r.Links.All()) != 0 { |
| + t.Fatal("refused socket registered") |
| + } |
| +} |
| + |
| +func TestDevboxSocketSetupRefusals(t *testing.T) { |
| + h := newDevboxHarness(t, nil) |
| + path := devboxSocketTarget(t, h) |
| + t.Run("missing user", func(t *testing.T) { |
| + t.Setenv("CARLOS_DEVBOX_SERVICE_USER", "devbox-no-such-user-486") |
| + mod := func(c *devboxCLI) { |
| + c.grantACL = func(string, uint32) error { t.Fatal("ACL written without user"); return nil } |
| + } |
| + out, errOut, code := runDevboxCLI(t, h.sock, mod, "share", "--socket", path, "shop") |
| + want := "this machine has no devbox service user called devbox-no-such-user-486. tell whoever runs this devbox\n" |
| + if code != 1 || out != "" || errOut != want { |
| + t.Fatalf("share = %d, %q, %q", code, out, errOut) |
| + } |
| + }) |
| + t.Run("acl", func(t *testing.T) { |
| + mod := func(c *devboxCLI) { |
| + c.grantACL = func(got string, uid uint32) error { |
| + if got != path || uid != me() { |
| + t.Fatalf("grant = %q, %d", got, uid) |
| + } |
| + return os.ErrPermission |
| + } |
| + } |
| + out, errOut, code := runDevboxCLI(t, h.sock, mod, "share", "--socket", path, "shop") |
| + want := "could not let the devbox service open " + path + ". check that the socket is yours, or tell whoever runs this devbox\n" |
| + if code != 1 || out != "" || errOut != want { |
| + t.Fatalf("share = %d, %q, %q", code, out, errOut) |
| + } |
| + }) |
| + if len(h.r.Links.All()) != 0 { |
| + t.Fatal("refused socket registered") |
| + } |
| +} |
| + |
| +func TestDevboxSocketUnreachableCLI(t *testing.T) { |
| + h := newDevboxHarness(t, nil) |
| + dir := t.TempDir() |
| + h.r.SocketsDir = dir |
| + t.Setenv("CARLOS_DEVBOX_SOCKETS_DIR", dir) |
| + path := filepath.Join(dir, "gone.sock") |
| + ln, err := net.ListenUnix("unix", &net.UnixAddr{Name: path, Net: "unix"}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + ln.SetUnlinkOnClose(false) |
| + ln.Close() |
| + mod := func(c *devboxCLI) { c.serviceUID = func() (uint32, error) { return me(), nil } } |
| + out, errOut, code := runDevboxCLI(t, h.sock, mod, "share", "--socket", path, "shop") |
| + want := "nothing is answering on " + path + ". start your tool, then share again\n" |
| + if code != 1 || out != "" || errOut != want { |
| + t.Fatalf("share = %d, %q, %q", code, out, errOut) |
| + } |
| + if len(h.r.Links.All()) != 0 { |
| + t.Fatal("unreachable socket registered") |
| + } |
| +} |
| + |
| +func TestDevboxIdentityRefusalRendering(t *testing.T) { |
| + t.Setenv("CARLOS_DEVBOX_SOCKETS_DIR", "") |
| + path := "/run/carlos-devbox/sockets/shop.sock" |
| + rc := refusalContext{path: path, allow: "alice@github"} |
| + for _, tt := range []struct{ code, want string }{ |
| + {devbox.CodeSocketPath, path + " is not directly inside /run/carlos-devbox/sockets. have your tool create its socket there, then share it"}, |
| + {devbox.CodeNotASocket, path + " is not a socket. give the path of the Unix socket your tool listens on"}, |
| + {devbox.CodeSocketNotYours, path + " belongs to another account. you can only share a socket you created"}, |
| + {devbox.CodeSocketUnreachable, "nothing is answering on " + path + ". start your tool, then share again"}, |
| + {devbox.CodeSocketUnsupported, "--socket works on Linux devboxes only for now. on this devbox, use --port"}, |
| + {devbox.CodeBadAllow, "alice@github is not a tailnet login. give logins such as alice@github, separated by commas, with no spaces"}, |
| + } { |
| + t.Run(tt.code, func(t *testing.T) { |
| + if err := rc.render(devbox.WireError{Code: tt.code}); err == nil || err.Error() != tt.want { |
| + t.Fatalf("render = %v, want %q", err, tt.want) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestDevboxShareOneTarget(t *testing.T) { |
| + for _, flags := range [][]string{ |
| + nil, {"--port", "8080", "--socket", "/missing"}, {"--dir", ".", "--socket", "/missing"}, |
| + {"--port", "8080", "--dir", ".", "--socket", "/missing"}, |
| + } { |
| + out, errOut, code := runDevboxCLI(t, "/missing", nil, append([]string{"share", "shop"}, flags...)...) |
| + if code != 1 || out != "" || errOut != "give one of --port for a dev server, --socket for a socket or --dir for a folder\n" { |
| + t.Fatalf("share = %d, %q, %q", code, out, errOut) |
| + } |
| + } |
| +} |
| + |
| +func TestDevboxShareAllowCap(t *testing.T) { |
| + raw := strings.Repeat("alice@github,", devbox.MaxAllowEntries) + "overflow@github" |
| + out, errOut, code := runDevboxCLI(t, "/missing", nil, "share", "shop", "--port", "8080", "--allow", raw) |
| + if code != 1 || out != "" || errOut != "overflow@github is not a tailnet login. give logins such as alice@github, separated by commas, with no spaces\n" { |
| + t.Fatalf("share = %d, %q, %q", code, out, errOut) |
| + } |
| +} |
| + |
| +func TestDevboxIdentityHelp(t *testing.T) { |
| + t.Setenv("CARLOS_DEVBOX_SOCKETS_DIR", "/example/sockets") |
| + for _, tt := range []struct { |
| + verb string |
| + want []string |
| + }{ |
| + {"share", []string{ |
| + "usage: carlos devbox share [--port <port>, --socket <path> or --dir <path>] [--allow <logins>] [<name>]", |
| + "a Unix socket your tool listens on, inside /example/sockets. share makes it private to you and the devbox service. if your tool restarts and makes a new socket, share again", |
| + "tailnet logins that may open the link, separated by commas, such as alice@github. anyone else sees a page saying the link is private. without --allow, everyone on the tailnet can open it. sharing again replaces the list", |
| + }}, |
| + {"list", []string{"usage: carlos devbox list [--allowed]", "also show who can open your private links"}}, |
| + } { |
| + _, errOut, code := runDevboxCLI(t, "/missing", nil, tt.verb, "--help") |
| + if code != 0 { |
| + t.Fatalf("help = %d, %q", code, errOut) |
| + } |
| + for _, want := range tt.want { |
| + if !strings.Contains(errOut, want) { |
| + t.Errorf("%s help missing %q: %q", tt.verb, want, errOut) |
| + } |
| + } |
| + } |
| +} |
diff --git a/infra/devbox/README.md b/infra/devbox/README.md| index a8ffe7bd2..8e991eba4 100644 |
| --- a/infra/devbox/README.md |
| +++ b/infra/devbox/README.md |
| @@ -151,6 +151,22 @@ Add one A record, by hand, once: *.devbox.example.com pointing at this machine's |
| *.devbox.example.com. 300 IN A 100.64.0.10 |
| ``` |
| +## Visitor identity and private links |
| + |
| +The service tells each link's upstream who is visiting, in the Carlos-Visitor-Login, Carlos-Visitor-Name and Carlos-Visitor-Node headers. It removes any a visitor sends, so a tool behind a link can trust them. |
| + |
| +Tailscale names a device's owner, not the person using it. So a visit from this machine, or from any node listed in CARLOS_DEVBOX_SHARED_NODES, carries no identity, and a link shared with --allow refuses it. |
| + |
| +Add every other shared machine, such as another devbox, to CARLOS_DEVBOX_SHARED_NODES in the environment file, as machine names separated by commas. |
| + |
| +```sh |
| +CARLOS_DEVBOX_SHARED_NODES=devbox-2 |
| +``` |
| + |
| +The service user asks tailscaled who each visitor is. If it cannot, every visitor is anonymous, and links shared with --allow refuse everyone. |
| + |
| +The service creates /run/carlos-devbox/sockets when it starts. Any account can create a socket there, but nobody can list the folder, and only a socket's owner can remove it. |
| + |
| ## Check that it works |
| - [ ] As an ordinary user, then again as an agent account, run a dev server on 127.0.0.1, publish it, open the link from another device on the tailnet, and see an edit appear through live reload. |
| @@ -183,6 +199,20 @@ Add one A record, by hand, once: *.devbox.example.com pointing at this machine's |
| ss -ltnp | grep carlos |
| ``` |
| +- [ ] Share a socket link and open it from another device. Check that another account on this machine cannot connect to the socket. |
| + |
| + ```sh |
| + carlos devbox share --socket /run/carlos-devbox/sockets/shop.sock shop |
| + curl --unix-socket /run/carlos-devbox/sockets/shop.sock http://localhost/ |
| + ``` |
| + |
| +- [ ] Share a link with --allow and your own login. Check that it opens from your own device and shows the private page from a browser on this machine. |
| + |
| + ```sh |
| + carlos devbox share --socket /run/carlos-devbox/sockets/shop.sock --allow alice@github shop |
| + carlos devbox list --allowed |
| + ``` |
| + |
| A dev server that checks the Host header needs the devbox domain added once per project. In Vite that is server.allowedHosts, and in Rails it is config.hosts. |
| ```js |
diff --git a/internal/devbox/registrar.go b/internal/devbox/registrar.go| index b6d081cfe..dfcbe1d6e 100644 |
| --- a/internal/devbox/registrar.go |
| +++ b/internal/devbox/registrar.go |
| @@ -881,20 +881,28 @@ func answering(ctx context.Context, port int) bool { |
| return true |
| } |
| -func validateAllow(entries []string) ([]string, *refusal) { |
| +// AllowError identifies the entry refused by ValidateAllow. |
| +type AllowError struct{ Login string } |
| + |
| +func (e *AllowError) Error() string { return "devbox: invalid allow entry" } |
| + |
| +// ValidateAllow normalizes and deduplicates logins for both the CLI and |
| +// registrar. It trims entries, then requires nonempty printable ASCII within |
| +// the caps. |
| +func ValidateAllow(entries []string) ([]string, error) { |
| if len(entries) > MaxAllowEntries { |
| - return nil, refuse(http.StatusBadRequest, CodeBadAllow) |
| + return nil, &AllowError{Login: entries[MaxAllowEntries]} |
| } |
| var allow []string |
| seen := make(map[string]bool, len(entries)) |
| for _, entry := range entries { |
| entry = strings.TrimSpace(entry) |
| if len(entry) == 0 || len(entry) > MaxAllowLogin { |
| - return nil, refuse(http.StatusBadRequest, CodeBadAllow) |
| + return nil, &AllowError{Login: entry} |
| } |
| for i := range entry { |
| if entry[i] < '!' || entry[i] > '~' { |
| - return nil, refuse(http.StatusBadRequest, CodeBadAllow) |
| + return nil, &AllowError{Login: entry} |
| } |
| } |
| entry = strings.ToLower(entry) |
| @@ -906,6 +914,14 @@ func validateAllow(entries []string) ([]string, *refusal) { |
| return allow, nil |
| } |
| +func validateAllow(entries []string) ([]string, *refusal) { |
| + allow, err := ValidateAllow(entries) |
| + if err != nil { |
| + return nil, refuse(http.StatusBadRequest, CodeBadAllow) |
| + } |
| + return allow, nil |
| +} |
| + |
| // AllowFor returns a copy of the allow list for a host under Domain. |
| // The copy comes from Links.Get through linkForHost. |
| func (r *Registrar) AllowFor(host string) []string { |
diff --git a/internal/edge/devboxfront.go b/internal/edge/devboxfront.go| index db3345e5b..1bef459cf 100644 |
| --- a/internal/edge/devboxfront.go |
| +++ b/internal/edge/devboxfront.go |
| @@ -14,9 +14,9 @@ import ( |
| "time" |
| ) |
| -// These are http.StatusText(http.StatusForbidden) until DBX-C107/C108 are approved. |
| -const devboxPrivateTitle = "Forbidden" |
| -const devboxPrivateSentence = "Forbidden" |
| +// Approved private-page copy (#486 A, DBX-C107/C108). |
| +const devboxPrivateTitle = "This link is private" |
| +const devboxPrivateSentence = "Only the people it was shared with can open it, from their own devices on the tailnet." |
| const devboxIdentityTimeout = 2 * time.Second |
diff --git a/internal/edge/devboxfront_test.go b/internal/edge/devboxfront_test.go| index 4ee7bd882..349da722a 100644 |
| --- a/internal/edge/devboxfront_test.go |
| +++ b/internal/edge/devboxfront_test.go |
| @@ -182,7 +182,7 @@ func TestFrontAllowRefusesOthers(t *testing.T) { |
| } |
| } |
| body := w.Body.String() |
| - for _, want := range []string{"<title>" + devboxPrivateTitle + "</title>", "<h1>" + devboxPrivateTitle + "</h1>", "<p>" + devboxPrivateSentence + "</p>"} { |
| + for _, want := range []string{"<title>This link is private</title>", "<h1>This link is private</h1>", "<p>Only the people it was shared with can open it, from their own devices on the tailnet.</p>"} { |
| if !strings.Contains(body, want) { |
| t.Errorf("body %q lacks %q", body, want) |
| } |