Compare commits
5 Commits
a42ba6f880
...
fix/securi
| Author | SHA1 | Date | |
|---|---|---|---|
| 4125a4c102 | |||
| c0ab087436 | |||
| 3edabb72ba | |||
| 1c9e90b033 | |||
| 2176383729 |
@@ -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:])
|
||||
|
||||
@@ -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 {
|
||||
|
||||
104
internal/client/client_test.go
Normal file
104
internal/client/client_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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{}{}}
|
||||
|
||||
Reference in New Issue
Block a user