Compare commits
4 Commits
feat/knowl
...
feat/kb-le
| Author | SHA1 | Date | |
|---|---|---|---|
| 6dcf43ce41 | |||
| 729365ca46 | |||
| ef8d4dbdad | |||
| 4125a4c102 |
@@ -5,7 +5,9 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
neturl "net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@@ -70,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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
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
|
||||
|
||||
@@ -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/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()},
|
||||
|
||||
"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 == "" {
|
||||
|
||||
Reference in New Issue
Block a user