5943a20e2b
* feat(sidecar): add sidecar proxy for sandbox credential isolation
Keep real secrets (app_secret, access_token) out of sandbox environments.
CLI instances inside sandboxes connect to a trusted sidecar process via
HTTP; the sidecar verifies HMAC-signed requests and injects real tokens
before forwarding to the Lark API.
Key components:
- `auth proxy` subcommand to start the sidecar server (build tag: authsidecar)
- Noop credential provider returns sentinel tokens in sidecar mode
- Transport interceptor rewrites requests to sidecar with HMAC signature
- Env provider yields to sidecar provider when AUTH_PROXY is set
- Supports both feishu and lark brand endpoints
* feat(sidecar): implement priority ordering for credential providers
* feat(sidecar): strip client-supplied auth headers and improve shutdown logging
* feat(sidecar): buffer request body to prevent HMAC mismatches on read errors
* feat(sidecar): fix CI
* refactor(sidecar): publish protocol package and move server to reference demo
The sidecar server is no longer shipped as a `lark-cli auth proxy`
subcommand. Instead, the CLI provides only the standard sidecar *client*
(via `-tags authsidecar`), while the wire-protocol utilities are exposed
as a public package for integrators to implement their own server.
Changes:
- Move `internal/sidecar/` → `sidecar/` so external integrators can
import HMAC signing, headers, sentinels and address validators.
- Remove `cmd/auth/proxy.go`, `proxy_stub.go`, `proxy_test.go` and the
conditional registration in `cmd/auth/auth.go`.
- Add `sidecar/server-demo/` — a reference server implementation behind
the `authsidecar_demo` build tag. It reuses the lark-cli credential
pipeline for local development; production integrators are expected
to replace the credential layer with their own secrets source.
- Update all internal imports from `internal/sidecar` to `sidecar`.
Rationale:
- Each integrator has different secrets management / HA / multi-tenant
requirements, so a one-size-fits-all server doesn't belong in the
shipped CLI.
- Keeping the client in-tree guarantees all sandbox-side code stays
protocol-compatible without a second repo to sync.
- The public `sidecar/` package pins the wire protocol as a stable
contract third-party servers must conform to.
Build matrix after this change:
- `go build` → standard CLI, no sidecar code
- `go build -tags authsidecar` → CLI + sidecar client
- `go build -tags authsidecar_demo \
./sidecar/server-demo/` → reference server binary
No production users are affected today because the server was not yet
released; existing sidecar-client users are unchanged.
* feat(sidecar): close 5 pre-release security gaps
- Server: enforce https-only target (no path/query/userinfo), pin
forwardURL to https:// — blocks cleartext token leak
- Protocol v1: canonical now covers version/identity/auth-header,
blocks identity-flip replay within drift window
- Client: ValidateProxyAddr requires loopback or same-host alias,
rejects userinfo and https (interceptor is http-only); cross-machine
is out of scope
- Build: non-authsidecar builds exit(2) when AUTH_PROXY is set,
preventing silent fallback to env credentials
- Demo: whitelist auth-header to Authorization / X-Lark-MCP-{UAT,TAT},
blocks token injection into Cookie / UA / X-Forwarded-For exfil paths
168 lines
5.3 KiB
Go
168 lines
5.3 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
//go:build authsidecar_demo
|
|
|
|
// Command sidecar-server-demo is a reference implementation of a sidecar
|
|
// auth proxy server. It is NOT production-ready — integrators should
|
|
// implement their own server conforming to the wire protocol defined in
|
|
// github.com/larksuite/cli/sidecar.
|
|
//
|
|
// The demo reuses the lark-cli credential pipeline (keychain + config) to
|
|
// resolve real tokens, so it only works on a machine that has been
|
|
// configured with `lark-cli auth login`.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/larksuite/cli/internal/cmdutil"
|
|
"github.com/larksuite/cli/internal/core"
|
|
"github.com/larksuite/cli/internal/envvars"
|
|
"github.com/larksuite/cli/internal/vfs"
|
|
"github.com/larksuite/cli/sidecar"
|
|
)
|
|
|
|
func main() {
|
|
listen := flag.String("listen", sidecar.DefaultListenAddr, "listen address (host:port)")
|
|
keyFile := flag.String("key-file", defaultKeyFile(), "path to write the HMAC key")
|
|
logFile := flag.String("log-file", "", "audit log file (stderr if empty)")
|
|
profile := flag.String("profile", "", "lark-cli profile name (empty = active profile)")
|
|
flag.Parse()
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
|
defer cancel()
|
|
|
|
if err := run(ctx, *listen, *keyFile, *logFile, *profile); err != nil {
|
|
fmt.Fprintln(os.Stderr, "error:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func defaultKeyFile() string {
|
|
if home, err := os.UserHomeDir(); err == nil {
|
|
return filepath.Join(home, ".lark-sidecar", "proxy.key")
|
|
}
|
|
return "/tmp/lark-sidecar/proxy.key"
|
|
}
|
|
|
|
func run(ctx context.Context, listen, keyFile, logFile, profile string) error {
|
|
// Reject self-proxy: if this process inherited AUTH_PROXY, the sidecar
|
|
// credential provider would activate and return sentinel tokens instead
|
|
// of real ones, breaking the "trusted side holds real credentials" premise.
|
|
if v := os.Getenv(envvars.CliAuthProxy); v != "" {
|
|
return fmt.Errorf("%s is set in this environment (%s); unset it before starting the sidecar server", envvars.CliAuthProxy, v)
|
|
}
|
|
if listen == "" {
|
|
return fmt.Errorf("invalid --listen address: empty")
|
|
}
|
|
|
|
// Generate HMAC key (32 bytes = 256 bits) and write it to disk (0600).
|
|
keyBytes := make([]byte, 32)
|
|
if _, err := rand.Read(keyBytes); err != nil {
|
|
return fmt.Errorf("failed to generate HMAC key: %v", err)
|
|
}
|
|
keyHex := hex.EncodeToString(keyBytes)
|
|
|
|
keyDir := filepath.Dir(keyFile)
|
|
if err := vfs.MkdirAll(keyDir, 0700); err != nil {
|
|
return fmt.Errorf("failed to create key directory: %v", err)
|
|
}
|
|
if err := vfs.WriteFile(keyFile, []byte(keyHex), 0600); err != nil {
|
|
return fmt.Errorf("failed to write key file: %v", err)
|
|
}
|
|
|
|
// Audit logger: file or stderr.
|
|
var auditLogger *log.Logger
|
|
if logFile != "" {
|
|
f, err := vfs.OpenFile(logFile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open log file: %v", err)
|
|
}
|
|
defer f.Close()
|
|
auditLogger = log.New(f, "", log.LstdFlags)
|
|
} else {
|
|
auditLogger = log.New(os.Stderr, "[audit] ", log.LstdFlags)
|
|
}
|
|
|
|
// Reuse the lark-cli credential pipeline. A production implementation
|
|
// would likely source credentials from a secrets manager instead.
|
|
factory := cmdutil.NewDefault(cmdutil.InvocationContext{Profile: profile})
|
|
cfg, err := factory.Config()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to load config: %v", err)
|
|
}
|
|
|
|
listener, err := net.Listen("tcp", listen)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to listen on %s: %v", listen, err)
|
|
}
|
|
defer listener.Close()
|
|
|
|
allowedHosts := buildAllowedHosts(
|
|
core.ResolveEndpoints(core.BrandFeishu),
|
|
core.ResolveEndpoints(core.BrandLark),
|
|
)
|
|
allowedIDs := buildAllowedIdentities(cfg)
|
|
|
|
handler := &proxyHandler{
|
|
key: []byte(keyHex),
|
|
cred: factory.Credential,
|
|
appID: cfg.AppID,
|
|
brand: cfg.Brand,
|
|
logger: auditLogger,
|
|
forwardCl: newForwardClient(),
|
|
allowedHosts: allowedHosts,
|
|
allowedIDs: allowedIDs,
|
|
}
|
|
|
|
server := &http.Server{
|
|
Handler: handler,
|
|
ReadHeaderTimeout: 10 * time.Second,
|
|
ReadTimeout: 60 * time.Second,
|
|
IdleTimeout: 120 * time.Second,
|
|
MaxHeaderBytes: 1 << 20,
|
|
}
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
auditLogger.Println("shutting down...")
|
|
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
if err := server.Shutdown(shutdownCtx); err != nil {
|
|
auditLogger.Printf("shutdown error: %v", err)
|
|
}
|
|
}()
|
|
|
|
keyPrefix := keyHex
|
|
if len(keyPrefix) > 8 {
|
|
keyPrefix = keyPrefix[:8]
|
|
}
|
|
proxyURL := "http://" + listen
|
|
fmt.Fprintf(os.Stderr, "Auth sidecar listening on %s\n", proxyURL)
|
|
fmt.Fprintf(os.Stderr, "HMAC key prefix: %s\n", keyPrefix)
|
|
fmt.Fprintf(os.Stderr, "Full key written to %s (mode 0600)\n", keyFile)
|
|
fmt.Fprintf(os.Stderr, "\nSet in sandbox:\n")
|
|
fmt.Fprintf(os.Stderr, " export %s=%q\n", envvars.CliAuthProxy, proxyURL)
|
|
fmt.Fprintf(os.Stderr, " export %s=\"<read from %s>\"\n", envvars.CliProxyKey, keyFile)
|
|
fmt.Fprintf(os.Stderr, " export %s=%q\n", envvars.CliAppID, cfg.AppID)
|
|
fmt.Fprintf(os.Stderr, " export %s=%q\n", envvars.CliBrand, string(cfg.Brand))
|
|
|
|
if err := server.Serve(listener); err != nil && err != http.ErrServerClosed {
|
|
return fmt.Errorf("sidecar server exited unexpectedly: %v", err)
|
|
}
|
|
return nil
|
|
}
|