mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 04:14:15 +00:00
Markdown rendered in finding reports, task reports and transcripts comes from agent output and target content. An image in it was fetched by the operator's browser as soon as the view rendered, so an image URL pointing to an outside host could carry data out of the console with no click and outside the sandbox egress controls. The Markdown renderer now loads only same-origin images under /api/ and /uploads/, checked on the normalized URL (dot segments resolved, protocol relative URLs, backslashes and encoded slashes refused). Any other image is shown as its alt text and URL, linked when it is an http(s) URL. The Content-Security-Policy adds img-src 'self' data: blob:, which keeps images off other origins for the whole console, Markdown or not. Tests: a Go test asserts the console CSP img-src directive, and a new test:markdown web test (wired into the CI web job) covers external, same-origin and tricky image URLs.
62 lines
2.5 KiB
Go
62 lines
2.5 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/netip"
|
|
)
|
|
|
|
// Security headers are split in two layers so the API and the operator console
|
|
// each get what applies to them.
|
|
|
|
// securityHeaders applies the response headers that are safe for every response,
|
|
// including the JSON API. It wraps the whole handler tree so no route can forget
|
|
// them: a missing nosniff on a JSON reply is exactly the kind of gap that only
|
|
// shows up on the one route nobody remembered.
|
|
func (s *Server) securityHeaders(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
h := w.Header()
|
|
h.Set("X-Content-Type-Options", "nosniff")
|
|
// Do not leak workspace paths and task ids to third parties.
|
|
h.Set("Referrer-Policy", "same-origin")
|
|
// The API is JSON, but a proxied error page must still not be framed.
|
|
h.Set("X-Frame-Options", "SAMEORIGIN")
|
|
// img-src keeps the browser from fetching images off-origin: Markdown from agent
|
|
// and target text could otherwise beacon data out with an image URL.
|
|
h.Set("Content-Security-Policy", "frame-ancestors 'self'; img-src 'self' data: blob:")
|
|
if s.isHTTPS(r) {
|
|
// Ignored by browsers over plain HTTP, so it only takes effect behind
|
|
// the TLS-terminating proxy this deployment is documented to use.
|
|
h.Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains")
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
|
|
// consoleHeaders applies the headers that only make sense on documents, on top of
|
|
// the ones securityHeaders already set. Called from the console route.
|
|
func consoleHeaders(h http.Header) {
|
|
// The console needs no device APIs; keep them from being promptable.
|
|
h.Set("Permissions-Policy", "camera=(), microphone=(), geolocation=(), payment=(), usb=()")
|
|
// Separate the console's browsing context from the tabs it opens.
|
|
h.Set("Cross-Origin-Opener-Policy", "same-origin")
|
|
h.Set("Cross-Origin-Resource-Policy", "same-origin")
|
|
}
|
|
|
|
// isHTTPS reports whether the request reached us over TLS, either directly or
|
|
// through a trusted proxy that says so. X-Forwarded-Proto is only believed when
|
|
// the peer is in ARTEX_TRUSTED_PROXIES, the same rule clientIP uses: a direct
|
|
// client could otherwise claim HTTPS and get a header it cannot back up.
|
|
func (s *Server) isHTTPS(r *http.Request) bool {
|
|
if r.TLS != nil {
|
|
return true
|
|
}
|
|
if r.Header.Get("X-Forwarded-Proto") != "https" {
|
|
return false
|
|
}
|
|
peer, err := netip.ParseAddr(peerIP(r))
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return isTrusted(peer, s.trustedProxies)
|
|
}
|