Files
Jason 8e549b05d2 fix(server): detect svg root behind xml declaration (#15889)
## Description

Refs #15871

`get_mime` asks `infer` first, and `infer` returns `text/xml` for
anything that starts with `<?xml`. An SVG exported from draw.io,
Inkscape or Illustrator usually carries that declaration, so it was
labelled `text/xml`, while the same file without the declaration came
out as `image/svg+xml`.

`get_mime` now looks at the root element before calling `infer`. It
walks the first 64 KiB with `xmlparser` (already in the lock file
through `instant-xml`, now a direct dependency), skipping BOM,
whitespace, comments, processing instructions and DOCTYPE with an
internal subset, and stops at the first element name. A root of `svg` or
`prefix:svg` gives `image/svg+xml`. No DTD is fetched and no entity is
expanded.

`file-format` matches `<svg` as a bare prefix or substring, so
`<svg-image/>` or `<svgFoo>` used to come out as SVG. When the scan
finds a root with a different name, the result is now `text/xml`. When
the scan is inconclusive (truncated root tag, root beyond 64 KiB,
invalid UTF-8), the old result is kept.

Scope, to be precise about what this changes on canary: the normal image
upload path (`setBlob`, presigned and multipart) stores the MIME type
the client sends and never sniffs, so a correctly typed upload does not
hit this. What does hit it is every path that relies on server-side
detection: object storage writes without a content type
(`complete_for_body`), comment attachments (`getMime`), and the
`sniffMime` callers (copilot attachments, workspace files, transcripts,
conversation inbox, avatars). Blobs already stored as `text/xml` are not
migrated.

Security: `applyAttachHeaders` already treats both `text/xml` and
`image/svg+xml` as dangerous inline types (forced `attachment`
disposition plus `nosniff`), so download handling does not change.
Avatars and inbox images still go through `processImage`, which rejects
SVG just as before.

Tests:

- `cargo test -p affine_server_native file_type::tests`: declaration,
BOM, comments, DOCTYPE with entities, `xml-stylesheet` PI, `svg:svg`, a
long Illustrator-style header; non-SVG roots, `<svg>` inside comments,
entities and children, `<SVG>`, `<svg-image>`; truncated and empty
input; the 64 KiB bound.
- `cargo test -p affine_server_native
runtime::object_storage::fs::tests`: put without a content type, then
HEAD/GET, for bare SVG, declaration SVG and plain XML; explicit
`image/svg+xml` and `text/xml` are kept as given.
- `cargo clippy -p affine_server_native --lib` is clean.

## Checklist

- [x] I have signed the [AFFiNE Contributor License
Agreement](https://cla-assistant.io/toeverything/AFFiNE)
- [x] The PR targets the `canary` branch and its title follows
[Conventional Commits](https://www.conventionalcommits.org/)
- [x] Tests are added or updated where it makes sense
- [x] `yarn lint` and `yarn typecheck` pass locally (Rust-only change;
`cargo clippy` and `cargo fmt --check` on the touched files)
- [x] If the PR code includes AI-generated edits, I have carefully
reviewed it to ensure that the scope of the PR is consistent with what
is claimed in the title and description, and that there are no redundant
or invalid implementations.
- [x] I agree that maintainers may close the PR without discussion if
they consider the code quality to be too low.
2026-11-23 12:01:46 +08:00
..
2025-12-10 16:02:28 +08:00