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).
71 lines
2.9 KiB
Go
71 lines
2.9 KiB
Go
package settings
|
||
|
||
import (
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// An origin is saved as the browser sends it, whichever way the user wrote
|
||
// it, and a wildcard or anything that is not an origin is refused (#1051).
|
||
func TestCleanOrigins(t *testing.T) {
|
||
got, err := CleanOrigins([]string{" http://LocalHost:3000/ ", "https://a.example:443", "http://[::1]:8080", "http://a.example:80", "", "http://localhost:3000"})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
want := []string{"http://localhost:3000", "https://a.example", "http://[::1]:8080", "http://a.example"}
|
||
if strings.Join(got, " ") != strings.Join(want, " ") {
|
||
t.Fatalf("%v, want %v", got, want)
|
||
}
|
||
for _, bad := range []string{"*", "https://*.example.com", "localhost:3000", "file:///x", "http://a.example/path", "http://u:p@a.example", "http://a.example?x=1", "null"} {
|
||
if _, err := CleanOrigins([]string{bad}); err == nil {
|
||
t.Errorf("%q taken", bad)
|
||
}
|
||
}
|
||
}
|
||
|
||
// A browser extension's page is listed by its origin, as the browser
|
||
// sends it (#1051, SkyAerope on Discord: chrome-extension://<id> couldn't
|
||
// be added): Chromium's id of 32 letters a–p, Firefox's and Safari's
|
||
// UUID, however the user cased it or with a trailing "/". A bare scheme,
|
||
// a wildcard, an id that browser never gives, a path or a port is refused.
|
||
func TestCleanExtensionOrigins(t *testing.T) {
|
||
const chrome = "abcdefghijklmnopabcdefghijklmnop"
|
||
const uuid = "e0b5d7c7-c079-484b-8825-44d261383cb6"
|
||
got, err := CleanOrigins([]string{
|
||
"chrome-extension://" + chrome,
|
||
" Chrome-Extension://" + strings.ToUpper(chrome) + "/ ",
|
||
"moz-extension://" + strings.ToUpper(uuid) + "/",
|
||
"safari-web-extension://" + uuid,
|
||
})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
want := []string{"chrome-extension://" + chrome, "moz-extension://" + uuid, "safari-web-extension://" + uuid}
|
||
if strings.Join(got, " ") != strings.Join(want, " ") {
|
||
t.Fatalf("%v, want %v", got, want)
|
||
}
|
||
for _, bad := range []string{
|
||
"chrome-extension://", "chrome-extension:///", "chrome-extension://*", "chrome-extension://" + chrome[:31] + "*",
|
||
"chrome-extension://" + chrome[:31], // too short
|
||
"chrome-extension://" + chrome + "a", // too long
|
||
"chrome-extension://" + chrome[:31] + "q", // not a–p
|
||
"chrome-extension://" + chrome + "/popup.html", // a page, not an origin
|
||
"chrome-extension://" + chrome + ":80",
|
||
"chrome-extension://u@" + chrome,
|
||
"chrome-extension://" + chrome + "?x=1",
|
||
"chrome-extension://" + uuid, // Firefox's id under Chrome's scheme
|
||
"moz-extension://" + chrome, // and Chrome's under Firefox's
|
||
"moz-extension://{" + uuid + "}", // braces are not in the origin
|
||
"moz-extension://" + strings.ReplaceAll(uuid, "-", ""),
|
||
"moz-extension://" + uuid[:35] + "g",
|
||
"safari-web-extension://" + uuid + "/x",
|
||
"moz-extension://*",
|
||
"safari-web-extension://",
|
||
"ms-browser-extension://" + chrome,
|
||
} {
|
||
if o, err := CleanOrigins([]string{bad}); err == nil {
|
||
t.Errorf("%q taken as %v", bad, o)
|
||
}
|
||
}
|
||
}
|