mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-11-24 09:41:51 +08:00
fix: make HTTP user agents configurable and remove tool identifiers
This commit is contained in:
1 parent
40746901db
commit
17c2fc5e08
30 files changed
+1204
-26
No files matched your search
@@ -0,0 +1,467 @@
|
||||
// Adapted from github.com/Autumn-27/norma/tool/webfetch.go at v0.4.3.
|
||||
// Keep the SDK tool metadata and fetch/render behavior; use ARTEX's explicit
|
||||
// HTTP identity for both the proxy attempt and its direct fallback. This local
|
||||
// adapter is needed until the SDK exposes request headers in actool.WebFetchConfig.
|
||||
package webfetch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Autumn-27/artex/internal/httpidentity"
|
||||
actool "github.com/Autumn-27/norma/tool"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
// New keeps the SDK's schema, permission checks and tool metadata.
|
||||
func New(cfg actool.WebFetchConfig) actool.CoreTool {
|
||||
base := actool.NewWebFetch(cfg)
|
||||
return actool.Build(actool.Spec{
|
||||
Name: base.Name(), Description: base.Description(), Prompt: base.Prompt(),
|
||||
Schema: base.InputSchema(), Permissions: base.CheckPermissions,
|
||||
ReadOnly: base.IsReadOnly, Concurrent: base.IsConcurrencySafe,
|
||||
Run: webFetchRunner(cfg),
|
||||
})
|
||||
}
|
||||
|
||||
const maxFetchBytes = 2 << 20 // 2 MiB
|
||||
|
||||
// newFetchClient returns an HTTP client honoring the proxy / TLS config.
|
||||
func newFetchClient(cfg actool.WebFetchConfig) *http.Client {
|
||||
tr := &http.Transport{}
|
||||
if cfg.Proxy != "" {
|
||||
if pu, err := url.Parse(cfg.Proxy); err == nil {
|
||||
tr.Proxy = http.ProxyURL(pu)
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case cfg.InsecureTLS:
|
||||
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
case cfg.CACert != "":
|
||||
// Trust the proxy's CA in addition to the system roots, so certs it
|
||||
// re-signs verify while every other connection stays fully checked.
|
||||
if pem, err := os.ReadFile(cfg.CACert); err == nil {
|
||||
pool, _ := x509.SystemCertPool()
|
||||
if pool == nil {
|
||||
pool = x509.NewCertPool()
|
||||
}
|
||||
if pool.AppendCertsFromPEM(pem) {
|
||||
tr.TLSClientConfig = &tls.Config{RootCAs: pool}
|
||||
}
|
||||
}
|
||||
}
|
||||
return &http.Client{Transport: tr}
|
||||
}
|
||||
|
||||
// proxyLikelyErr reports whether a fetch error is the kind a recording/MITM proxy
|
||||
// introduces — a closed tunnel (EOF / connection reset), a failed CONNECT, or a
|
||||
// TLS/HTTP2 mismatch — so a direct (no-proxy) retry may succeed. Name-resolution
|
||||
// failures and caller cancellation/timeout are target- or caller-side problems a
|
||||
// direct retry won't fix, so those return false (no retry).
|
||||
func proxyLikelyErr(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
s := strings.ToLower(err.Error())
|
||||
for _, n := range []string{"no such host", "context canceled", "context deadline", "timeout"} {
|
||||
if strings.Contains(s, n) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for _, p := range []string{
|
||||
"eof", "connection reset", "connection refused", "proxyconnect",
|
||||
"broken pipe", "forcibly closed", "malformed", "http2", "http/2",
|
||||
"tls:", "protocol error",
|
||||
} {
|
||||
if strings.Contains(s, p) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// staticAssetExts are file extensions WebFetch refuses: linked resources (scripts,
|
||||
// styles, source maps, images, fonts, media, wasm) that are not page content.
|
||||
var staticAssetExts = map[string]bool{
|
||||
".js": true, ".mjs": true, ".cjs": true, ".css": true, ".map": true,
|
||||
".png": true, ".jpg": true, ".jpeg": true, ".gif": true, ".svg": true,
|
||||
".webp": true, ".avif": true, ".ico": true, ".bmp": true,
|
||||
".woff": true, ".woff2": true, ".ttf": true, ".otf": true, ".eot": true,
|
||||
".wasm": true, ".mp4": true, ".webm": true, ".mp3": true, ".wav": true, ".ogg": true,
|
||||
}
|
||||
|
||||
// staticAssetExt returns the offending extension (with dot) if rawURL points at a
|
||||
// static asset, or "" otherwise. The query string and fragment are ignored, so
|
||||
// "/app.js?v=3" is still caught.
|
||||
func staticAssetExt(rawURL string) string {
|
||||
path := rawURL
|
||||
if u, err := url.Parse(rawURL); err == nil {
|
||||
path = u.Path
|
||||
}
|
||||
if i := strings.LastIndexByte(path, '/'); i >= 0 {
|
||||
path = path[i+1:] // last segment only
|
||||
}
|
||||
dot := strings.LastIndexByte(path, '.')
|
||||
if dot < 0 {
|
||||
return ""
|
||||
}
|
||||
if ext := strings.ToLower(path[dot:]); staticAssetExts[ext] {
|
||||
return ext
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func webFetchRunner(cfg actool.WebFetchConfig) func(context.Context, json.RawMessage, *actool.ToolContext) (actool.Result, error) {
|
||||
userAgent := httpidentity.UserAgent()
|
||||
client := newFetchClient(cfg)
|
||||
// direct is the no-proxy fallback used when a proxied fetch fails for a
|
||||
// proxy-caused reason (closed tunnel / failed CONNECT / TLS or HTTP2 mismatch):
|
||||
// it bypasses the configured proxy so a recording/MITM hiccup doesn't fail an
|
||||
// otherwise-reachable URL. nil when no proxy is configured (nothing to bypass).
|
||||
var direct *http.Client
|
||||
if cfg.Proxy != "" {
|
||||
direct = newFetchClient(actool.WebFetchConfig{InsecureTLS: cfg.InsecureTLS})
|
||||
}
|
||||
return func(ctx context.Context, input json.RawMessage, _ *actool.ToolContext) (actool.Result, error) {
|
||||
var in struct {
|
||||
URL string `json:"url"`
|
||||
Extract bool `json:"extract"`
|
||||
}
|
||||
if err := json.Unmarshal(input, &in); err != nil {
|
||||
return actool.Result{}, err
|
||||
}
|
||||
if !strings.HasPrefix(in.URL, "http://") && !strings.HasPrefix(in.URL, "https://") {
|
||||
return actool.Errorf("Error: url must start with http:// or https://"), nil
|
||||
}
|
||||
if ext := staticAssetExt(in.URL); ext != "" {
|
||||
return actool.Errorf("Error: WebFetch does not fetch static assets (" + ext + " file). It reads HTML pages, documentation, and API responses — not linked resources like scripts, styles, images, or fonts. Fetch the page that references this asset instead."), nil
|
||||
}
|
||||
cctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(cctx, http.MethodGet, in.URL, nil)
|
||||
if err != nil {
|
||||
return actool.Errorf("Error: " + err.Error()), nil
|
||||
}
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
resp, err := client.Do(req)
|
||||
// One direct retry when the proxied attempt failed for a proxy-caused reason.
|
||||
// A non-proxy error (bad host, caller timeout/cancel) is returned as-is — a
|
||||
// direct retry wouldn't fix it. The direct response is NOT captured by the proxy.
|
||||
if err != nil && direct != nil && proxyLikelyErr(err) {
|
||||
if r2, e2 := http.NewRequestWithContext(cctx, http.MethodGet, in.URL, nil); e2 == nil {
|
||||
r2.Header.Set("User-Agent", userAgent)
|
||||
if resp2, err2 := direct.Do(r2); err2 == nil {
|
||||
resp, err = resp2, nil
|
||||
} else {
|
||||
err = err2 // surface the direct failure (more representative of the target)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return actool.Errorf("Error fetching URL: " + err.Error()), nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, maxFetchBytes))
|
||||
|
||||
header := fmt.Sprintf("[%d %s] %s\n", resp.StatusCode, http.StatusText(resp.StatusCode), in.URL)
|
||||
// Cheap fingerprint signal that HTML→Markdown would otherwise hide.
|
||||
if sv := resp.Header.Get("Server"); sv != "" {
|
||||
header += "Server: " + sv + "\n"
|
||||
}
|
||||
if xp := resp.Header.Get("X-Powered-By"); xp != "" {
|
||||
header += "X-Powered-By: " + xp + "\n"
|
||||
}
|
||||
|
||||
var out string
|
||||
if strings.Contains(strings.ToLower(resp.Header.Get("content-type")), "html") {
|
||||
doc, perr := html.Parse(strings.NewReader(string(body)))
|
||||
if perr != nil { // html.Parse is lenient; this is effectively unreachable
|
||||
out = string(body)
|
||||
} else {
|
||||
out = htmlToMarkdown(doc)
|
||||
if in.Extract {
|
||||
if recon := extractRecon(doc); recon != "" {
|
||||
out += "\n\n" + recon
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
out = string(body)
|
||||
}
|
||||
return actool.Text(truncate(header+strings.TrimSpace(out), 50000)), nil
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
reWS = regexp.MustCompile(`[ \t]+`)
|
||||
reLineTrim = regexp.MustCompile(` *\n *`) // strip spaces hugging a newline
|
||||
reBlankLines = regexp.MustCompile(`\n{2,}`) // collapse blank lines → one newline
|
||||
// reEndpoint matches quoted absolute URLs and root-relative paths in inline JS.
|
||||
reEndpoint = regexp.MustCompile("[\"'`](https?://[^\"'`\\s]+|/[A-Za-z0-9._~$&'()*+,;=:@%/?#\\[\\]-]{2,})[\"'`]")
|
||||
)
|
||||
|
||||
// skipMarkdown are elements whose subtree carries no readable prose.
|
||||
var skipMarkdown = map[string]bool{"script": true, "style": true, "noscript": true, "svg": true, "template": true}
|
||||
|
||||
// htmlToMarkdown renders the visible content of a parsed document as Markdown.
|
||||
// Text nodes are already entity-decoded by the parser, so no unescaping is needed.
|
||||
func htmlToMarkdown(doc *html.Node) string {
|
||||
var b strings.Builder
|
||||
renderNode(&b, doc)
|
||||
s := reWS.ReplaceAllString(b.String(), " ")
|
||||
s = reLineTrim.ReplaceAllString(s, "\n") // drop indentation / trailing spaces per line
|
||||
s = reBlankLines.ReplaceAllString(s, "\n") // no blank lines — compact, token-lean output
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
func renderNode(b *strings.Builder, n *html.Node) {
|
||||
switch n.Type {
|
||||
case html.TextNode:
|
||||
b.WriteString(strings.ReplaceAll(n.Data, "\n", " "))
|
||||
return
|
||||
case html.ElementNode:
|
||||
if skipMarkdown[n.Data] {
|
||||
return
|
||||
}
|
||||
default:
|
||||
if n.Type != html.DocumentNode {
|
||||
return
|
||||
}
|
||||
}
|
||||
tag := ""
|
||||
if n.Type == html.ElementNode {
|
||||
tag = n.Data
|
||||
}
|
||||
switch tag {
|
||||
case "h1", "h2", "h3", "h4", "h5", "h6":
|
||||
b.WriteString("\n\n" + strings.Repeat("#", int(tag[1]-'0')) + " ")
|
||||
renderChildren(b, n)
|
||||
b.WriteString("\n\n")
|
||||
case "title":
|
||||
b.WriteString("\n\n# ")
|
||||
renderChildren(b, n)
|
||||
b.WriteString("\n\n")
|
||||
case "p", "div", "section", "article", "header", "footer", "main", "ul", "ol", "table", "tr", "blockquote":
|
||||
b.WriteString("\n\n")
|
||||
renderChildren(b, n)
|
||||
b.WriteString("\n\n")
|
||||
case "br":
|
||||
b.WriteString("\n")
|
||||
case "hr":
|
||||
b.WriteString("\n\n---\n\n")
|
||||
case "li":
|
||||
b.WriteString("\n- ")
|
||||
renderChildren(b, n)
|
||||
case "a":
|
||||
var inner strings.Builder
|
||||
renderChildren(&inner, n)
|
||||
text := strings.TrimSpace(inner.String())
|
||||
if href := attr(n, "href"); href != "" && text != "" {
|
||||
fmt.Fprintf(b, "[%s](%s)", text, href)
|
||||
} else {
|
||||
b.WriteString(text)
|
||||
}
|
||||
case "img":
|
||||
if src := attr(n, "src"); src != "" {
|
||||
fmt.Fprintf(b, "", attr(n, "alt"), src)
|
||||
}
|
||||
case "strong", "b":
|
||||
b.WriteString("**")
|
||||
renderChildren(b, n)
|
||||
b.WriteString("**")
|
||||
case "em", "i":
|
||||
b.WriteString("*")
|
||||
renderChildren(b, n)
|
||||
b.WriteString("*")
|
||||
case "code":
|
||||
b.WriteString("`")
|
||||
renderChildren(b, n)
|
||||
b.WriteString("`")
|
||||
case "pre":
|
||||
var inner strings.Builder
|
||||
textOnly(&inner, n)
|
||||
b.WriteString("\n\n```\n" + strings.TrimSpace(inner.String()) + "\n```\n\n")
|
||||
default:
|
||||
renderChildren(b, n)
|
||||
}
|
||||
}
|
||||
|
||||
func renderChildren(b *strings.Builder, n *html.Node) {
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
renderNode(b, c)
|
||||
}
|
||||
}
|
||||
|
||||
// extractRecon walks the document for security-relevant signal that Markdown
|
||||
// rendering drops: comments, script sources, inline endpoints, forms, meta, links.
|
||||
func extractRecon(doc *html.Node) string {
|
||||
var comments, scriptSrcs, inlineEndpoints, forms, metas, links []string
|
||||
seenLink := map[string]bool{}
|
||||
|
||||
var walk func(n *html.Node)
|
||||
walk = func(n *html.Node) {
|
||||
switch n.Type {
|
||||
case html.CommentNode:
|
||||
if c := oneLine(n.Data, 300); c != "" {
|
||||
comments = append(comments, c)
|
||||
}
|
||||
case html.ElementNode:
|
||||
switch n.Data {
|
||||
case "script":
|
||||
if src := attr(n, "src"); src != "" {
|
||||
scriptSrcs = append(scriptSrcs, src)
|
||||
} else {
|
||||
var body strings.Builder
|
||||
textOnly(&body, n)
|
||||
for _, m := range reEndpoint.FindAllStringSubmatch(body.String(), 40) {
|
||||
inlineEndpoints = append(inlineEndpoints, m[1])
|
||||
}
|
||||
}
|
||||
case "form":
|
||||
forms = append(forms, describeForm(n))
|
||||
case "meta":
|
||||
name := attr(n, "name")
|
||||
if name == "" {
|
||||
name = attr(n, "property")
|
||||
}
|
||||
if content := attr(n, "content"); name != "" && content != "" {
|
||||
metas = append(metas, name+"="+oneLine(content, 120))
|
||||
}
|
||||
case "a", "link":
|
||||
if h := attr(n, "href"); h != "" && !seenLink[h] {
|
||||
seenLink[h] = true
|
||||
links = append(links, h)
|
||||
}
|
||||
}
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
walk(c)
|
||||
}
|
||||
}
|
||||
walk(doc)
|
||||
|
||||
var b strings.Builder
|
||||
b.WriteString("=== RECON ===")
|
||||
reconSection(&b, "COMMENTS", comments)
|
||||
reconSection(&b, "SCRIPTS (src)", dedup(scriptSrcs))
|
||||
reconSection(&b, "INLINE ENDPOINTS", dedup(inlineEndpoints))
|
||||
reconSection(&b, "FORMS", forms)
|
||||
reconSection(&b, "META", metas)
|
||||
reconSection(&b, "LINKS", capSlice(links, 100))
|
||||
if b.Len() == len("=== RECON ===") {
|
||||
return "" // nothing worth reporting
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// describeForm summarizes a <form> as "METHOD action → name(type[=value]) …".
|
||||
// Hidden inputs keep their value (CSRF tokens, parameter defaults matter in recon).
|
||||
func describeForm(form *html.Node) string {
|
||||
method := strings.ToUpper(attr(form, "method"))
|
||||
if method == "" {
|
||||
method = "GET"
|
||||
}
|
||||
var inputs []string
|
||||
var walk func(n *html.Node)
|
||||
walk = func(n *html.Node) {
|
||||
if n.Type == html.ElementNode {
|
||||
switch n.Data {
|
||||
case "input", "textarea", "select":
|
||||
typ := attr(n, "type")
|
||||
if typ == "" {
|
||||
typ = n.Data
|
||||
}
|
||||
desc := attr(n, "name") + "(" + typ
|
||||
if typ == "hidden" {
|
||||
if v := attr(n, "value"); v != "" {
|
||||
desc += "=" + oneLine(v, 60)
|
||||
}
|
||||
}
|
||||
inputs = append(inputs, desc+")")
|
||||
}
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
walk(c)
|
||||
}
|
||||
}
|
||||
walk(form)
|
||||
return fmt.Sprintf("%s %s → %s", method, attr(form, "action"), strings.Join(inputs, " "))
|
||||
}
|
||||
|
||||
// --- small helpers ---
|
||||
|
||||
func attr(n *html.Node, key string) string {
|
||||
for _, a := range n.Attr {
|
||||
if a.Key == key {
|
||||
return a.Val
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func textOnly(b *strings.Builder, n *html.Node) {
|
||||
if n.Type == html.TextNode {
|
||||
b.WriteString(n.Data)
|
||||
return
|
||||
}
|
||||
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
||||
textOnly(b, c)
|
||||
}
|
||||
}
|
||||
|
||||
func oneLine(s string, max int) string {
|
||||
s = strings.Join(strings.Fields(s), " ")
|
||||
if len(s) > max {
|
||||
return s[:max] + "…"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func reconSection(b *strings.Builder, title string, items []string) {
|
||||
if len(items) == 0 {
|
||||
return
|
||||
}
|
||||
b.WriteString("\n\n## " + title + "\n")
|
||||
for _, it := range items {
|
||||
b.WriteString("- " + it + "\n")
|
||||
}
|
||||
}
|
||||
|
||||
func dedup(in []string) []string {
|
||||
seen := map[string]bool{}
|
||||
out := in[:0:0]
|
||||
for _, s := range in {
|
||||
if s == "" || seen[s] {
|
||||
continue
|
||||
}
|
||||
seen[s] = true
|
||||
out = append(out, s)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func capSlice(in []string, n int) []string {
|
||||
if len(in) <= n {
|
||||
return in
|
||||
}
|
||||
return append(in[:n:n], fmt.Sprintf("… (%d more)", len(in)-n))
|
||||
}
|
||||
|
||||
// truncate retains the SDK's head/tail output limit.
|
||||
func truncate(s string, max int) string {
|
||||
if max <= 0 || len(s) <= max {
|
||||
return s
|
||||
}
|
||||
half := max / 2
|
||||
return s[:half] + fmt.Sprintf("\n\n... [%d characters truncated] ...\n\n", len(s)-max) + s[len(s)-half:]
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package webfetch
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
actool "github.com/Autumn-27/norma/tool"
|
||||
)
|
||||
|
||||
func callFetch(t *testing.T, tool actool.CoreTool, target string, extract bool) actool.Result {
|
||||
t.Helper()
|
||||
input, err := json.Marshal(map[string]any{"url": target, "extract": extract})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := tool.Call(context.Background(), input, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func TestIdentityAcrossRoutes(t *testing.T) {
|
||||
for _, ua := range []string{"", "Captured-UA/1.0"} {
|
||||
for _, route := range []string{"direct", "proxy", "fallback"} {
|
||||
t.Run(route+"/"+ua, func(t *testing.T) {
|
||||
t.Setenv("ARTEX_HTTP_USER_AGENT", ua)
|
||||
var targetCalls, proxyCalls atomic.Int32
|
||||
checkUA := func(r *http.Request) {
|
||||
if got := r.Header.Get("User-Agent"); got != ua {
|
||||
t.Errorf("User-Agent = %q, want %q", got, ua)
|
||||
}
|
||||
if _, present := r.Header["User-Agent"]; ua == "" && present {
|
||||
t.Error("empty User-Agent present on wire")
|
||||
}
|
||||
}
|
||||
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
targetCalls.Add(1)
|
||||
checkUA(r)
|
||||
if r.URL.Path == "/start" {
|
||||
http.Redirect(w, r, "/page", http.StatusFound)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
fmt.Fprint(w, `<h1>Page</h1><!--keep--><script src="/app.js"></script><form action="/submit"><input name="csrf" type="hidden" value="token"></form>`)
|
||||
}))
|
||||
defer target.Close()
|
||||
proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
proxyCalls.Add(1)
|
||||
checkUA(r)
|
||||
if route == "fallback" {
|
||||
conn, _, err := w.(http.Hijacker).Hijack()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
conn.Close() // Deterministic EOF, causing the SDK-compatible retry.
|
||||
return
|
||||
}
|
||||
req := r.Clone(r.Context())
|
||||
req.RequestURI = ""
|
||||
// A forwarding proxy must preserve absence, like ARTEX's sink.
|
||||
if _, present := req.Header["User-Agent"]; !present {
|
||||
req.Header.Set("User-Agent", "")
|
||||
}
|
||||
resp, err := target.Client().Do(req)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
w.WriteHeader(http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
for key, values := range resp.Header {
|
||||
w.Header()[key] = values
|
||||
}
|
||||
w.WriteHeader(resp.StatusCode)
|
||||
io.Copy(w, resp.Body)
|
||||
}))
|
||||
defer proxy.Close()
|
||||
cfg := actool.WebFetchConfig{}
|
||||
if route != "direct" {
|
||||
cfg.Proxy = proxy.URL
|
||||
}
|
||||
result := callFetch(t, New(cfg), target.URL+"/start", true)
|
||||
for _, want := range []string{"[200 OK]", "# Page", "=== RECON ===", "keep", "/app.js", "csrf(hidden=token)"} {
|
||||
if result.IsError || !strings.Contains(result.Flatten(), want) {
|
||||
t.Fatalf("missing %q in result: %+v", want, result)
|
||||
}
|
||||
}
|
||||
if targetCalls.Load() != 2 {
|
||||
t.Errorf("target calls = %d, want 2", targetCalls.Load())
|
||||
}
|
||||
if route != "direct" && proxyCalls.Load() == 0 {
|
||||
t.Error("configured proxy was bypassed")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTLSAndStaticAssetGuard(t *testing.T) {
|
||||
t.Setenv("ARTEX_HTTP_USER_AGENT", "Captured-UA/1.0")
|
||||
var calls atomic.Int32
|
||||
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls.Add(1)
|
||||
if r.UserAgent() != "Captured-UA/1.0" {
|
||||
t.Errorf("User-Agent = %q", r.UserAgent())
|
||||
}
|
||||
fmt.Fprint(w, "verified")
|
||||
}))
|
||||
defer server.Close()
|
||||
certPath := filepath.Join(t.TempDir(), "ca.pem")
|
||||
cert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: server.Certificate().Raw})
|
||||
if err := os.WriteFile(certPath, cert, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tool := New(actool.WebFetchConfig{CACert: certPath})
|
||||
if result := callFetch(t, tool, server.URL, false); result.IsError || !strings.Contains(result.Flatten(), "verified") {
|
||||
t.Fatalf("CA trust lost: %+v", result)
|
||||
}
|
||||
if result := callFetch(t, tool, server.URL+"/app.js?v=1", false); !result.IsError {
|
||||
t.Fatalf("static asset guard lost: %+v", result)
|
||||
}
|
||||
if calls.Load() != 1 {
|
||||
t.Errorf("static asset triggered network request: calls = %d", calls.Load())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSDKMetadataPreserved(t *testing.T) {
|
||||
base := actool.NewWebFetch(actool.WebFetchConfig{})
|
||||
local := New(actool.WebFetchConfig{})
|
||||
if local.Name() != base.Name() || local.Description() != base.Description() || !reflect.DeepEqual(local.InputSchema(), base.InputSchema()) {
|
||||
t.Fatal("SDK tool contract changed")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user