Safe SVG

Mô tả

Safe SVG cho phép bạn tải file SVG lên website!

Plugin cho phép bạn file SVG lên website đồng thời đảm bảo chúng được làm sạch để ngăn chặn các lỗ hổng bảo mật SVG/XML ảnh hưởng đến web bạn. Ngoài ra, nó còn cho phép bạn xem trước các fileSVG đã tải lên trong thư viện media ở mọi chế độ xem.

Tính năng hiện có

  • Sanitised SVG (Làm sạch SVG) – Loại bỏ các lỗ hổng bảo mật trong file SVG trước khi tải lên website.
  • Tối ưu SVGO – Tải SVG thông qua công cụ SVGO để tiết kiệm dung lượng. Mặc định tính năng này đã bị tắt nhưng bạn có thể bật nó thông qua code sau: add_filter( 'safe_svg_optimizer_enabled', '__return_true' );
  • Xem SVG trong Thư viện – Bạn có thể xem trực tiếp file SVG trong Thư viện như khi xem ảnh.
  • Cấp quyền tải lên – Có thể cấp quyền tải file SVG cho những tài khoản nhất định trên web.

Plugin được phát hành để giải quyết vấn đề #24251.

SVG Sanitization bằng thư viện: https://github.com/darylldoyle/svg-sanitizer.

Tối ưu dung lượng SVG bằng thư viện: https://github.com/svg/svgo.

Technical: Upload Path Security

WordPress’s _wp_handle_upload( $file, $action ) function allows any $action value, which determines the filter hook name: {$action}_prefilter. Safe SVG hooks common actions like wp_handle_upload and wp_handle_sideload, but cannot hook arbitrary custom actions defined by third-party code. Since upload actions are unbounded and MIME allowances are global, we cannot guarantee sanitization coverage across all possible upload paths.

Khối

Plugin này cung cấp 1 khối.

  • Safe SVG Display the SVG icon

Cài đặt

Cài đặt qua thư mục WordPress hoặc tải xuống, giải nén và tải các tệp lên thư mục /wp-content/plugins/.

Hỏi đáp

Chúng ta có thể thay đổi các attribute và tag được phép (allowed attribute/allowed tag) không?

Tất nhiên, bạn có thể thực hiện thông qua filter svg_allowed_attributes và svg_allowed_tags filters.
Chúng nhận một đối số (argument) và phải trả về đối số (return) đó. Xem các ví dụ bên dưới:

add_filter( 'svg_allowed_attributes', function ( $attributes ) {

    // Do what you want here...

    // This should return an array so add your attributes to
    // to the $attributes array before returning it. E.G.

    $attributes[] = 'target'; // This would allow the target="" attribute.

    return $attributes;
} );


add_filter( 'svg_allowed_tags', function ( $tags ) {

    // Do what you want here...

    // This should return an array so add your tags to
    // to the $tags array before returning it. E.G.

    $tags[] = 'use'; // This would allow the <use> element.

    return $tags;
} );

Can my theme style an inline SVG?

Mostly, yes. The Inline SVG block renders an SVG that carries its own <style> element inside a shadow root, because CSS inside an inline SVG is otherwise applied to the whole page rather than just the SVG. Stylesheets cannot reach into a shadow root, so theme CSS such as .entry-content svg { fill: red; } will not apply to those SVGs.

Inherited properties still cross the boundary, so setting color on an ancestor and using currentColor inside the SVG works, as do CSS custom properties. SVGs that do not contain a <style> element are rendered without the shadow root and can be styled by theme stylesheets.

To turn isolation off, at the cost of allowing an SVG’s CSS to affect the rest of the page:

add_filter( 'safe_svg_inline_use_shadow_dom', '__return_false' );

Why doesn’t Safe SVG globally enable SVG uploads?

Safe SVG only allows SVGs through upload paths it can actively sanitize. While most WordPress uploads use standard functions like wp_handle_upload() (which Safe SVG hooks), plugins and themes can create custom upload paths by calling WordPress’s underlying _wp_handle_upload() function with arbitrary action parameters.

Globally enabling the image/svg+xml MIME type would allow SVGs through all upload paths—including custom ones Safe SVG cannot intercept and sanitize. This would create security vulnerabilities where unsanitized SVGs containing malicious scripts could be uploaded.

This is a deliberate design decision: Safe SVG prioritizes guaranteed sanitization over broad compatibility. SVGs are only allowed when we can ensure they’re safe.

Where do I report security bugs found in this plugin?

Please report security bugs found in the source code of the Safe SVG plugin through the Patchstack Vulnerability Disclosure  Program. The Patchstack team will assist you with verification, CVE assignment, and notify the developers of this plugin.

Đánh giá

6 Tháng 8, 2026 1 trả lời
Must use plugin, well maintained, installing almost every time and just now I've realized, that I didn't review it. Keep it up and thanks!
11 Tháng 3, 2026 1 trả lời
Needed SVG upload support, and this plugin did the job. Very lightweight and easy to use. No issues so far. Some additional settings would be nice, but overall, it's quite solid.
21 Tháng 6, 2025 1 trả lời
Would have given a 5 star, but it seems support is missing for the taxonomy / terms section (like in categories) upload for SVG images. Keep getting an error that the upload isn't supported. Hopefully this will be fixed in a future update. Will update once this is added. Cheers!
30 Tháng 4, 2025 1 trả lời
Great plugin! very usefull, but please can you add the possibility to add an inline SVG on the block pasting svg code? Thanks!
Đọc tất cả 79 đánh giá

Người đóng góp & Lập trình viên

“Safe SVG” là mã nguồn mở. Những người sau đã đóng góp vào plugin này.

Những người đóng góp

“Safe SVG” đã được dịch qua 32 ngôn ngữ. Cảm ơn những người tham gia dịch vì đóng góp của họ.

Dịch “Safe SVG” sang ngôn ngữ của bạn.

Muốn tham gia phát triển?

Duyệt code, check out SVN repository, hoặc theo dõi nhật ký phát triển qua RSS.

Nhật ký thay đổi

2.5.1 – 2026-09-22

2.5.0 – 2026-09-07

2.4.0 – 2025-09-22

Xem chi tiết lịch sử thay đổi (changelog) tại đây.