mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
SVG: Add dompurify preprocessor step (#62143)
Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
18
public/app/core/components/SVG/SanitizedSVG.tsx
Normal file
18
public/app/core/components/SVG/SanitizedSVG.tsx
Normal file
@@ -0,0 +1,18 @@
|
||||
import * as DOMPurify from 'dompurify';
|
||||
import React from 'react';
|
||||
import SVG, { Props } from 'react-inlinesvg';
|
||||
|
||||
export const SanitizedSVG = (props: Props) => {
|
||||
return <SVG {...props} cacheRequests={true} preProcessor={getCleanSVG} />;
|
||||
};
|
||||
|
||||
let cache = new Map<string, string>();
|
||||
|
||||
function getCleanSVG(code: string): string {
|
||||
let clean = cache.get(code);
|
||||
if (!clean) {
|
||||
clean = DOMPurify.sanitize(code, { USE_PROFILES: { svg: true, svgFilters: true } });
|
||||
cache.set(code, clean);
|
||||
}
|
||||
return clean;
|
||||
}
|
||||
Reference in New Issue
Block a user