6 Commits

Author SHA1 Message Date
4125a4c102 fix(security): keep credentials off argv and plaintext transports
- M7: ResolveToken accepts the token via the HF_TOKEN env var (so it need
  not appear in argv, where it's visible in ps/shell history); the HTTP
  client refuses to send a token / API key over plaintext http:// to a
  non-loopback host (use https://). Loopback http is still allowed for
  local dev.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-31 20:16:36 +01:00
h z
c0ab087436 Merge pull request 'fix(cli): assign-schedule-type dispatch at top level' (#8) from fix/assign-schedule-type-dispatch into main 2026-05-29 07:47:28 +00:00
3edabb72ba fix(cli): assign-schedule-type dispatch at top level
`assign-schedule-type` is registered in CommandSurface() as a Group, so
the default branch of main()'s switch picks it up via findGroup() and
hands it to handleGroup. handleGroup then treats args[0] (the agent-id)
as a subcommand name, fails findSubCommand, and errors:

    unknown assign-schedule-type subcommand: <agent-id>

The group has no subcommands — it's a leaf — so the call never reaches
handleAssignScheduleType. Add an explicit top-level case before the
default branch so the leaf bypasses the group dispatcher.

Pre-fix repro:
    $ AGENT_ID=ard hf assign-schedule-type analyst1 standard
    unknown assign-schedule-type subcommand: analyst1

Post-fix:
    $ AGENT_ID=ard CLAW_IDENTIFIER=server-t2 hf assign-schedule-type analyst1 standard
    Assigned schedule type 'standard' to agent 'analyst1'

Surfaced during recruitment workflow Step 5 on prod (sherlock/agent-resource-director).
2026-05-29 08:46:28 +01:00
h z
1c9e90b033 Merge pull request #7 2026-05-26 11:48:21 +00:00
2176383729 fix(cli): send api-keys via X-API-Key in client.New + help surface
passmgr.GetToken returns an api-key in padded-cell mode (provisioned by
scripts/provision-hf-accounts.sh via 'hf user reset-apikey'), but every
call site funneled that through client.New which sent it as a
'Authorization: Bearer <hex>'. The HF backend's HTTPBearer middleware
expects JWT shape there and rejects hex strings as 'Could not validate
credentials'. The d2b83ad backend fix added a Bearer-fallback that tries
the value as an api-key, which masked the issue against current prod;
older backends or any future change in that fallback still 401.

Two changes:
- client.New auto-detects shape: 'eyJ'-prefix + two dots == JWT (Bearer),
  anything else == api-key (X-API-Key). Empty token sets neither header.
- internal/help/surface.go's loadPermissionState (called by hf --help
  introspection) switches to client.NewWithAPIKey explicitly so the
  command-discovery path doesn't depend on the heuristic at all. When
  that path failed silently (Known:false), agents would see only the
  always-permitted commands ('user.*', 'agent.status', 'config',
  'health', 'version') and conclude they had no project permission.

Adds internal/client/client_test.go covering both header paths plus
empty-token, isLikelyJWT cases, and NewWithAPIKey precedence.

Verified end-to-end in sim against a rebuilt hf-backend matching prod
(commit d2b83ad): cli with --token <api-key> sends X-Api-Key header,
backend returns 200 on /projects + /auth/me/permissions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 12:43:04 +01:00
a42ba6f880 fix(cli): gate hf project create on project.create (was project.write)
surface.go declared project/create as Permitted: has(perms, "project.write"),
but the backend now (and the user-facing role editor's intent) uses
`project.create` as the dedicated create gate. Switching CLI and backend
to agree on the same perm so a role granted just `project.create` (e.g.
mgr in the new seed) can run `hf project create` without needing the
broader project.write.

Companion change to HarborForge.Backend@HEAD which adds project.create to
DEFAULT_PERMISSIONS, gives it to mgr by default, and rewrites the
POST /projects gate to consult it.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-24 22:09:41 +01:00
5 changed files with 183 additions and 8 deletions

View File

@@ -68,6 +68,11 @@ func main() {
default:
output.Errorf("unknown agent subcommand: %s", args[1])
}
case "assign-schedule-type":
// Leaf command (no subcommands) — args are <agent-id> <type-name>.
// Must dispatch at top level because handleGroup treats args[0] as
// a subcommand name and would error "unknown ... subcommand: <agent-id>".
handleAssignScheduleType(args[1:])
default:
if group, ok := findGroup(args[0]); ok {
handleGroup(group, args[1:])

View File

@@ -5,7 +5,9 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
neturl "net/url"
"strings"
"time"
)
@@ -19,14 +21,34 @@ type Client struct {
}
// New creates a Client with the given base URL and optional auth token.
//
// The token is sent as Authorization: Bearer when it looks like a JWT
// (eyJ-prefixed, three dot-separated segments). Anything else is treated as
// an API key and sent via X-API-Key. This lets call sites that historically
// passed an api-key as a "token" (e.g. the value returned by passmgr.GetToken
// in padded-cell mode) authenticate correctly without per-callsite churn.
func New(baseURL, token string) *Client {
return &Client{
c := &Client{
BaseURL: strings.TrimRight(baseURL, "/"),
Token: token,
HTTPClient: &http.Client{
Timeout: 30 * time.Second,
},
}
if isLikelyJWT(token) {
c.Token = token
} else if token != "" {
c.APIKey = token
}
return c
}
// isLikelyJWT returns true for tokens that look like a JSON Web Token:
// "eyJ"-prefixed (base64-encoded JSON header opening with `{"`) and exactly
// two dots separating header.payload.signature. API keys minted by the HF
// backend are hex (`/users/{id}/apikey` returns a 64-hex-char `key`); fabric
// keys use a `fak_` prefix. None of those match this shape.
func isLikelyJWT(token string) bool {
return strings.HasPrefix(token, "eyJ") && strings.Count(token, ".") == 2
}
// NewWithAPIKey creates a Client that authenticates using X-API-Key.
@@ -50,8 +72,39 @@ func (e *RequestError) Error() string {
return fmt.Sprintf("HTTP %d: %s", e.StatusCode, e.Body)
}
// guardPlaintextCreds refuses to transmit a token / API key over a plaintext
// http:// connection to a non-loopback host (prevents credential interception).
func (c *Client) guardPlaintextCreds() error {
if c.Token == "" && c.APIKey == "" {
return nil
}
u, err := neturl.Parse(c.BaseURL)
if err != nil || u.Scheme != "http" {
return nil // parse errors and https:// are fine
}
if isLoopbackHost(u.Hostname()) {
return nil
}
return fmt.Errorf("refusing to send credentials over plaintext http:// to non-loopback host %q — use an https:// base URL (hf config set-url ...)", u.Hostname())
}
// isLoopbackHost reports whether h is a loopback address or localhost name.
func isLoopbackHost(h string) bool {
h = strings.ToLower(h)
if h == "localhost" || strings.HasSuffix(h, ".localhost") {
return true
}
if ip := net.ParseIP(h); ip != nil {
return ip.IsLoopback()
}
return false
}
// Do executes an HTTP request and returns the response body bytes.
func (c *Client) Do(method, path string, body io.Reader) ([]byte, error) {
if err := c.guardPlaintextCreds(); err != nil {
return nil, err
}
url := c.BaseURL + path
req, err := http.NewRequest(method, url, body)
if err != nil {

View File

@@ -0,0 +1,104 @@
package client
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestIsLikelyJWT(t *testing.T) {
cases := []struct {
in string
want bool
why string
}{
// Real JWT minted by an HS256 signer (header `{"alg":"HS256","typ":"JWT"}`).
{"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIn0.signature", true, "valid JWT shape"},
// HF backend api-keys are 64-char hex from /users/{id}/apikey.
{"f654c3ff0bbc09e6a22294dfbbbff371a4550366849f59de68ddf064742831a0", false, "hex api-key"},
// Fabric api-keys carry a fak_ prefix.
{"fak_30791357ca11ac2ff963999bf265f6a5f240593eb01c06fc", false, "fabric api-key"},
// eyJ prefix without three segments isn't a JWT.
{"eyJabc", false, "prefix only"},
{"eyJabc.def", false, "two segments"},
// Empty / nonsense.
{"", false, "empty"},
{"....", false, "dots only"},
}
for _, c := range cases {
if got := isLikelyJWT(c.in); got != c.want {
t.Errorf("isLikelyJWT(%q) = %v, want %v (%s)", c.in, got, c.want, c.why)
}
}
}
func TestNewAutoSelectsAuthHeader(t *testing.T) {
// Capture which auth header reaches the server for each token shape.
var lastReq *http.Request
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lastReq = r
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, "{}")
}))
defer srv.Close()
// api-key path: should go via X-API-Key, NOT Authorization.
apiKey := "f654c3ff0bbc09e6a22294dfbbbff371a4550366849f59de68ddf064742831a0"
if _, err := New(srv.URL, apiKey).Get("/anything"); err != nil {
t.Fatalf("api-key call failed: %v", err)
}
if got := lastReq.Header.Get("X-API-Key"); got != apiKey {
t.Errorf("api-key not sent as X-API-Key (got %q)", got)
}
if got := lastReq.Header.Get("Authorization"); got != "" {
t.Errorf("api-key leaked into Authorization header (got %q)", got)
}
// JWT path: should go via Authorization: Bearer, NOT X-API-Key.
jwt := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIn0.signature"
if _, err := New(srv.URL, jwt).Get("/anything"); err != nil {
t.Fatalf("jwt call failed: %v", err)
}
if got := lastReq.Header.Get("Authorization"); !strings.HasPrefix(got, "Bearer ") || !strings.HasSuffix(got, jwt) {
t.Errorf("jwt not sent as Bearer (got %q)", got)
}
if got := lastReq.Header.Get("X-API-Key"); got != "" {
t.Errorf("jwt leaked into X-API-Key header (got %q)", got)
}
// Empty token: neither header set.
if _, err := New(srv.URL, "").Get("/anything"); err != nil {
t.Fatalf("empty-token call failed: %v", err)
}
if got := lastReq.Header.Get("Authorization"); got != "" {
t.Errorf("empty token set Authorization (got %q)", got)
}
if got := lastReq.Header.Get("X-API-Key"); got != "" {
t.Errorf("empty token set X-API-Key (got %q)", got)
}
}
func TestNewWithAPIKeyAlwaysUsesAPIKeyHeader(t *testing.T) {
// Even if someone passes a JWT-shaped string via NewWithAPIKey, it must
// still go via X-API-Key — the explicit constructor wins.
var lastReq *http.Request
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lastReq = r
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, "{}")
}))
defer srv.Close()
jwtShape := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxIn0.signature"
if _, err := NewWithAPIKey(srv.URL, jwtShape).Get("/anything"); err != nil {
t.Fatalf("call failed: %v", err)
}
if got := lastReq.Header.Get("X-API-Key"); got != jwtShape {
t.Errorf("NewWithAPIKey didn't use X-API-Key (got %q)", got)
}
if got := lastReq.Header.Get("Authorization"); got != "" {
t.Errorf("NewWithAPIKey set Authorization (got %q)", got)
}
}

View File

@@ -1,6 +1,9 @@
package commands
import (
"os"
"strings"
"git.hangman-lab.top/zhi/HarborForge.Cli/internal/mode"
"git.hangman-lab.top/zhi/HarborForge.Cli/internal/output"
"git.hangman-lab.top/zhi/HarborForge.Cli/internal/passmgr"
@@ -20,11 +23,16 @@ func ResolveToken(tokenFlag string) string {
}
return tok
}
// manual mode
if tokenFlag == "" {
output.Error("--token <token> required or execute this with pcexec")
// manual mode — prefer the explicit flag, else fall back to the HF_TOKEN
// env var so the token need not appear in argv (visible via `ps`/history).
if tokenFlag != "" {
return tokenFlag
}
return tokenFlag
if env := strings.TrimSpace(os.Getenv("HF_TOKEN")); env != "" {
return env
}
output.Error("--token <token> or HF_TOKEN env required, or execute this with pcexec")
return ""
}
// RejectTokenInPaddedCell checks if --token was passed in padded-cell mode

View File

@@ -76,7 +76,7 @@ func CommandSurface() []Group {
SubCommands: []Command{
{Name: "list", Description: "List projects", Permitted: has(perms, "project.read")},
{Name: "get", Description: "Show a project by code", Permitted: has(perms, "project.read")},
{Name: "create", Description: "Create a project", Permitted: has(perms, "project.write")},
{Name: "create", Description: "Create a project", Permitted: has(perms, "project.create")},
{Name: "update", Description: "Update a project", Permitted: has(perms, "project.write")},
{Name: "delete", Description: "Delete a project", Permitted: has(perms, "project.delete")},
{Name: "members", Description: "List project members", Permitted: has(perms, "project.read")},
@@ -224,7 +224,12 @@ func loadPermissionState(token string) permissionState {
return permissionState{Known: false, Permissions: map[string]struct{}{}}
}
c := client.New(cfg.BaseURL, token)
// passmgr.GetToken() returns an api-key in padded-cell mode (provisioned
// by scripts/provision-hf-accounts.sh via `hf user reset-apikey`), so go
// through the X-API-Key path explicitly. client.New also auto-detects this
// nowadays, but the explicit call keeps the introspection path independent
// of that heuristic.
c := client.NewWithAPIKey(cfg.BaseURL, token)
data, err := c.Get("/auth/me/permissions")
if err != nil {
return permissionState{Known: false, Permissions: map[string]struct{}{}}