Files
supabase/apps/studio/components/interfaces/Reports/Reports.utils.test.ts
Jordi EnricandClaude Opus 4.8 faa6d46e8b fix(reports): guard against malformed URI in route renderer (#46729)
## Problem

The PostgREST observability page crashes for some projects with
`URIError: URI malformed`. The route renderer calls `decodeURIComponent`
directly on the request query string, which is user-controlled. A
malformed percent-sequence (for example a literal `%` in
`?discount=100%`) makes `decodeURIComponent` throw during render, taking
down the whole page via the global error boundary.

Tracked in Sentry issue 7536581822.

## Fix

Add a `safeDecodeURIComponent` helper that wraps `decodeURIComponent` in
a try/catch and falls back to the raw string on failure. Use it in the
route renderer. The sibling `queryParamsToObject` call is unaffected
since `URLSearchParams` already tolerates malformed escapes.

## How to test

- Open a project's PostgREST observability report
(`/project/[ref]/observability/postgrest`).
- Ensure a request with a malformed query string (e.g. a path containing
a bare `%`) appears in the data.
- Expected result: the row renders with the raw search string instead of
crashing the page.
- Unit tests for `safeDecodeURIComponent` cover valid decode, malformed
input, and empty string.

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 11:11:32 +00:00

60 lines
1.9 KiB
TypeScript

import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
import { describe, expect, it } from 'vitest'
import { formatTimestamp, safeDecodeURIComponent } from './Reports.utils'
dayjs.extend(utc)
describe('safeDecodeURIComponent', () => {
it('decodes a valid percent-encoded string', () => {
expect(safeDecodeURIComponent('a%20b')).toBe('a b')
})
it('returns the original string when percent-encoding is malformed', () => {
expect(safeDecodeURIComponent('?discount=100%')).toBe('?discount=100%')
})
it('handles an empty string', () => {
expect(safeDecodeURIComponent('')).toBe('')
})
})
describe('formatTimestamp', () => {
it('formats milliseconds timestamp correctly', () => {
const timestamp = 1640995200000 // 2022-01-01 00:00:00 UTC in milliseconds
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Jan 1, 12:00am')
})
it('formats microseconds timestamp correctly', () => {
const timestamp = 1640995200000000 // 2022-01-01 00:00:00 UTC in microseconds
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Jan 1, 12:00am')
})
it('formats seconds timestamp correctly', () => {
const timestamp = 1640995200 // 2022-01-01 00:00:00 UTC in seconds
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Jan 1, 12:00am')
})
it('handles string timestamp input', () => {
const timestamp = '1640995200000'
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Jan 1, 12:00am')
})
it('handles invalid string timestamp', () => {
const timestamp = 'invalid-timestamp'
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Invalid Date')
})
it('handles zero timestamp', () => {
const timestamp = 0
const result = formatTimestamp(timestamp, { returnUtc: true })
expect(result).toBe('Jan 1, 12:00am')
})
})