SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
fix: add safeguard for large log body entries (#8560)
Aditya Singhb40fda02cfb7
diff --git a/frontend/src/components/LogDetail/index.tsx b/frontend/src/components/LogDetail/index.tsxindex af4ba32177..ed9a025c05 100644--- a/frontend/src/components/LogDetail/index.tsx+++ b/frontend/src/components/LogDetail/index.tsx@@ -2,7 +2,6 @@import './LogDetails.styles.scss';import { Color, Spacing } from '@signozhq/design-tokens';-import Convert from 'ansi-to-html';import { Button, Divider, Drawer, Radio, Tooltip, Typography } from 'antd';import { RadioChangeEvent } from 'antd/lib';import cx from 'classnames';@@ -17,12 +16,10 @@ import JSONView from 'container/LogDetailedView/JsonView';import Overview from 'container/LogDetailedView/Overview';import {aggregateAttributesResourcesToString,- escapeHtml,+ getSanitizedLogBody,removeEscapeCharacters,- unescapeString,} from 'container/LogDetailedView/utils';import { useOptionsMenu } from 'container/OptionsMenu';-import dompurify from 'dompurify';import { useQueryBuilder } from 'hooks/queryBuilder/useQueryBuilder';import { useIsDarkMode } from 'hooks/useDarkMode';import { useNotifications } from 'hooks/useNotifications';@@ -46,14 +43,11 @@ import { AppState } from 'store/reducers';import { Query, TagFilter } from 'types/api/queryBuilder/queryBuilderData';import { DataSource, StringOperators } from 'types/common/queryBuilder';import { GlobalReducer } from 'types/reducer/globalTime';-import { FORBID_DOM_PURIFY_TAGS } from 'utils/app';import { RESOURCE_KEYS, VIEW_TYPES, VIEWS } from './constants';import { LogDetailProps } from './LogDetail.interfaces';import QueryBuilderSearchWrapper from './QueryBuilderSearchWrapper';-const convert = new Convert();-function LogDetail({log,onClose,@@ -118,11 +112,7 @@ function LogDetail({const htmlBody = useMemo(() => ({- __html: convert.toHtml(- dompurify.sanitize(unescapeString(escapeHtml(log?.body || '')), {- FORBID_TAGS: [...FORBID_DOM_PURIFY_TAGS],- }),- ),+ __html: getSanitizedLogBody(log?.body || '', { shouldEscapeHtml: true }),}),[log?.body],);diff --git a/frontend/src/components/Logs/ListLogView/index.tsx b/frontend/src/components/Logs/ListLogView/index.tsxindex 8526ce0be8..bdc8b2e77f 100644--- a/frontend/src/components/Logs/ListLogView/index.tsx+++ b/frontend/src/components/Logs/ListLogView/index.tsx@@ -1,15 +1,13 @@import './ListLogView.styles.scss';import { blue } from '@ant-design/colors';-import Convert from 'ansi-to-html';import { Typography } from 'antd';import cx from 'classnames';import LogDetail from 'components/LogDetail';import { VIEW_TYPES } from 'components/LogDetail/constants';import { DATE_TIME_FORMATS } from 'constants/dateTimeFormats';-import { escapeHtml, unescapeString } from 'container/LogDetailedView/utils';+import { getSanitizedLogBody } from 'container/LogDetailedView/utils';import { FontSize } from 'container/OptionsMenu/types';-import dompurify from 'dompurify';import { useActiveLog } from 'hooks/logs/useActiveLog';import { useCopyLogLink } from 'hooks/logs/useCopyLogLink';import { useIsDarkMode } from 'hooks/useDarkMode';@@ -20,7 +18,6 @@ import { useCallback, useMemo, useState } from 'react';// interfacesimport { IField } from 'types/api/logs/fields';import { ILog } from 'types/api/logs/log';-import { FORBID_DOM_PURIFY_TAGS } from 'utils/app';// componentsimport AddToQueryHOC, { AddToQueryHOCProps } from '../AddToQueryHOC';@@ -37,8 +34,6 @@ import {} from './styles';import { isValidLogField } from './util';-const convert = new Convert();-interface LogFieldProps {fieldKey: string;fieldValue: string;@@ -57,11 +52,7 @@ function LogGeneralField({}: LogFieldProps): JSX.Element {const html = useMemo(() => ({- __html: convert.toHtml(- dompurify.sanitize(unescapeString(escapeHtml(fieldValue)), {- FORBID_TAGS: [...FORBID_DOM_PURIFY_TAGS],- }),- ),+ __html: getSanitizedLogBody(fieldValue, { shouldEscapeHtml: true }),}),[fieldValue],);diff --git a/frontend/src/components/Logs/RawLogView/index.tsx b/frontend/src/components/Logs/RawLogView/index.tsxindex 9a305904a6..4ad7329f83 100644--- a/frontend/src/components/Logs/RawLogView/index.tsx+++ b/frontend/src/components/Logs/RawLogView/index.tsx@@ -1,13 +1,11 @@import './RawLogView.styles.scss';-import Convert from 'ansi-to-html';import { DrawerProps } from 'antd';import LogDetail from 'components/LogDetail';import { VIEW_TYPES, VIEWS } from 'components/LogDetail/constants';import { DATE_TIME_FORMATS } from 'constants/dateTimeFormats';-import { escapeHtml, unescapeString } from 'container/LogDetailedView/utils';+import { getSanitizedLogBody } from 'container/LogDetailedView/utils';import LogsExplorerContext from 'container/LogsExplorerContext';-import dompurify from 'dompurify';import { useActiveLog } from 'hooks/logs/useActiveLog';import { useCopyLogLink } from 'hooks/logs/useCopyLogLink';// hooks@@ -23,7 +21,6 @@ import {useMemo,useState,} from 'react';-import { FORBID_DOM_PURIFY_TAGS } from 'utils/app';import LogLinesActionButtons from '../LogLinesActionButtons/LogLinesActionButtons';import LogStateIndicator from '../LogStateIndicator/LogStateIndicator';@@ -32,8 +29,6 @@ import { getLogIndicatorType } from '../LogStateIndicator/utils';import { RawLogContent, RawLogViewContainer } from './styles';import { RawLogViewProps } from './types';-const convert = new Convert();-function RawLogView({isActiveLog,isReadOnly,@@ -176,11 +171,7 @@ function RawLogView({const html = useMemo(() => ({- __html: convert.toHtml(- dompurify.sanitize(unescapeString(escapeHtml(text)), {- FORBID_TAGS: [...FORBID_DOM_PURIFY_TAGS],- }),- ),+ __html: getSanitizedLogBody(text, { shouldEscapeHtml: true }),}),[text],);diff --git a/frontend/src/components/Logs/TableView/useTableView.tsx b/frontend/src/components/Logs/TableView/useTableView.tsxindex 8b97f6cfd2..0551ff479f 100644--- a/frontend/src/components/Logs/TableView/useTableView.tsx+++ b/frontend/src/components/Logs/TableView/useTableView.tsx@@ -1,17 +1,14 @@import './useTableView.styles.scss';-import Convert from 'ansi-to-html';import { Typography } from 'antd';import { ColumnsType } from 'antd/es/table';import cx from 'classnames';import { DATE_TIME_FORMATS } from 'constants/dateTimeFormats';-import { unescapeString } from 'container/LogDetailedView/utils';-import dompurify from 'dompurify';+import { getSanitizedLogBody } from 'container/LogDetailedView/utils';import { useIsDarkMode } from 'hooks/useDarkMode';import { FlatLogData } from 'lib/logs/flatLogData';import { useTimezone } from 'providers/Timezone';import { useMemo } from 'react';-import { FORBID_DOM_PURIFY_TAGS } from 'utils/app';import LogStateIndicator from '../LogStateIndicator/LogStateIndicator';import { getLogIndicatorTypeForTable } from '../LogStateIndicator/utils';@@ -27,8 +24,6 @@ import {UseTableViewResult,} from './types';-const convert = new Convert();-export const useTableView = (props: UseTableViewProps): UseTableViewResult => {const {logs,@@ -149,11 +144,7 @@ export const useTableView = (props: UseTableViewProps): UseTableViewResult => {children: (<TableBodyContentdangerouslySetInnerHTML={{- __html: convert.toHtml(- dompurify.sanitize(unescapeString(field as string), {- FORBID_TAGS: [...FORBID_DOM_PURIFY_TAGS],- }),- ),+ __html: getSanitizedLogBody(field as string),}}fontSize={fontSize}linesPerRow={linesPerRow}diff --git a/frontend/src/container/LogDetailedView/TableView/TableViewActions.tsx b/frontend/src/container/LogDetailedView/TableView/TableViewActions.tsxindex 3c4a102d88..4401f59ae0 100644--- a/frontend/src/container/LogDetailedView/TableView/TableViewActions.tsx+++ b/frontend/src/container/LogDetailedView/TableView/TableViewActions.tsx@@ -2,7 +2,6 @@import './TableViewActions.styles.scss';import { Color } from '@signozhq/design-tokens';-import Convert from 'ansi-to-html';import { Button, Popover, Spin, Tooltip, Tree } from 'antd';import GroupByIcon from 'assets/CustomIcons/GroupByIcon';import cx from 'classnames';@@ -12,22 +11,19 @@ import { OPERATORS } from 'constants/queryBuilder';import ROUTES from 'constants/routes';import { RESTRICTED_SELECTED_FIELDS } from 'container/LogsFilters/config';import { MetricsType } from 'container/MetricsApplication/constant';-import dompurify from 'dompurify';import { ArrowDownToDot, ArrowUpFromDot, Ellipsis } from 'lucide-react';import { useTimezone } from 'providers/Timezone';import React, { useCallback, useMemo, useState } from 'react';import { useLocation } from 'react-router-dom';import { DataTypes } from 'types/api/queryBuilder/queryAutocompleteResponse';-import { FORBID_DOM_PURIFY_TAGS } from 'utils/app';import { DataType } from '../TableView';import {- escapeHtml,filterKeyForField,getFieldAttributes,+ getSanitizedLogBody,parseFieldValue,removeEscapeCharacters,- unescapeString,} from '../utils';import useAsyncJSONProcessing from './useAsyncJSONProcessing';@@ -51,8 +47,6 @@ interface ITableViewActionsProps {) => () => void;}-const convert = new Convert();-// Memoized Tree Componentconst MemoizedTree = React.memo<{ treeData: any[] }>(({ treeData }) => (<Tree@@ -146,11 +140,7 @@ export default function TableViewActions(if (record.field !== 'body') return { __html: '' };return {- __html: convert.toHtml(- dompurify.sanitize(unescapeString(escapeHtml(record.value)), {- FORBID_TAGS: [...FORBID_DOM_PURIFY_TAGS],- }),- ),+ __html: getSanitizedLogBody(record.value, { shouldEscapeHtml: true }),};}, [record.field, record.value]);diff --git a/frontend/src/container/LogDetailedView/TableView/useAsyncJSONProcessing.ts b/frontend/src/container/LogDetailedView/TableView/useAsyncJSONProcessing.tsindex d7403a4796..bf5371edac 100644--- a/frontend/src/container/LogDetailedView/TableView/useAsyncJSONProcessing.ts+++ b/frontend/src/container/LogDetailedView/TableView/useAsyncJSONProcessing.ts@@ -4,6 +4,8 @@ import { useEffect, useRef, useState } from 'react';import { jsonToDataNodes, recursiveParseJSON } from '../utils';+const MAX_BODY_BYTES = 100 * 1024; // 100 KB+// Hook for async JSON processingconst useAsyncJSONProcessing = (value: string,@@ -31,6 +33,12 @@ const useAsyncJSONProcessing = (return (): void => {};}+ // Avoid processing if the json is too large+ const byteSize = new Blob([value]).size;+ if (byteSize > MAX_BODY_BYTES) {+ return (): void => {};+ }+processingRef.current = true;setJsonState({ isLoading: true, treeData: null, error: null });diff --git a/frontend/src/container/LogDetailedView/util.test.ts b/frontend/src/container/LogDetailedView/util.test.tsindex d5918f2bca..f2131c253b 100644--- a/frontend/src/container/LogDetailedView/util.test.ts+++ b/frontend/src/container/LogDetailedView/util.test.ts@@ -1,6 +1,11 @@import { DataTypes } from 'types/api/queryBuilder/queryAutocompleteResponse';-import { flattenObject, getDataTypes, recursiveParseJSON } from './utils';+import {+ flattenObject,+ getDataTypes,+ getSanitizedLogBody,+ recursiveParseJSON,+} from './utils';describe('recursiveParseJSON', () => {it('should return an empty object if the input is not valid JSON', () => {@@ -185,3 +190,146 @@ describe('Get Data Types utils', () => {expect(getDataTypes([2.5, 3, 1])).toBe(DataTypes.ArrayFloat64);});});++describe('getSanitizedLogBody', () => {+ it('should return sanitized HTML with default options (shouldEscapeHtml: false)', () => {+ const input = '<script>alert("xss")</script>Hello World';+ const result = getSanitizedLogBody(input);++ // Should remove script tags and return sanitized HTML+ expect(result).not.toContain('<script>');+ expect(result).toContain('Hello World');+ });++ it('should escape HTML when shouldEscapeHtml is true', () => {+ const input = '<script>alert("xss")</script>Hello World';+ const result = getSanitizedLogBody(input, { shouldEscapeHtml: true });++ // Should escape HTML entities+ expect(result).toContain('<script>');+ expect(result).toContain('</script>');+ expect(result).toContain('Hello World');+ });++ it('should handle ANSI color codes correctly', () => {+ const input = '\x1b[32mHello\x1b[0m World';+ const result = getSanitizedLogBody(input);++ // Should convert ANSI codes to HTML spans+ expect(result).toContain('<span');+ expect(result).toContain('Hello');+ expect(result).toContain('World');+ });++ it('should handle unescaped strings correctly', () => {+ const input = 'Hello\\nWorld\\tTab';+ const result = getSanitizedLogBody(input);++ // Should unescape the string+ expect(result).toContain('Hello');+ expect(result).toContain('World');+ });++ it('should handle empty string input', () => {+ const result = getSanitizedLogBody('');+ expect(result).toBe('');+ });++ it('should handle null/undefined input gracefully', () => {+ const result1 = getSanitizedLogBody(null as any);+ const result2 = getSanitizedLogBody(undefined as any);++ expect(result1).toBe('');+ expect(result2).toBe('');+ });++ it('should handle special characters and entities', () => {+ const input = '& < > " \' & < >';+ const result = getSanitizedLogBody(input, { shouldEscapeHtml: true });++ // Should escape HTML entities+ expect(result).toContain('&');+ expect(result).toContain('<');+ expect(result).toContain('>');+ expect(result).toContain('"');+ });++ it('should handle complex HTML with mixed content', () => {+ const input =+ '<div><p>Hello <strong>World</strong></p><script>alert("xss")</script></div>';+ const result = getSanitizedLogBody(input);++ // Should keep safe HTML but remove script tags+ expect(result).toContain('<div>');+ expect(result).toContain('<p>');+ expect(result).toContain('<strong>');+ expect(result).toContain('Hello');+ expect(result).toContain('World');+ expect(result).not.toContain('<script>');+ });++ it('should handle JSON-like strings', () => {+ const input = '{"key": "value", "nested": {"inner": "data"}}';+ const result = getSanitizedLogBody(input);++ // Should preserve JSON structure+ expect(result).toContain('{');+ expect(result).toContain('}');+ expect(result).toContain('key');+ expect(result).toContain('value');+ });++ it('should handle URLs and links', () => {+ const input = 'Visit https://example.com for more info';+ const result = getSanitizedLogBody(input);++ // Should preserve the URL text+ expect(result).toContain('https://example.com');+ expect(result).toContain('Visit');+ expect(result).toContain('info');+ });++ it('should handle error cases and return fallback', () => {+ // Mock console.error to avoid noise in tests+ const originalConsoleError = console.error;+ console.error = jest.fn();++ // Create a scenario that might cause an error+ const input = 'Normal text';+ const result = getSanitizedLogBody(input);++ // Should return the processed text normally+ expect(result).toContain('Normal text');++ // Restore console.error+ console.error = originalConsoleError;+ });++ it('should handle different escape scenarios correctly', () => {+ const input1 = '<div>Hello</div>';+ const result1 = getSanitizedLogBody(input1, { shouldEscapeHtml: false });+ const result2 = getSanitizedLogBody(input1, { shouldEscapeHtml: true });++ // Without escaping, should keep HTML structure+ expect(result1).toContain('<div>');+ expect(result1).toContain('</div>');++ // With escaping, should escape HTML entities+ expect(result2).toContain('<div>');+ expect(result2).toContain('</div>');+ });++ it('should handle ANSI codes with HTML escaping', () => {+ const input = '\x1b[32mHello\x1b[0m <script>World</script>';+ const result = getSanitizedLogBody(input, { shouldEscapeHtml: true });++ // Should handle both ANSI codes and HTML escaping+ expect(result).toContain('<span');+ expect(result).toContain('Hello');+ expect(result).toContain('<script>');+ expect(result).toContain('World');+ expect(result).toContain('</script>');+ });+});++//diff --git a/frontend/src/container/LogDetailedView/utils.tsx b/frontend/src/container/LogDetailedView/utils.tsxindex 6f04bd2ca1..97c13474f9 100644--- a/frontend/src/container/LogDetailedView/utils.tsx+++ b/frontend/src/container/LogDetailedView/utils.tsx@@ -1,13 +1,18 @@+import Convert from 'ansi-to-html';import { DataNode } from 'antd/es/tree';import { MetricsType } from 'container/MetricsApplication/constant';+import dompurify from 'dompurify';import { uniqueId } from 'lodash-es';import { ILog, ILogAggregateAttributesResources } from 'types/api/logs/log';import { DataTypes } from 'types/api/queryBuilder/queryAutocompleteResponse';+import { FORBID_DOM_PURIFY_TAGS } from 'utils/app';import BodyTitleRenderer from './BodyTitleRenderer';import { typeToArrayTypeMapper } from './config';import { AnyObject, IFieldAttributes } from './LogDetailedView.types';+const convertInstance = new Convert();+export const recursiveParseJSON = (obj: string): Record<string, unknown> => {try {const value = JSON.parse(obj);@@ -336,3 +341,21 @@ export function findKeyPath(});return finalPath;}++export const getSanitizedLogBody = (+ text: string,+ options: { shouldEscapeHtml?: boolean } = {},+): string => {+ const { shouldEscapeHtml = false } = options;+ const escapedText = shouldEscapeHtml ? escapeHtml(text) : text;+ try {+ return convertInstance.toHtml(+ dompurify.sanitize(unescapeString(escapedText), {+ FORBID_TAGS: [...FORBID_DOM_PURIFY_TAGS],+ }),+ );+ } catch (error) {+ console.error('Error sanitizing text', error, text);+ return '{}';+ }+};