Files
zeekayandHanzo Dev a386271c80 isolation is a value the org sets, and an empty one is not runc
Every sandbox was created privileged under the daemon's default runtime, and
the setting that would have changed it defaulted to an empty string that
silently meant runc. For a service whose whole premise is running code
somebody else wrote, that is not a boundary.

An org sets its isolation and the create request carries it, so a sandbox
records what it was made with: changing the policy moves new sandboxes and
leaves running ones alone. gvisor is the default. firecracker is delivered
through kata's shim, which is why it is one name rather than two. runc is
refused unless the runner names that org, and an empty list names nobody.

Absent means the default, unknown is a refusal, and a runtime the daemon does
not have is a refusal. Nothing substitutes quietly.

The runner took an organization in a field called UserId with a note beside
it saying so. Deciding who may have runc through a field named after
something else is not reviewable.

Privileged is replaced by dropping everything and granting the eight the
daemon uses. Nothing the sandbox does broke; what stopped working is mount,
mknod, raw sockets, chroot, writing to proc, and seeing the host's disks.
AUDIT_WRITE is among them because without it sudo works and complains every
time.

The creation histogram's lowest bucket was a tenth of a second, so the metric
could not observe the claim the product makes about its own speed. It can
now, and it carries the isolation, so comparing them is a query.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-11 13:47:31 -07:00

189 lines
5.1 KiB
Go

// Copyright 2025 Hanzo Industries Inc.
// SPDX-License-Identifier: AGPL-3.0
package proxy
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"time"
common_errors "github.com/hanzoai/runtime/libs/common-go/pkg/errors"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func (p *Proxy) GetProxyTarget(ctx *gin.Context) (*url.URL, map[string]string, error) {
// Extract port and sandbox ID from the host header
// Expected format: 1234-some-id-uuid.proxy.domain
targetPort, sandboxID, err := p.parseHost(ctx.Request.Host)
if err != nil {
ctx.Error(common_errors.NewBadRequestError(err))
return nil, nil, err
}
if targetPort == "" {
ctx.Error(common_errors.NewBadRequestError(errors.New("target port is required")))
return nil, nil, errors.New("target port is required")
}
if sandboxID == "" {
ctx.Error(common_errors.NewBadRequestError(errors.New("sandbox ID is required")))
return nil, nil, errors.New("sandbox ID is required")
}
isPublic, err := p.getSandboxPublic(ctx, sandboxID)
if err != nil {
ctx.Error(common_errors.NewBadRequestError(fmt.Errorf("failed to get sandbox public status: %w", err)))
return nil, nil, fmt.Errorf("failed to get sandbox public status: %w", err)
}
if !*isPublic || targetPort == "22222" {
err, didRedirect := p.Authenticate(ctx, sandboxID)
if err != nil {
if !didRedirect {
ctx.Error(common_errors.NewUnauthorizedError(err))
}
return nil, nil, err
}
}
runnerInfo, err := p.getRunnerInfo(ctx, sandboxID)
if err != nil {
ctx.Error(common_errors.NewBadRequestError(fmt.Errorf("failed to get runner info: %w", err)))
return nil, nil, fmt.Errorf("failed to get runner info: %w", err)
}
// Build the target URL
targetURL := fmt.Sprintf("%s/sandboxes/%s/toolbox/proxy/%s", runnerInfo.ApiUrl, sandboxID, targetPort)
// Get the wildcard path and normalize it
path := ctx.Param("path")
// Ensure path always has a leading slash but not duplicate slashes
if path == "" {
path = "/"
} else if !strings.HasPrefix(path, "/") {
path = "/" + path
}
// Create the complete target URL with path
target, err := url.Parse(fmt.Sprintf("%s%s", targetURL, path))
if err != nil {
ctx.Error(common_errors.NewBadRequestError(fmt.Errorf("failed to parse target URL: %w", err)))
return nil, nil, fmt.Errorf("failed to parse target URL: %w", err)
}
return target, map[string]string{
"X-Runtime-Authorization": fmt.Sprintf("Bearer %s", runnerInfo.ApiKey),
"X-Forwarded-Host": ctx.Request.Host,
}, nil
}
func (p *Proxy) getRunnerInfo(ctx context.Context, sandboxId string) (*RunnerInfo, error) {
has, err := p.runnerCache.Has(ctx, sandboxId)
if err != nil {
return nil, err
}
if has {
return p.runnerCache.Get(ctx, sandboxId)
}
runner, _, err := p.apiclient.RunnersAPI.GetRunnerBySandboxId(context.Background(), sandboxId).Execute()
if err != nil {
return nil, err
}
info := RunnerInfo{
ApiUrl: runner.ApiUrl,
ApiKey: runner.ApiKey,
}
err = p.runnerCache.Set(ctx, sandboxId, info, 1*time.Hour)
if err != nil {
log.Errorf("Failed to set runner info in cache: %v", err)
}
return &info, nil
}
func (p *Proxy) getSandboxPublic(ctx context.Context, sandboxId string) (*bool, error) {
has, err := p.sandboxPublicCache.Has(ctx, sandboxId)
if err != nil {
return nil, err
}
if has {
return p.sandboxPublicCache.Get(ctx, sandboxId)
}
isPublic := false
_, resp, _ := p.apiclient.PreviewAPI.IsSandboxPublic(context.Background(), sandboxId).Execute()
if resp != nil && resp.StatusCode == http.StatusOK {
isPublic = true
}
err = p.sandboxPublicCache.Set(ctx, sandboxId, isPublic, 2*time.Minute)
if err != nil {
log.Errorf("Failed to set sandbox public in cache: %v", err)
}
return &isPublic, nil
}
func (p *Proxy) getSandboxAuthKeyValid(ctx context.Context, sandboxId string, authKey string) (*bool, error) {
has, err := p.sandboxAuthKeyValidCache.Has(ctx, authKey)
if err != nil {
return nil, err
}
if has {
return p.sandboxAuthKeyValidCache.Get(ctx, authKey)
}
isValid := false
_, resp, _ := p.apiclient.PreviewAPI.IsValidAuthToken(context.Background(), sandboxId, authKey).Execute()
if resp != nil && resp.StatusCode == http.StatusOK {
isValid = true
}
err = p.sandboxAuthKeyValidCache.Set(ctx, authKey, isValid, 2*time.Minute)
if err != nil {
log.Errorf("Failed to set sandbox auth key valid in cache: %v", err)
}
return &isValid, nil
}
func (p *Proxy) parseHost(host string) (targetPort string, sandboxID string, err error) {
// Extract port and sandbox ID from the host header
// Expected format: 1234-some-id-uuid.proxy.domain
if host == "" {
return "", "", errors.New("host is required")
}
// Split the host to extract the port and sandbox ID
parts := strings.Split(host, ".")
if len(parts) == 0 {
return "", "", errors.New("invalid host format")
}
// Extract port from the first part (e.g., "1234-some-id-uuid")
hostPrefix := parts[0]
dashIndex := strings.Index(hostPrefix, "-")
if dashIndex == -1 {
return "", "", errors.New("invalid host format: port and sandbox ID not found")
}
targetPort = hostPrefix[:dashIndex]
sandboxID = hostPrefix[dashIndex+1:]
return targetPort, sandboxID, nil
}