214 lines
6.5 KiB
Go
214 lines
6.5 KiB
Go
package platform
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"errors"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/netip"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"gitea.entroia.com/Zayden-Jung/foundry-core/internal/codex"
|
|
)
|
|
|
|
func canvasPublicIP(ip netip.Addr) bool {
|
|
ip = ip.Unmap()
|
|
if !ip.IsValid() || !ip.IsGlobalUnicast() || ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
|
|
return false
|
|
}
|
|
for _, cidr := range []string{"0.0.0.0/8", "100.64.0.0/10", "168.63.129.16/32", "192.0.0.0/24", "192.0.2.0/24", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4", "2001:db8::/32"} {
|
|
if netip.MustParsePrefix(cidr).Contains(ip) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
func canvasImageURL(raw string) bool {
|
|
u, err := url.Parse(raw)
|
|
return err == nil && u.Scheme == "https" && u.Hostname() != "" && u.User == nil && len(raw) < 2048
|
|
}
|
|
func fetchCanvasImage(ctx context.Context, src string) ([]byte, error) {
|
|
if !canvasImageURL(src) {
|
|
return nil, errors.New("invalid_image_input")
|
|
}
|
|
transport := &http.Transport{DisableCompression: true, DialContext: func(ctx context.Context, network, address string) (net.Conn, error) {
|
|
host, port, err := net.SplitHostPort(address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
addresses, err := net.DefaultResolver.LookupNetIP(ctx, "ip", host)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// Reject the whole DNS answer if it contains a private address. Dial a
|
|
// validated literal so a second DNS resolution cannot rebind the hostname.
|
|
if len(addresses) == 0 {
|
|
return nil, errors.New("invalid_image_host")
|
|
}
|
|
for _, ip := range addresses {
|
|
if !canvasPublicIP(ip) {
|
|
return nil, errors.New("invalid_image_host")
|
|
}
|
|
}
|
|
dialer := net.Dialer{Timeout: 5 * time.Second}
|
|
for _, ip := range addresses {
|
|
connection, e := dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), port))
|
|
if e == nil {
|
|
return connection, nil
|
|
}
|
|
err = e
|
|
}
|
|
return nil, err
|
|
}}
|
|
defer transport.CloseIdleConnections()
|
|
client := &http.Client{Transport: transport, Timeout: 5 * time.Second, CheckRedirect: func(request *http.Request, via []*http.Request) error {
|
|
if len(via) > 3 || !canvasImageURL(request.URL.String()) {
|
|
return errors.New("invalid_image_redirect")
|
|
}
|
|
return nil
|
|
}}
|
|
request, err := http.NewRequestWithContext(ctx, "GET", src, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode != 200 || response.ContentLength > assetLimit {
|
|
return nil, errors.New("invalid_image_input")
|
|
}
|
|
data, err := io.ReadAll(io.LimitReader(response.Body, assetLimit+1))
|
|
if err != nil || len(data) > assetLimit {
|
|
return nil, errors.New("invalid_image_input")
|
|
}
|
|
return data, nil
|
|
}
|
|
func (s *service) freezeCanvasImage(ctx context.Context, tx *sql.Tx, project, workspace, board, account string, content map[string]any) (map[string]any, error) {
|
|
id, _ := content["assetId"].(string)
|
|
src, _ := content["src"].(string)
|
|
prefix := s.assetURL(project, workspace, "")
|
|
if id == "" && strings.HasPrefix(src, prefix) {
|
|
id = strings.TrimPrefix(src, prefix)
|
|
}
|
|
if id != "" {
|
|
if !uuidSlugPattern.MatchString(id) {
|
|
return nil, errors.New("invalid_image_input")
|
|
}
|
|
allowed, err := assetReadable(queueRequest(ctx), tx, project, workspace, id, account)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !allowed {
|
|
return nil, errors.New("invalid_image_input")
|
|
}
|
|
return map[string]any{"assetId": id}, nil
|
|
}
|
|
var data []byte
|
|
var err error
|
|
if strings.HasPrefix(src, "data:image/") {
|
|
_, encoded, ok := strings.Cut(src, ",")
|
|
if !ok {
|
|
return nil, errors.New("invalid_image_input")
|
|
}
|
|
data, err = base64.StdEncoding.DecodeString(encoded)
|
|
} else {
|
|
data, err = fetchCanvasImage(ctx, src)
|
|
}
|
|
if err != nil || len(data) == 0 || len(data) > assetLimit {
|
|
return nil, errors.New("invalid_image_input")
|
|
}
|
|
mime, width, height, err := inspectAsset(data)
|
|
if err != nil {
|
|
return nil, errors.New("invalid_image_input")
|
|
}
|
|
digest := sha256.Sum256(data)
|
|
err = tx.QueryRowContext(ctx, `INSERT INTO canvas_assets(project,workspace_id,uploaded_by,mime_type,bytes,width,height,digest,idempotency_key) VALUES($1,$2,$3,$4,$5,$6,$7,$8,gen_random_uuid()) RETURNING id::text`, project, workspace, account, mime, data, width, height, hex.EncodeToString(digest[:])).Scan(&id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
_, err = tx.ExecContext(ctx, `INSERT INTO canvas_asset_bindings(project,workspace_id,asset_id,board_id) VALUES($1,$2,$3,$4)`, project, workspace, id, board)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]any{"assetId": id}, nil
|
|
}
|
|
func (s *service) hydrateCanvasTask(ctx context.Context, run canvasQueueRun, step canvasAIStepPlan, results map[string]canvasStepResult, task *codex.CanvasTask) error {
|
|
ids := []string{}
|
|
if id, _ := step.FrozenContent["assetId"].(string); id != "" {
|
|
ids = append(ids, id)
|
|
}
|
|
for _, source := range step.StaticInputs {
|
|
if id, _ := source.Content["assetId"].(string); id != "" {
|
|
ids = append(ids, id)
|
|
}
|
|
}
|
|
for _, edge := range run.Plan.Edges {
|
|
if edge.Target == step.ID {
|
|
if id := results[edge.Source].AssetID; id != "" {
|
|
ids = append(ids, id)
|
|
}
|
|
}
|
|
}
|
|
|
|
seen := map[string]bool{}
|
|
total := 0
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
if err = lockCanvasAIWorkspace(ctx, tx, run.Project, run.Workspace); err != nil {
|
|
return err
|
|
}
|
|
board, ok, err := resolveCanvasAITarget(ctx, tx, run.Project, run.Workspace, step)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return errors.New("target_removed")
|
|
}
|
|
_, role, err := lockMaterial(ctx, tx, run.Project, run.Workspace, board, run.Account)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if role == "viewer" {
|
|
return errors.New("permission_revoked")
|
|
}
|
|
for _, id := range ids {
|
|
if seen[id] {
|
|
continue
|
|
}
|
|
seen[id] = true
|
|
if len(seen) > 4 {
|
|
return errors.New("context_too_large")
|
|
}
|
|
allowed, err := assetReadable(queueRequest(ctx), tx, run.Project, run.Workspace, id, run.Account)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !allowed {
|
|
return errors.New("permission_revoked")
|
|
}
|
|
var image codex.CanvasImage
|
|
err = tx.QueryRowContext(ctx, `SELECT mime_type,bytes FROM canvas_assets WHERE project=$1 AND workspace_id=$2 AND id=$3`, run.Project, run.Workspace, id).Scan(&image.MIME, &image.Data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
total += len(image.Data)
|
|
if total > codex.CanvasImageBytesLimit {
|
|
return errors.New("context_too_large")
|
|
}
|
|
task.Images = append(task.Images, image)
|
|
}
|
|
return nil
|
|
}
|