mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## 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.
29 lines
1.0 KiB
TypeScript
29 lines
1.0 KiB
TypeScript
export const SENSITIVE_DATA_MARKER = '[SENSITIVE]'
|
|
|
|
export const isSensitiveComment = (comment: string | null | undefined): boolean => {
|
|
if (!comment) return false
|
|
const normalized = comment.trim()
|
|
return normalized.startsWith(SENSITIVE_DATA_MARKER) || normalized.endsWith(SENSITIVE_DATA_MARKER)
|
|
}
|
|
|
|
export const decodeSensitiveComment = (comment: string | null | undefined): string | null => {
|
|
if (!comment) return null
|
|
let stripped = comment.trim()
|
|
if (stripped.startsWith(SENSITIVE_DATA_MARKER)) {
|
|
stripped = stripped.slice(SENSITIVE_DATA_MARKER.length).replace(/^ /, '')
|
|
} else if (stripped.endsWith(SENSITIVE_DATA_MARKER)) {
|
|
stripped = stripped.slice(0, -SENSITIVE_DATA_MARKER.length).replace(/ $/, '')
|
|
}
|
|
return stripped || null
|
|
}
|
|
|
|
export const encodeSensitiveComment = (
|
|
comment: string | null | undefined,
|
|
isSensitive: boolean
|
|
): string | null => {
|
|
const base = comment ?? ''
|
|
if (isSensitive && base) return `${SENSITIVE_DATA_MARKER} ${base}`
|
|
if (isSensitive) return SENSITIVE_DATA_MARKER
|
|
return base || null
|
|
}
|