5 Commits

Author SHA1 Message Date
f145979684 fix(worklog): always send logged_date (required datetime) on worklog add
The backend's WorkLogCreate.logged_date is a REQUIRED datetime, but the CLI
only sent it when --date was passed, and then as a bare YYYY-MM-DD that
failed datetime parsing → `hf worklog add` always 422'd (missing, then
invalid). Default logged_date to now (RFC3339); anchor a bare --date
<yyyy-mm-dd> to start-of-day so it parses as a datetime.

Verified on sim: `hf worklog add --task <code> --hours <n> --desc ...`
(no --date) now succeeds.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 10:15:10 +01:00
6dcf43ce41 feat(help): add knowledge-base leaf help (per-command usage)
The knowledge-base command family was registered in the help surface
(group + subcommands) but had NO leaf entries in internal/help/leaf.go, so
`hf knowledge-base add-fact --help` (and every other kb subcommand) printed
no usage. Weak agents then guessed flags (e.g. `add-fact --kb --topic-id
--title` instead of `--topic <id> --fact <text>`).

Add leaf help for all 18 kb subcommands (list/get/tree/topics/create/update/
delete/link/unlink/add-topic/update-topic/delete-topic/add-category/
update-category/delete-category/add-fact/update-fact/delete-fact), with the
authoritative usage strings copied from internal/commands/knowledge_base.go.
Now `hf knowledge-base <sub> --help` renders proper usage.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 08:52:40 +01:00
729365ca46 Merge fix/security-audit: CLI credential hardening 2026-06-01 09:23:52 +01:00
ef8d4dbdad Merge feat/knowledge-base: KnowledgeBase CLI commands 2026-06-01 09:23:52 +01:00
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
4 changed files with 148 additions and 78 deletions

View File

@@ -5,7 +5,9 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"net"
"net/http" "net/http"
neturl "net/url"
"strings" "strings"
"time" "time"
) )
@@ -70,8 +72,39 @@ func (e *RequestError) Error() string {
return fmt.Sprintf("HTTP %d: %s", e.StatusCode, e.Body) 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. // Do executes an HTTP request and returns the response body bytes.
func (c *Client) Do(method, path string, body io.Reader) ([]byte, error) { 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 url := c.BaseURL + path
req, err := http.NewRequest(method, url, body) req, err := http.NewRequest(method, url, body)
if err != nil { if err != nil {

View File

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

View File

@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"time"
"git.hangman-lab.top/zhi/HarborForge.Cli/internal/output" "git.hangman-lab.top/zhi/HarborForge.Cli/internal/output"
) )
@@ -35,9 +36,18 @@ func RunWorklogAdd(taskCode string, hours float64, desc, date, tokenFlag string)
if desc != "" { if desc != "" {
payload["description"] = desc payload["description"] = desc
} }
// logged_date is a REQUIRED datetime on the backend. Default to now; if
// the operator passed --date <yyyy-mm-dd>, anchor it to start-of-day so a
// bare date still parses as a datetime.
loggedDate := time.Now().UTC().Format(time.RFC3339)
if date != "" { if date != "" {
payload["logged_date"] = date if len(date) == 10 { // bare YYYY-MM-DD
loggedDate = date + "T00:00:00Z"
} else {
loggedDate = date
} }
}
payload["logged_date"] = loggedDate
body, err := json.Marshal(payload) body, err := json.Marshal(payload)
if err != nil { if err != nil {

View File

@@ -193,6 +193,25 @@ func leafHelpSpec(group, cmd string) (leafHelp, bool) {
"monitor/api-key": {Summary: "Manage monitor API keys", Usage: []string{"hf monitor api-key generate <identifier>", "hf monitor api-key revoke <identifier>"}, Flags: authFlagHelp()}, "monitor/api-key": {Summary: "Manage monitor API keys", Usage: []string{"hf monitor api-key generate <identifier>", "hf monitor api-key revoke <identifier>"}, Flags: authFlagHelp()},
"monitor/api-key/generate": {Summary: "Generate a monitor API key", Usage: []string{"hf monitor api-key generate <identifier>"}, Flags: authFlagHelp()}, "monitor/api-key/generate": {Summary: "Generate a monitor API key", Usage: []string{"hf monitor api-key generate <identifier>"}, Flags: authFlagHelp()},
"monitor/api-key/revoke": {Summary: "Revoke a monitor API key", Usage: []string{"hf monitor api-key revoke <identifier>"}, Flags: authFlagHelp()}, "monitor/api-key/revoke": {Summary: "Revoke a monitor API key", Usage: []string{"hf monitor api-key revoke <identifier>"}, Flags: authFlagHelp()},
"knowledge-base/list": {Summary: "List knowledge bases", Usage: []string{"hf knowledge-base list [--project <project-code>]"}, Flags: authFlagHelp()},
"knowledge-base/get": {Summary: "Show a knowledge base by code", Usage: []string{"hf knowledge-base get <kb-code>"}, Flags: authFlagHelp()},
"knowledge-base/tree": {Summary: "Show the full topic/category/fact tree", Usage: []string{"hf knowledge-base tree <kb-code>"}, Flags: authFlagHelp()},
"knowledge-base/topics": {Summary: "List topics in a knowledge base", Usage: []string{"hf knowledge-base topics <kb-code>"}, Flags: authFlagHelp()},
"knowledge-base/create": {Summary: "Create a knowledge base", Usage: []string{"hf knowledge-base create --title <title> [--desc <description>]"}, Flags: authFlagHelp()},
"knowledge-base/update": {Summary: "Update a knowledge base", Usage: []string{"hf knowledge-base update <kb-code> [--title <title>] [--desc <description>]"}, Flags: authFlagHelp()},
"knowledge-base/delete": {Summary: "Delete a knowledge base", Usage: []string{"hf knowledge-base delete <kb-code>"}, Flags: authFlagHelp()},
"knowledge-base/link": {Summary: "Link a knowledge base to a project", Usage: []string{"hf knowledge-base link <kb-code> --project <project-code>"}, Flags: authFlagHelp()},
"knowledge-base/unlink": {Summary: "Unlink a knowledge base from a project", Usage: []string{"hf knowledge-base unlink <kb-code> --project <project-code>"}, Flags: authFlagHelp()},
"knowledge-base/add-topic": {Summary: "Add a topic to a knowledge base", Usage: []string{"hf knowledge-base add-topic <kb-code> --topic <name> [--desc <description>]"}, Flags: authFlagHelp()},
"knowledge-base/update-topic": {Summary: "Update a topic", Usage: []string{"hf knowledge-base update-topic <topic-id> [--topic <name>] [--desc <description>]"}, Flags: authFlagHelp()},
"knowledge-base/delete-topic": {Summary: "Delete a topic", Usage: []string{"hf knowledge-base delete-topic <topic-id>"}, Flags: authFlagHelp()},
"knowledge-base/add-category": {Summary: "Add a category under a topic", Usage: []string{"hf knowledge-base add-category --topic <topic-id> --name <name> [--parent <category-id>] [--desc <description>]"}, Flags: authFlagHelp()},
"knowledge-base/update-category": {Summary: "Update a category", Usage: []string{"hf knowledge-base update-category <category-id> [--name <name>] [--parent <category-id>] [--desc <description>]"}, Flags: authFlagHelp()},
"knowledge-base/delete-category": {Summary: "Delete a category", Usage: []string{"hf knowledge-base delete-category <category-id>"}, Flags: authFlagHelp()},
"knowledge-base/add-fact": {Summary: "Add a fact under a topic (optionally a category)", Usage: []string{"hf knowledge-base add-fact --topic <topic-id> [--category <category-id>] --fact <text>"}, Flags: authFlagHelp()},
"knowledge-base/update-fact": {Summary: "Update a fact", Usage: []string{"hf knowledge-base update-fact <fact-id> [--fact <text>] [--category <category-id>]"}, Flags: authFlagHelp()},
"knowledge-base/delete-fact": {Summary: "Delete a fact", Usage: []string{"hf knowledge-base delete-fact <fact-id>"}, Flags: authFlagHelp()},
} }
if group == "" { if group == "" {