Files
ai/controllers/message_util.go
zeekay 99f2e88361 A tenant's URL is fetched by outbound's rule, and never read as a path
Every address a request names now reaches the network through one client,
github.com/hanzoai/outbound (proxy.Public), which judges the address at
the dial, on the IP the socket connects to, and re-dials and re-judges
every redirect.

What it replaces failed three ways. A message href was checked by
util.Fetchable and then fetched by a different client: the name was
resolved twice, so a rebinding answer passed the check, and a redirect
to 169.254.169.254 was followed unchecked. The check itself was
net.IP's predicates, which admit NAT64 64:ff9b::a9fe:a9fe, 6to4,
Teredo, 198.18/15, 240/4 and the TEST-NETs; proxy/public.go's routable
admitted the same. And /v1/ai/rag/embed's url and /v1/ai/rag/ingest's
files[].url had no check at all: a loopback or metadata URL was
fetched, and a scheme-less string was read off this machine's disk.

A tenant document now goes through txt.GetParsedTextFromAddress (http
and https only, over proxy.Public); txt.GetParsedTextFromUrl is for
addresses this system stored. The Go crawler fetches every page and
robots.txt through proxy.Public, and the browser engine is handed only
addresses outbound.Host finds public.
2026-10-18 22:21:42 -07:00

108 lines
3.7 KiB
Go

// Copyright 2023-2025 Hanzo AI Inc. All Rights Reserved.
// Portions Copyright 2023 The OpenAgent Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"bufio"
"encoding/json"
"fmt"
"path/filepath"
"regexp"
"github.com/hanzoai/ai/object"
"github.com/hanzoai/ai/txt"
)
// noteFailure records a failure on the message and answers the text to tell the
// client. Recording it and telling them are separate things: there is one way to
// record, and two connections a client may be listening on.
func noteFailure(message *object.Message, errorText string, lang string) string {
if message == nil {
return errorText
}
var err error
if !message.IsAlerted {
if err = message.SendErrorEmail(errorText, lang); err != nil {
errorText = fmt.Sprintf("%s\n%s", errorText, err.Error())
}
}
if message.ErrorText != errorText || !message.IsAlerted || err != nil {
message.ErrorText = errorText
message.IsAlerted = true
if _, err = object.UpdateMessage(message.GetId(), message, false); err != nil {
errorText = fmt.Sprintf("%s\n%s", errorText, err.Error())
}
}
return errorText
}
// errorEvent is the one shape a failure takes on a chat stream.
func errorEvent(text string) string {
return fmt.Sprintf("event: myerror\ndata: %s\n\n", text)
}
// ResponseErrorStream tells a client still holding the request's own connection.
func (c *ApiController) ResponseErrorStream(message *object.Message, errorText string) {
text := noteFailure(message, errorText, c.GetAcceptLanguage())
if err := c.Bytes(c.Fiber().Response().StatusCode(), []byte(errorEvent(text))); err != nil {
c.ResponseError(err.Error())
}
}
// streamError records a failure on the message and tells the client through the
// stream's own writer.
//
// It is the in-stream counterpart of ResponseErrorStream, and the difference is
// which connection is still there to answer on. Once a streamed body is being
// produced, fasthttp is draining the writer from its own goroutine and the
// request context has been released — so the error event goes out through w, and
// the language has to be carried in rather than read off a header.
func streamError(w *bufio.Writer, message *object.Message, errorText string, lang string) {
_, _ = w.WriteString(errorEvent(noteFailure(message, errorText, lang)))
_ = w.Flush()
}
func refineQuestionTextViaParsingUrlContent(question string, lang string) (string, error) {
re := regexp.MustCompile(`href="([^"]+)"`)
urls := re.FindStringSubmatch(question)
if len(urls) == 0 {
return question, nil
}
// The href came out of the question a person typed, so it is an address a
// request named: read only as one, through outbound's rule (no path on this
// disk, no neighbour on this network, no redirect to either).
href := urls[1]
ext := filepath.Ext(href)
content, err := txt.GetParsedTextFromAddress(href, ext, lang)
if err != nil {
return "", err
}
aTag := regexp.MustCompile(`<a\s+[^>]*href=["']([^"']+)["'][^>]*>.*?</a>`)
res := aTag.ReplaceAllString(question, content)
return res, nil
}
func ConvertMessageDataToJSON(data string) ([]byte, error) {
jsonData := map[string]string{"text": data}
jsonBytes, err := json.Marshal(jsonData)
if err != nil {
return nil, err
}
return jsonBytes, nil
}