Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export function convertRelayPiiConfig(relayPiiConfig?: string | null): Rule[] {
source,
placeholder: redaction?.text,
pattern: resolvedRule.pattern,
replaceCaptured:
resolvedRule.replaceGroups?.length === 1 &&
resolvedRule.replaceGroups?.at(0) === 1,
});
} else {
convertedRules.push({
Expand All @@ -75,6 +78,9 @@ export function convertRelayPiiConfig(relayPiiConfig?: string | null): Rule[] {
type: RuleType.PATTERN,
source,
pattern: resolvedRule.pattern,
replaceCaptured:
resolvedRule.replaceGroups?.length === 1 &&
resolvedRule.replaceGroups?.at(0) === 1,
});
} else {
convertedRules.push({id, method, type, source});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ describe('Edit Modal', () => {
method: 'mask',
pattern: '',
placeholder: '',
replaceCaptured: false,
type: 'anything',
source: valueSuggestions[2]!.value,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import {css} from '@emotion/react';
import styled from '@emotion/styled';
import sortBy from 'lodash/sortBy';

import {Flex} from '@sentry/scraps/layout';
import {Tooltip} from '@sentry/scraps/tooltip';

import {Alert} from 'sentry/components/core/alert';
import {Button} from 'sentry/components/core/button';
import {Checkbox} from 'sentry/components/core/checkbox';
import {Input} from 'sentry/components/core/input';
import FieldGroup from 'sentry/components/forms/fieldGroup';
import RadioField from 'sentry/components/forms/fields/radioField';
Expand All @@ -14,6 +18,7 @@ import {space} from 'sentry/styles/space';
import type {Organization} from 'sentry/types/organization';
import type {Project} from 'sentry/types/project';
import withOrganization from 'sentry/utils/withOrganization';
import {hasCaptureGroups} from 'sentry/views/settings/components/dataScrubbing/modals/utils';
import {
AllowedDataScrubbingDatasets,
MethodType,
Expand Down Expand Up @@ -46,7 +51,7 @@ type Props<V extends Values, K extends keyof V> = {
errors: Partial<V>;
eventId: EventId;
onAttributeError: (message: string) => void;
onChange: (field: K, value: string) => void;
onChange: (field: K, value: V[K]) => void;
onChangeDataset: (dataset: AllowedDataScrubbingDatasets) => void;
onUpdateEventId: (eventId: string) => void;
onValidate: (field: K) => () => void;
Expand All @@ -60,6 +65,35 @@ type State = {
displayEventId: boolean;
};

function ReplaceCapturedCheckbox({
values,
onChange,
}: {
onChange: (field: 'replaceCaptured', value: boolean) => void;
values: Values;
}) {
const disabled = !hasCaptureGroups(values.pattern);
return (
<Tooltip
title={disabled ? t('This rule does not contain capture groups') : undefined}
disabled={!disabled}
>
<Flex gap="xs" align="center">
<Checkbox
id="replace-captured"
name="replaceCaptured"
checked={values.replaceCaptured}
disabled={disabled}
onChange={e => onChange('replaceCaptured', e.target.checked)}
/>
<ReplaceCapturedLabel htmlFor="replace-captured" disabled={disabled}>
{t('Only replace first capture match')}
</ReplaceCapturedLabel>
</Flex>
</Tooltip>
);
}

class Form extends Component<Props<Values, KeysOfUnion<Values>>, State> {
state: State = {
displayEventId: !!this.props.eventId?.value,
Expand All @@ -68,7 +102,7 @@ class Form extends Component<Props<Values, KeysOfUnion<Values>>, State> {
handleChange =
<K extends keyof Values>(field: K) =>
(event: React.ChangeEvent<HTMLInputElement>) => {
this.props.onChange(field, event.target.value);
this.props.onChange(field, event.target.value as Values[K]);
};

handleToggleEventId = () => {
Expand Down Expand Up @@ -217,6 +251,7 @@ class Form extends Component<Props<Values, KeysOfUnion<Values>>, State> {
onBlur={onValidate('pattern')}
id="regex-matches"
/>
<ReplaceCapturedCheckbox values={values} onChange={onChange} />
</FieldGroup>
)}
</FieldContainer>
Expand Down Expand Up @@ -384,6 +419,7 @@ class Form extends Component<Props<Values, KeysOfUnion<Values>>, State> {
onBlur={onValidate('pattern')}
id="regex-matches"
/>
<ReplaceCapturedCheckbox values={values} onChange={onChange} />
</FieldGroup>
)}
</FieldContainer>
Expand Down Expand Up @@ -426,7 +462,7 @@ const FieldContainer = styled('div')<{hasTwoColumns: boolean}>`
@media (min-width: ${p => p.theme.breakpoints.sm}) {
gap: ${space(2)};
${p => p.hasTwoColumns && `grid-template-columns: 1fr 1fr;`}
margin-bottom: ${p => (p.hasTwoColumns ? 0 : space(2))};
margin-bottom: ${p => p.theme.space.xl};
}
`;

Expand All @@ -446,6 +482,7 @@ const SourceGroup = styled('div')<{isExpanded?: boolean}>`

const RegularExpression = styled(Input)`
font-family: ${p => p.theme.text.familyMono};
margin-bottom: ${p => p.theme.space.md};
`;

const DatasetRadioField = styled(RadioField)`
Expand Down Expand Up @@ -474,3 +511,14 @@ const Toggle = styled(Button)`
align-items: center;
}
`;

const ReplaceCapturedLabel = styled('label')<{disabled: boolean}>`
font-weight: normal;
margin-bottom: 0;
line-height: 1rem;
${p =>
p.disabled &&
css`
color: ${p.theme.disabled};
`}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
import Form from './form';
import handleError, {ErrorType} from './handleError';
import Modal from './modal';
import {useSourceGroupData} from './utils';
import {hasCaptureGroups, useSourceGroupData} from './utils';

type FormProps = React.ComponentProps<typeof Form>;
type Values = FormProps['values'];
Expand Down Expand Up @@ -118,6 +118,7 @@ class ModalManager extends Component<ModalManagerWithLocalStorageProps, State> {
source: initialState?.source ?? '',
placeholder: initialState?.placeholder ?? '',
pattern: initialState?.pattern ?? '',
replaceCaptured: initialState?.replaceCaptured ?? false,
};
}

Expand Down Expand Up @@ -244,8 +245,13 @@ class ModalManager extends Component<ModalManagerWithLocalStorageProps, State> {
[field]: value,
};

if (values.type !== RuleType.PATTERN && values.pattern) {
if (values.type !== RuleType.PATTERN) {
values.pattern = '';
values.replaceCaptured = false;
}

if (values.type === RuleType.PATTERN && !hasCaptureGroups(values.pattern)) {
values.replaceCaptured = false;
}

if (values.method !== MethodType.REPLACE && values.placeholder) {
Expand Down Expand Up @@ -291,7 +297,12 @@ class ModalManager extends Component<ModalManagerWithLocalStorageProps, State> {
handleValidate =
<K extends keyof Values>(field: K) =>
() => {
const isFieldValueEmpty = !this.state.values[field].trim();
const value = this.state.values[field];
if (typeof value !== 'string') {
return;
}

const isFieldValueEmpty = !value.trim();

const fieldErrorAlreadyExist = this.state.errors[field];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ export function useSourceGroupData() {
saveToSourceGroupData,
};
}

export function hasCaptureGroups(pattern: string) {
const m = pattern.match(/\(.*\)/);
return m !== null && m.length > 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Incorrect regex capture group detection logic

The hasCaptureGroups function incorrectly identifies capture groups by matching any parentheses with /\(.*\)/. This matches non-capturing groups like (?:...), lookaheads like (?=...), and even escaped literal parentheses \(, treating them all as capturing groups when they're not. This causes the checkbox to remain enabled for patterns that don't actually have capture groups.

Fix in Cursor Fix in Web

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fair point, but I only use this as a minimal condition to enable the checkbox. The user still needs to willingly check it to enable replacement.

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function getSubmitFormatRule(rule: Rule): PiiConfig {
method: rule.method,
text: rule?.placeholder,
},
replaceGroups: rule.replaceCaptured ? [1] : undefined,
};
}

Expand All @@ -22,6 +23,7 @@ function getSubmitFormatRule(rule: Rule): PiiConfig {
redaction: {
method: rule.method,
},
replaceGroups: rule.replaceCaptured ? [1] : undefined,
};
}

Expand Down
9 changes: 8 additions & 1 deletion static/app/views/settings/components/dataScrubbing/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export type RuleDefault = RuleBase & {

type RulePattern = RuleBase & {
pattern: string;
replaceCaptured: boolean;
type: RuleType.PATTERN;
} & Pick<RuleDefault, 'method'>;

Expand All @@ -94,7 +95,12 @@ export type EventId = {
value: string;
};

export type EditableRule = Omit<Record<KeysOfUnion<Rule>, string>, 'id'>;
export type EditableRule = Omit<
{
[K in KeysOfUnion<Rule>]: K extends 'replaceCaptured' ? boolean : string;
},
'id'
>;

export type AttributeResults = Record<
AllowedDataScrubbingDatasets,
Expand Down Expand Up @@ -122,6 +128,7 @@ type PiiConfigPattern = {
method: RulePattern['method'];
};
type: RulePattern['type'];
replaceGroups?: number[];
};

type PiiConfigReplaceAndPattern = Omit<PiiConfigPattern, 'redaction'> &
Expand Down
Loading