Skip to content

SVGO crashes on a file where the <svg> element is missing both width/height and viewBox attributes #2219

@homatyano

Description

@homatyano

The issue

If the source SVG file's element is missing both the width/height and the viewBox attributes, SVGO then zeroes the image's width and height. But these zeroes are numbers and not strings, so when cleanupAttrs() tries to use .replace() on them, it crashes.

To Reproduce

Use the cleanupAttrs() filter on the following file:

<svg xmlns="http://www.w3.org/2000/svg">
    <rect width="10" height="10"/>
</svg>

Expected behavior

SVGO gracefully handles the missing values.

Observed behavior

SVGO crashes with the following message:

TypeError: node.attributes[name].replace is not a function
    at Object.enter (file:///opt/homebrew/Cellar/svgo/4.0.1/libexec/lib/node_modules/svgo/plugins/cleanupAttrs.js:30:59)
    at visit (file:///opt/homebrew/Cellar/svgo/4.0.1/libexec/lib/node_modules/svgo/lib/util/visit.js:12:30)
    at visit (file:///opt/homebrew/Cellar/svgo/4.0.1/libexec/lib/node_modules/svgo/lib/util/visit.js:21:7)
    at invokePlugins (file:///opt/homebrew/Cellar/svgo/4.0.1/libexec/lib/node_modules/svgo/lib/svgo/plugins.js:30:7)
    at Module.optimize (file:///opt/homebrew/Cellar/svgo/4.0.1/libexec/lib/node_modules/svgo/lib/svgo.js:119:5)
    at optimize (file:///opt/homebrew/Cellar/svgo/4.0.1/libexec/lib/node_modules/svgo/lib/svgo-node.js:90:15)
    at processSVGData (file:///opt/homebrew/Cellar/svgo/4.0.1/libexec/lib/node_modules/svgo/lib/svgo/coa.js:400:14)
    at file:///opt/homebrew/Cellar/svgo/4.0.1/libexec/lib/node_modules/svgo/lib/svgo/coa.js:379:15
    at async Promise.all (index 0)
    at async Command.action (file:///opt/homebrew/Cellar/svgo/4.0.1/libexec/lib/node_modules/svgo/lib/svgo/coa.js:264:7)

Desktop

  • SVGO Version: 4.0.1
  • Node.js Version: v24.15.0
  • OS: MacOS Tahoe 26.4.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions