Files
supabase/apps/studio/lib/sensitive-column-comment.test.ts
Ayush Gade 9f351563a9 fix(studio): prevent column comment data loss from sensitivity marker (#53051)
## What

Studio stores a column's "sensitive data" flag in-band by adding
`[SENSITIVE]` to the column's comment. The encode/decode helpers matched
the marker **anywhere** in the string, which caused two problems:

* A comment that merely *contains* `[SENSITIVE]` (e.g. `Fields marked
[SENSITIVE] are PII`) was wrongly treated as sensitive.
* The marker was stripped from the **middle** of the user's text on save
- even when the sensitivity toggle was off - silently corrupting
user-authored content.

Discussion: https://github.com/orgs/supabase/discussions/52699

## Changes

Following the direction in the discussion:

1. **One shared helper** - the marker logic was duplicated in three
places (`ColumnEditor.utils.ts`, `state/table-editor-table.tsx`, and the
toggle in `TableEditor/Column.tsx`), each slightly different. These are
consolidated into a single `lib/sensitive-column-comment.ts` with
`isSensitiveComment` / `encodeSensitiveComment` /
`decodeSensitiveComment`.
2. **Prefix-anchored detection and removal** - the marker is only
matched and stripped when it's an exact leading prefix (plus at most one
following space), so a mid-string `[SENSITIVE]` is preserved verbatim.
3. **Standardized the marker position** - `Column.tsx`'s toggle
previously appended the marker to the *end* of the comment, while the
column editor prepended it. The toggle now writes the canonical prefix,
so detection is consistent across all paths.
4. **Backward compatibility** - to avoid losing the flag on columns that
were saved with a *trailing* marker (the old toggle behavior),
`decodeSensitiveComment` also recognizes and strips an existing trailing
marker. Re-saving such a column migrates it to the prefix form.

## Example

```ts
// Before
encodeSensitiveComment('a [SENSITIVE] b', false) // -> 'a  b'  (data loss)

// After
encodeSensitiveComment('a [SENSITIVE] b', false) // -> 'a [SENSITIVE] b'  (preserved)
```

Tests

Added lib/sensitive-column-comment.test.ts covering the encode/decode
round trip, the mid-string case, the bare-marker case, and legacy
trailing-marker migration (16 cases).

Note for reviewers

Point 4 (decoding a legacy trailing marker) is the one behavior I'd like
a sanity check on.... it's there so columns already flagged via the old
grid toggle don't silently lose their flag after this change. Happy to
drop it if you'd prefer to keep decode strictly prefix-only.
2026-11-06 19:24:48 +00:00

108 lines
3.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
decodeSensitiveComment,
encodeSensitiveComment,
isSensitiveComment,
} from './sensitive-column-comment'
describe('isSensitiveComment', () => {
it('detects a leading marker', () => {
expect(isSensitiveComment('[SENSITIVE] secret')).toBe(true)
expect(isSensitiveComment('[SENSITIVE]')).toBe(true)
})
it('detects a legacy trailing marker', () => {
expect(isSensitiveComment('secret [SENSITIVE]')).toBe(true)
})
it('detects a marker despite surrounding whitespace', () => {
expect(isSensitiveComment(' [SENSITIVE] secret')).toBe(true)
expect(isSensitiveComment('email [SENSITIVE] ')).toBe(true)
})
it('ignores a marker in the middle of the comment', () => {
expect(isSensitiveComment('Fields marked [SENSITIVE] are PII')).toBe(false)
})
it('returns false for empty input', () => {
expect(isSensitiveComment('')).toBe(false)
expect(isSensitiveComment(null)).toBe(false)
expect(isSensitiveComment(undefined)).toBe(false)
})
})
describe('decodeSensitiveComment', () => {
it('strips a leading marker and one following space', () => {
expect(decodeSensitiveComment('[SENSITIVE] secret')).toBe('secret')
})
it('strips a legacy trailing marker and one preceding space', () => {
expect(decodeSensitiveComment('secret [SENSITIVE]')).toBe('secret')
})
it('returns null for a bare marker', () => {
expect(decodeSensitiveComment('[SENSITIVE]')).toBeNull()
})
it('preserves a mid-string marker verbatim', () => {
expect(decodeSensitiveComment('Fields marked [SENSITIVE] are PII')).toBe(
'Fields marked [SENSITIVE] are PII'
)
})
it('returns null for empty input', () => {
expect(decodeSensitiveComment('')).toBeNull()
expect(decodeSensitiveComment(null)).toBeNull()
})
})
describe('encodeSensitiveComment', () => {
it('prepends the marker when sensitive', () => {
expect(encodeSensitiveComment('hello', true)).toBe('[SENSITIVE] hello')
})
it('stores a bare marker when sensitive with no comment', () => {
expect(encodeSensitiveComment('', true)).toBe('[SENSITIVE]')
expect(encodeSensitiveComment(null, true)).toBe('[SENSITIVE]')
})
it('leaves a non-sensitive comment untouched', () => {
expect(encodeSensitiveComment('hello', false)).toBe('hello')
})
it('returns null for an empty non-sensitive comment', () => {
expect(encodeSensitiveComment('', false)).toBeNull()
})
it('does not mangle a mid-string marker when not sensitive', () => {
expect(encodeSensitiveComment('a [SENSITIVE] b', false)).toBe('a [SENSITIVE] b')
})
it('preserves decoded text that itself starts with the marker', () => {
expect(encodeSensitiveComment('[SENSITIVE] secret', true)).toBe(
'[SENSITIVE] [SENSITIVE] secret'
)
})
})
describe('decode then encode round trip', () => {
it('migrates a legacy trailing marker to a prefix', () => {
expect(encodeSensitiveComment(decodeSensitiveComment('secret [SENSITIVE]'), true)).toBe(
'[SENSITIVE] secret'
)
})
it('migrates a legacy trailing marker with trailing whitespace', () => {
expect(encodeSensitiveComment(decodeSensitiveComment('email [SENSITIVE] '), true)).toBe(
'[SENSITIVE] email'
)
})
it('does not double-prefix an already-encoded comment', () => {
expect(encodeSensitiveComment(decodeSensitiveComment('[SENSITIVE] secret'), true)).toBe(
'[SENSITIVE] secret'
)
})
})