mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-04 19:04:37 +03:00
## Description
`isRefererAllowed` compares `new URL(referer).origin`. For non-special
schemes that is the opaque string `"null"`:
```js
new URL('assets://./').origin // 'null'
new URL('capacitor://localhost/').origin // 'null'
new URL('file:///index.html').origin // 'null'
```
So a `Referer` coming from the desktop (`assets://.`), mobile
(`capacitor://localhost`) or legacy (`file://`) clients can never match
the allowed origins, although the same origins are accepted when they
arrive in an `Origin` header (`DESKTOP_CLIENT_ORIGINS` /
`MOBILE_CLIENT_ORIGINS`, merged into the worker's list since #15547).
Requests that carry only a `Referer`, such as `<img
src=".../api/worker/image-proxy?...">` loads, are rejected with `400
Invalid header`.
This serializes such origins as `protocol//host`, which is how a browser
writes them in an `Origin` header. http(s) referers are unchanged.
Refs #14865 (the `Origin` half of that issue is already fixed on
canary).
## Testing
- Added `plugins/worker/__tests__/headers.spec.ts`: http(s) referers,
custom-scheme referers from the list, custom-scheme referers not in the
list, invalid input.
- I could not run the full `ava` suite locally. The function was checked
in isolation with Node on the same cases (9/9).
## Not included
Allowing `image-proxy` requests that have neither `Origin` nor `Referer`
(bug 3 in #14865). That loosens the check and I could not confirm what
the Electron `<img>` loads actually send.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **Bug Fixes**
* Referer checks now correctly recognize allowed client origins using
custom URL schemes, alongside allowed HTTP and HTTPS origins. Unlisted
origins and invalid URLs continue to be rejected, helping ensure
requests from supported clients are handled consistently without
broadening access to unapproved origins.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->