[v11.1.x] Frontend: Use safe stringifier in parseBody (#90234)
Frontend: Use safe stringifier in parseBody (#90047)
* Frontend: Use safe stringifier in parseBody
Closes #88064
(cherry picked from commit 434f386982)
This commit is contained in:
@@ -10,3 +10,22 @@
|
||||
export const isEmptyObject = (value: unknown): value is Record<string, never> => {
|
||||
return typeof value === 'object' && value !== null && Object.keys(value).length === 0;
|
||||
};
|
||||
|
||||
/** Stringifies an object that may contain circular references */
|
||||
export function safeStringifyValue(value: unknown) {
|
||||
const getCircularReplacer = () => {
|
||||
const seen = new WeakSet();
|
||||
return (_: string, value: object | null) => {
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
if (seen.has(value)) {
|
||||
return;
|
||||
}
|
||||
seen.add(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
};
|
||||
|
||||
return JSON.stringify(value, getCircularReplacer());
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { omitBy } from 'lodash';
|
||||
|
||||
import { deprecationWarning } from '@grafana/data';
|
||||
import { deprecationWarning, safeStringifyValue } from '@grafana/data';
|
||||
import { BackendSrvRequest } from '@grafana/runtime';
|
||||
|
||||
export const parseInitFromOptions = (options: BackendSrvRequest): RequestInit => {
|
||||
@@ -93,7 +93,7 @@ export const parseBody = (options: BackendSrvRequest, isAppJson: boolean) => {
|
||||
return options.data;
|
||||
}
|
||||
|
||||
return isAppJson ? JSON.stringify(options.data) : new URLSearchParams(options.data);
|
||||
return isAppJson ? safeStringifyValue(options.data) : new URLSearchParams(options.data);
|
||||
};
|
||||
|
||||
export async function parseResponseBody<T>(
|
||||
|
||||
Reference in New Issue
Block a user