What: Settings › Network and sharing › Web pages now takes a browser
extension's origin besides http(s) ones: chrome-extension://<id>
(Chromium, the id 32 letters a-p), moz-extension://<uuid> (Firefox's
Internal UUID) and safari-web-extension://<uuid> (Safari). cleanOrigin
takes only those three schemes, only with an id of that browser's shape,
lower-cased and with a trailing "/" let go; a bare scheme, a wildcard, a
path, a port, user info, a query or another browser's id shape is
refused. The GUI maps the new refusal to the same message, which now
names the extension form, in en/zh/zh-TW/ja/de, and a listed origin
wraps instead of being cut (an extension's runs to 50-60 characters and
was ellipsized at a 440px panel). The 401 a listed caller gets without a
key names extensions too. Docs: gateway-routing.md, the settings field.
Why: an extension without host_permissions (Sider and the like) is held
to CORS; the gateway never answered its preflight, since its origin
couldn't be listed. Nothing else in the access rules changes: corsGuard
treats a listed extension exactly as a listed page (preflight answered,
every call needs an enabled gateway key or gets 401, even on loopback),
an unlisted extension passes with no CORS headers as before (foreignPage
still only judges http/https), and lanGuard's rebound/Host checks, the
allow list and every other route guard are untouched.
The Origin each browser sends was checked, not guessed: real Chromium
(below) sent Origin: chrome-extension://<32 a-p>; Firefox's is
moz-extension://<Internal UUID> (Gotify's and others' CORS docs list
it); Safari's is safari-web-extension://<lowercase uuid>, per Apple's
developer forums (thread 654839), where an Apple engineer notes the uuid
changes on each Safari launch, so a Safari listing lasts until it
restarts (said in the doc). A content script's fetch carries the page's
origin (MDN), so only extension pages, popups and workers are covered.
Run, at 28aac86bc:
- TestCleanExtensionOrigins (good ids, upper case, trailing slash;
malformed, wildcard, bare scheme, path, port, userinfo, query, swapped
id shapes) and TestCORSExtensionOrigin (through lanGuard: unlisted
preflight/request get no CORS headers and the request runs as before;
listed chrome and moz preflight 204 with the headers; keyed call 200
with Access-Control-Allow-Origin; keyless or wrong key 401 without
reaching the vendor; another id still unlisted). Both fail on the old
origins.go (the extension origin "is not an origin").
- go test ./internal/settings ./internal/gateway ./internal/gui: green.
- gofmt -l clean; go vet ./... darwin, GOOS=linux -tags nogui,
GOOS=windows: clean.
- GUI: cors-origins (extended: an extension added upper-cased with a
trailing slash is saved lower-cased and fits the panel, a malformed id
and chrome-extension://* refused in the page's language), i18n-keys,
gui-de, gui-ja, gui-zh-tw, allow-ips, lan-docker, chromium and webkit:
green. cors-origins fails on the old app.js/app.css (the name was cut,
the extension refusal untranslated).
- Real use: an unpacked MV3 extension with no host_permissions, loaded in
headless Chromium 1234 (playwright launchPersistentContext,
--load-extension), fetched a sandbox gateway (lanGuard(New().Handler())
on 127.0.0.1:3591 under a temp HOME, fake vendor) from its service
worker and its own page. Not listed: "Failed to fetch" in all four
cases. Listed (typed upper-case with "/"): with the key, read 200 and
the vendor's reply; without it, read the 401.
Not tried: Firefox and Safari extensions for real (their origins are from
the sources above), Edge/Brave, and the full magpie serve binary (the
harness ran the gateway's own handler stack, not its background loops).
89 lines
3.3 KiB
Go
89 lines
3.3 KiB
Go
package settings
|
||
|
||
import (
|
||
"fmt"
|
||
"net"
|
||
"net/url"
|
||
"regexp"
|
||
"slices"
|
||
"strings"
|
||
)
|
||
|
||
// CleanOrigins is the web origins a user typed for CORSOrigins, as a
|
||
// browser's Origin header spells them: scheme://host[:port], lower-cased,
|
||
// with no path and the scheme's default port left out. http and https are
|
||
// origins, and so is a browser extension's page by its id (#1051,
|
||
// SkyAerope: an extension without host permissions is held to CORS like a
|
||
// page), as extensionIDs spells them; "*" is refused, since it would let
|
||
// every web page or extension the user has spend their subscriptions.
|
||
func CleanOrigins(in []string) ([]string, error) {
|
||
var out []string
|
||
for _, raw := range in {
|
||
o, err := cleanOrigin(raw)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if o != "" && !slices.Contains(out, o) {
|
||
out = append(out, o)
|
||
}
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
func cleanOrigin(raw string) (string, error) {
|
||
raw = strings.TrimSpace(raw)
|
||
if raw == "" {
|
||
return "", nil
|
||
}
|
||
if strings.Contains(raw, "*") {
|
||
return "", fmt.Errorf("%q: name each origin; a wildcard would let every web page use magpie", raw)
|
||
}
|
||
if o, ok, err := extensionOrigin(raw); ok {
|
||
return o, err
|
||
}
|
||
u, err := url.Parse(strings.TrimSuffix(raw, "/"))
|
||
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" || u.User != nil ||
|
||
(u.Path != "" && u.Path != "/") || u.RawQuery != "" || u.Fragment != "" {
|
||
return "", fmt.Errorf("%q is not an origin: write it as http://localhost:3000 or https://app.example.com, or an extension's as chrome-extension://<its ID>", raw)
|
||
}
|
||
host, port := strings.ToLower(u.Hostname()), u.Port()
|
||
if (u.Scheme == "http" && port == "80") || (u.Scheme == "https" && port == "443") {
|
||
port = ""
|
||
}
|
||
if strings.Contains(host, ":") {
|
||
host = "[" + host + "]"
|
||
}
|
||
if port != "" {
|
||
host = net.JoinHostPort(strings.Trim(host, "[]"), port)
|
||
}
|
||
return u.Scheme + "://" + host, nil
|
||
}
|
||
|
||
// extensionIDs is the host each browser's extension origin has, as it
|
||
// sends it in Origin for a fetch from the extension's own pages, popup or
|
||
// service worker (a content script's fetch carries the page's origin):
|
||
// Chromium's chrome-extension://<id>, the id 32 letters a–p; Firefox's
|
||
// moz-extension://<uuid>, its per-install Internal UUID; Safari's
|
||
// safari-web-extension://<uuid>, which Safari picks anew on each launch.
|
||
var extensionIDs = map[string]*regexp.Regexp{
|
||
"chrome-extension": regexp.MustCompile(`^[a-p]{32}$`),
|
||
"moz-extension": regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`),
|
||
"safari-web-extension": regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`),
|
||
}
|
||
|
||
// extensionOrigin is raw as an extension's origin, lower-cased, with a
|
||
// trailing "/" let go; ok says raw names an extension scheme at all, and
|
||
// then err says when its id isn't one that browser gives.
|
||
func extensionOrigin(raw string) (o string, ok bool, err error) {
|
||
scheme, rest, found := strings.Cut(strings.ToLower(raw), "://")
|
||
id := extensionIDs[scheme]
|
||
if !found || id == nil {
|
||
return "", false, nil
|
||
}
|
||
rest = strings.TrimSuffix(rest, "/")
|
||
if !id.MatchString(rest) {
|
||
return "", true, fmt.Errorf("%q is not an extension's origin: write it as %s://<its ID>, the ID as the browser's extensions page shows it", raw, scheme)
|
||
}
|
||
return scheme + "://" + rest, true, nil
|
||
}
|