From 87dc7f09259455c6edbe7fb1ded1e171bfc01827 Mon Sep 17 00:00:00 2001 From: Topher Hindman Date: Fri, 30 Oct 2026 13:48:22 -0700 Subject: [PATCH 1/2] fix(lk): --dev under --user-auth is an error No user-auth path reads --dev. Commands that load a session project asked the Public API for a token for the user's project, and agent runs took their key from the agent's env files, so `lk --user-auth --dev room list` acted on a cloud project instead of the local dev server, without a word about --dev. --dev asks for the dev server's API key, which is API-key auth, so the combination is now an error, checked once in the root Before so every command gets it: "--dev can't be combined with --user-auth; remove --user-auth or drop --dev". It follows --dev with an API key or secret, which is also an error. --dev alone and --user-auth alone are unchanged. --- cmd/lk/main.go | 4 +++ .../testdata/script/dev-with-user-auth.txtar | 35 +++++++++++++++++++ cmd/lk/user_auth.go | 10 ++++++ cmd/lk/user_auth_test.go | 28 +++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 cmd/lk/testdata/script/dev-with-user-auth.txtar diff --git a/cmd/lk/main.go b/cmd/lk/main.go index 7fce7f6a1..1951c1980 100644 --- a/cmd/lk/main.go +++ b/cmd/lk/main.go @@ -226,6 +226,10 @@ func initLogger(ctx context.Context, cmd *cli.Command) (context.Context, error) } util.DetectBackground() + if err := devUnderUserAuthGate(cmd); err != nil { + return nil, err + } + showBanner(ctx, cmd) // Nudge users still on API-key auth toward the new account-based flow. diff --git a/cmd/lk/testdata/script/dev-with-user-auth.txtar b/cmd/lk/testdata/script/dev-with-user-auth.txtar new file mode 100644 index 000000000..aea8ad0a4 --- /dev/null +++ b/cmd/lk/testdata/script/dev-with-user-auth.txtar @@ -0,0 +1,35 @@ +# --dev under --user-auth is an error naming what to remove. lk never acts +# on the session's project instead of the dev server: the fake would answer +# that project's server, so lk can only fail by refusing. +respond POST http://one.test/twirp/livekit.RoomService/ListRooms 200 '' +respond POST http://dev.test/twirp/livekit.RoomService/ListRooms 200 '' +respond GET http://publicapi.test/v0/projects 200 '{"items":[{"id":"p_1","name":"one"}]}' + +! exec lk --user-auth --dev --url ws://dev.test room list +stderr '^--dev can''t be combined with --user-auth; remove --user-auth or drop --dev$' + +! exec lk --user-auth project list --dev +stderr '^--dev can''t be combined with --user-auth; remove --user-auth or drop --dev$' + +requests +! stdout . + +# --dev alone still uses the dev credentials, at --url, with a user signed in. +exec lk --dev --url ws://dev.test room list +stderr '^Using dev credentials$' +requests +cmp stdout want-requests + +-- home/.livekit/cli-config.yaml -- +default_user: u_1 +users: + - id: u_1 + email: dev@example.com + session_token: st_1 + api_url: http://publicapi.test + projects: + - project_id: p_1 + name: one + url: ws://one.test +-- want-requests -- +POST http://dev.test/twirp/livekit.RoomService/ListRooms apikey=devkey diff --git a/cmd/lk/user_auth.go b/cmd/lk/user_auth.go index 9015a3407..49c3d3f52 100644 --- a/cmd/lk/user_auth.go +++ b/cmd/lk/user_auth.go @@ -91,6 +91,16 @@ func userAuthGate(cmd *cli.Command) error { return nil } +// devUnderUserAuthGate refuses --dev under --user-auth. --dev asks for the +// local dev server's API key, which no user-auth path reads, so lk would +// otherwise act on the session's cloud project instead of the dev server. +func devUnderUserAuthGate(cmd *cli.Command) error { + if userAuthEnabled(cmd) && cmd.Bool("dev") { + return errors.New("--dev can't be combined with --user-auth; remove --user-auth or drop --dev") + } + return nil +} + // authModeFlags declares, for a dual-mode command, which flags are valid only in // one auth mode. It lets a command reject cross-mode misuse up front with a clear // message instead of silently ignoring a flag the active mode can't honor: diff --git a/cmd/lk/user_auth_test.go b/cmd/lk/user_auth_test.go index 9cbf6cb15..e24f62665 100644 --- a/cmd/lk/user_auth_test.go +++ b/cmd/lk/user_auth_test.go @@ -112,6 +112,34 @@ func TestUserAuthEnabled(t *testing.T) { assert.False(t, enabled(t, "--legacy-auth")) } +// TestDevUnderUserAuthGate checks that --dev under --user-auth, in either +// spelling, is an error, and that each flag alone is not. +func TestDevUnderUserAuthGate(t *testing.T) { + gate := func(t *testing.T, args ...string) error { + t.Helper() + var err error + cmd := &cli.Command{ + Name: "lk", + Flags: []cli.Flag{ + &cli.BoolFlag{Name: "user-auth", Aliases: []string{"experimental-auth"}}, + &cli.BoolFlag{Name: "dev"}, + }, + Action: func(_ context.Context, cmd *cli.Command) error { + err = devUnderUserAuthGate(cmd) + return nil + }, + } + require.NoError(t, cmd.Run(context.Background(), append([]string{"lk"}, args...))) + return err + } + + const want = "--dev can't be combined with --user-auth; remove --user-auth or drop --dev" + assert.EqualError(t, gate(t, "--user-auth", "--dev"), want) + assert.EqualError(t, gate(t, "--experimental-auth", "--dev"), want) + assert.NoError(t, gate(t, "--dev")) + assert.NoError(t, gate(t, "--user-auth")) +} + func TestRequireUserSessionAPIURL(t *testing.T) { const staging = "https://beta-api.staging.livekit.cloud" setHome(t, t.TempDir()) From 85814236eed18cc8ce7410b5a4ca5ed66197a472 Mon Sep 17 00:00:00 2001 From: Topher Hindman Date: Fri, 30 Oct 2026 13:48:24 -0700 Subject: [PATCH 2/2] test(lk): expect --dev under --user-auth to fail --dev under --user-auth is now an error for every command, so the property requires lk to fail without naming a project, as it does for --user-auth with --legacy-auth. --- cmd/lk/credential_property_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/lk/credential_property_test.go b/cmd/lk/credential_property_test.go index 0c6a347a1..32871e0d7 100644 --- a/cmd/lk/credential_property_test.go +++ b/cmd/lk/credential_property_test.go @@ -538,15 +538,15 @@ func requestProject(line string) (string, bool) { return strings.TrimSuffix(host, ".test"), true } -// mustFail is set when lk can't act: both mode flags at once, a Public API -// command outside --user-auth, --dev with an inline key or secret outside -// --user-auth, or, under --user-auth, no user who can act (no unexpired -// session, a --user with no unexpired session, or a Public API URL nobody is -// signed in to). Every drawn user is signed in to publicAPI. +// mustFail is set when lk can't act: both mode flags at once, --dev under +// --user-auth, a Public API command outside --user-auth, --dev with an inline +// key or secret outside --user-auth, or, under --user-auth, no user who can +// act (no unexpired session, a --user with no unexpired session, or a Public +// API URL nobody is signed in to). Every drawn user is signed in to publicAPI. func (c credentialCase) mustFail() bool { _, key, secret := c.inlineCredentials() switch { - case c.userAuth && c.legacyAuth: + case c.userAuth && (c.legacyAuth || c.dev): return true case !c.userAuth: return c.command.publicAPI || c.dev && (key != "" || secret != "")