You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(svg): support valid SVGs without root width/height or viewBox - #348
Added a linear single-pass XML prolog and root <svg> detector (isSVGInput in src/svg.ts) so SVGs without width, height, or viewBox attributes on the root <svg> element (e.g., <symbol viewBox="..."> + <use> icon files or bare <svg>) are recognized when image-meta rejects them for missing dimensions.
Kept IPX's security guarantees intact: allowedInputFormats is still checked before touching SVGO/sharp, non-SVG HTML documents (<html><body><svg></svg></body></html>) are still rejected with 400 IPX_INVALID_IMAGE, and SVG passthrough remains sanitized by sanitizeSVGPlugin.
When rasterizing an SVG whose dimensions were missing from imageMeta, the Sharp metadata probe resolves width/height for downstream dimension clamping while preserving the existing multiPage height limit guard.
pnpm test (vitest run --coverage) — 9 test files passed, 609 tests passed (10 pre-existing skips on Windows in server.test.ts, fs-dirs.test.ts, unstorage.test.ts), including new unit & integration tests in test/svg.test.ts and test/index.test.ts
Summary by CodeRabbit
Bug Fixes
SVG images without explicit width or height are now recognized and can be passed through or resized.
SVG content, including symbol elements, is preserved during processing.
SVG images continue to follow allowed input format settings, and malformed SVG or non-SVG input receives an appropriate error.
No actionable comments were generated in the recent review.
Walkthrough
IPX now recognizes SVG input that lacks root dimensions when image metadata parsing fails. It probes Sharp for missing dimensions and continues through existing SVG or raster processing. Tests cover SVG recognition, processing, format restrictions, and invalid input.
Changes: Dimensionless SVG support
Layer / File(s)
Summary
Recognize SVG roots src/svg.ts, test/svg.test.ts
The new isSVGInput helper recognizes lowercase SVG roots after supported XML prolog content. Tests cover valid roots, prologs, and non-SVG input.
Process SVG without dimensions src/ipx.ts, test/index.test.ts, test/svg.test.ts
When metadata parsing fails, IPX accepts input recognized as SVG and supplies placeholder dimensions. The Sharp metadata probe runs when dimensions are missing and fills available values. Tests cover SVG pass-through, rasterization, input-format rejection, and invalid SVG errors.
sequenceDiagram
participant Caller
participant IPX
participant Meta as image-meta
participant Recognizer as isSVGInput
participant Sharp
Caller->>IPX: Submit image input
IPX->>Meta: Parse image metadata
Meta-->>IPX: Metadata parse fails
IPX->>Recognizer: Check input bytes for an SVG root
Recognizer-->>IPX: Return SVG recognition result
IPX->>Sharp: Probe metadata when dimensions are missing
Sharp-->>IPX: Return available dimensions
IPX-->>Caller: Return processed SVG or raster output
Loading
Merge Risk: Minimal · up to 98626
This change lets IPX accept valid SVGs whose root element has no width, height or viewBox, such as symbol/use sprites. HTML documents containing an SVG, and other non-SVG input, are still rejected with 400. No actionable merge-blocking risk was identified.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "fix(svg): support valid SVGs without root width/height or viewBox" matches the main change. The PR adds SVG recognition (isSVGInput in src/svg.ts) and processing for SVGs whose root svg lac…
Linked Issues check
[#287] The issue requires IPX to accept SVGs whose root <svg> lacks width, height, and viewBox, such as files that put viewBox only on a nested <symbol> used by <use>. Instead of returni…
Out of Scope Changes check
All changed areas support the #287 objective. src/svg.ts adds the root-element detector. src/ipx.ts adds the fallback path and the Sharp metadata probe for missing dimensions. The tests cover the …
Full details: Title check
Explanation
The title "fix(svg): support valid SVGs without root width/height or viewBox" matches the main change. The PR adds SVG recognition (isSVGInput in src/svg.ts) and processing for SVGs whose root svg lacks dimensions, and the linked issue #287 asks for the same behavior. The title is specific and concise and follows the conventional commit style. It does not list every detail, such as the Sharp metadata probe, which is acceptable.
Full details: Linked Issues check
Explanation
[#287] The issue requires IPX to accept SVGs whose root <svg> lacks width, height, and viewBox, such as files that put viewBox only on a nested <symbol> used by <use>. Instead of returning 400 IPX_INVALID_IMAGE, IPX should render them. The PR adds exported isSVGInput in src/svg.ts. It skips the BOM, whitespace, processing instructions, comments, and DOCTYPE, then checks that the first root element is <svg. src/ipx.ts (per the summary) uses this check when image-meta fails. It assigns placeholder dimensions, continues through the format checks and SVG or Sharp processing, and runs a Sharp metadata probe to fill missing width or height. The reported tests in test/index.test.ts cover a dimensionless inline SVG that passes through as svg+xml with its <symbol> retained, rasterizes to WebP at width 64, and is rejected by allowedInputFormats. The reported test/svg.test.ts cases cover prolog and root detection. Limitation: I could not read the src/ipx.ts diff or the test files (the diff was unavailable and the file read was truncated). The src/ipx.ts flow is established from the PR summary and the reported tests, not from line-level inspection.
Full details: Out of Scope Changes check
Explanation
All changed areas support the #287 objective. src/svg.ts adds the root-element detector. src/ipx.ts adds the fallback path and the Sharp metadata probe for missing dimensions. The tests cover the new behavior. The PR states that the multiPage height-limit guard is kept, that allowedInputFormats is still checked before SVGO or Sharp, that HTML documents with an embedded <svg> are still rejected with 400 IPX_INVALID_IMAGE, and that SVG passthrough is still sanitized by sanitizeSVGPlugin. The HTML rejection follows from the root-element check, which requires the first root element to be svg. I did not find any change unrelated to #287. Limitation: the line-level src/ipx.ts diff could not be read, so these retained guards are supported by the PR description and the reported tests, not by direct code inspection.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: defaults
Review profile: CHILL
Plan: Advanced
Run ID: 3092db23-02a7-4ba4-b3d0-be3c94f21355
Commits
Reviewing files that changed from the base of the PR and between c573e7f and 9862637.
Files selected for processing: 4
src/ipx.ts
src/svg.ts
test/index.test.ts
test/svg.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #287
Summary
<svg>detector (isSVGInputinsrc/svg.ts) so SVGs withoutwidth,height, orviewBoxattributes on the root<svg>element (e.g.,<symbol viewBox="...">+<use>icon files or bare<svg>) are recognized whenimage-metarejects them for missing dimensions.allowedInputFormatsis still checked before touching SVGO/sharp, non-SVG HTML documents (<html><body><svg></svg></body></html>) are still rejected with400 IPX_INVALID_IMAGE, and SVG passthrough remains sanitized bysanitizeSVGPlugin.imageMeta, the Sharp metadata probe resolves width/height for downstream dimension clamping while preserving the existingmultiPageheight limit guard.Verification
pnpm build(1440ms)pnpm lint(eslint . && prettier -c src test scripts automd.config.ts) — passedpnpm test:types(tsc --noEmit) — passedpnpm test(vitest run --coverage) — 9 test files passed, 609 tests passed (10 pre-existing skips on Windows inserver.test.ts,fs-dirs.test.ts,unstorage.test.ts), including new unit & integration tests intest/svg.test.tsandtest/index.test.tsSummary by CodeRabbit