Skip to content
Open
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
11 changes: 2 additions & 9 deletions src/modules/integration-picker/components/IntegrationFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { FieldErrors, UseFormSetValue } from 'react-hook-form';
import { useForm } from 'react-hook-form';
import useDeepCompareEffect from 'use-deep-compare-effect';
import { AuthenticationNotice, ConnectorConfigField } from '../types';
import { shownFieldValue } from '../utils/credentialsPayload';
import { partitionNotices } from '../utils/partitionNotices';
import { formatSecretPlaceholder, isSecretPlaceholder } from '../utils/secretPlaceholder';
import {
Expand Down Expand Up @@ -309,15 +310,7 @@ export const IntegrationForm: React.FC<IntegrationFieldsProps> = ({
const schema = useMemo(() => createFormSchema(fields, rules), [fields, rules]);

const defaultValues = useMemo(() => {
const initialData: Record<string, string> = {};
fields.forEach((field) => {
if (field.value !== undefined) {
initialData[field.key] = field.value.toString();
} else {
initialData[field.key] = '';
}
});
return initialData;
return Object.fromEntries(fields.map((field) => [field.key, shownFieldValue(field)]));
}, [fields]);

const { formState, watch, reset, setValue, getValues } = useForm({
Expand Down
34 changes: 12 additions & 22 deletions src/modules/integration-picker/hooks/useIntegrationPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import {
isFalconConnectorConfig,
isLegacyConnectorConfig,
} from '../types';
import { buildCredentialsPayload, withoutClearedFields } from '../utils/credentialsPayload';
import { describeConnectError, errorStatus } from '../utils/requestErrors';
import { isSecretPlaceholder } from '../utils/secretPlaceholder';

// Shared retry configuration for queries
const RETRY_CONFIG = {
Expand Down Expand Up @@ -561,23 +561,13 @@ export const useIntegrationPicker = ({
setConnectionState({ loading: true, success: false });

try {
const cleanedFormData = { ...formData };
if (accountData) {
fields.forEach((field) => {
if (field.secret !== false || field.type === 'password') {
const fieldValue = cleanedFormData[field.key];
if (isSecretPlaceholder(fieldValue) && !editingSecrets.has(field.key)) {
delete cleanedFormData[field.key];
}
}
});
}

Object.keys(cleanedFormData).forEach((key) => {
if (cleanedFormData[key] === '') {
delete cleanedFormData[key];
}
const credentials = buildCredentialsPayload({
formData,
fields,
editingSecrets,
editing: !!prefillAccount,
});
const filledCredentials = withoutClearedFields(credentials);

const shouldRedirectForOAuth =
authConfig?.type === 'oauth2' &&
Expand Down Expand Up @@ -624,8 +614,8 @@ export const useIntegrationPicker = ({
`${dashboardUrl}/embedded/accounts/callback`,
);
let windowUrl = `${baseUrl}/connect/oauth2/${selectedIntegration.integration_id}?redirect_uri=${callbackEmbeddedAccountsUrl}&token=${token}&connection_attempt_id=${attemptId}`;
Object.keys(cleanedFormData).forEach((key) => {
windowUrl += `&${key}=${encodeURIComponent(cleanedFormData[key])}`;
Object.keys(filledCredentials).forEach((key) => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

On OAuth2 accounts a cleared optional field is still kept. filledCredentials has the null keys stripped, so the popup URL carries nothing for a field the user emptied, and the account keeps the old value. For example, with region=eu stored, emptying region and reconnecting leaves region=eu.

Could we pass cleared keys through the URL too (an empty value the API reads as removal), or call out in the PR that OAuth edits are out of scope for ENG-902?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Out of scope for ENG-902, as the PR body's "Sibling surfaces" table says (deferred). An empty query param alone wouldn't fix it: the OAuth callback merges the URL params onto the stored credentials, the same way the PATCH did. So removal needs an API change on the /connect/oauth2 callback too, ideally carrying the cleared keys on the connection attempt rather than in the URL. That's a separate change across hub and uc-api, so it'll get its own follow-up ticket, linked here once it's filed.

windowUrl += `&${key}=${encodeURIComponent(filledCredentials[key])}`;
});

const width = 1024;
Expand Down Expand Up @@ -691,14 +681,14 @@ export const useIntegrationPicker = ({
accountId,
token,
integrationId: selectedIntegration.integration_id,
credentials: cleanedFormData,
credentials,
});
successData = { id: accountId, provider: selectedIntegration.provider };
} else {
const response = await connectAccount({
baseUrl,
token,
credentials: cleanedFormData,
credentials: filledCredentials,
integrationId: selectedIntegration.integration_id,
});
if (!response) {
Expand All @@ -723,7 +713,7 @@ export const useIntegrationPicker = ({
formData,
editingSecrets,
handleSuccess,
accountData,
prefillAccount,
fields,
accountId,
authConfig,
Expand Down
173 changes: 173 additions & 0 deletions src/modules/integration-picker/utils/credentialsPayload.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { describe, expect, it } from 'vitest';
import type { ConnectorConfigField } from '../types';
import { buildCredentialsPayload, withoutClearedFields } from './credentialsPayload';

const field = (overrides: Partial<ConnectorConfigField>): ConnectorConfigField => ({
label: overrides.key ?? 'field',
key: 'field',
required: false,
readOnly: false,
secret: false,
placeholder: '',
...overrides,
});

const placeholder = '__secretvalue:**redacted**1234';

const shownFields = [
field({ key: 'apiUrl', required: true, value: 'https://api.example.com' }),
field({ key: 'region', value: 'eu' }),
field({ key: 'retries', value: '3' }),
field({ key: 'subdomain' }),
field({ key: 'apiKey', secret: true, required: true, value: placeholder }),
field({ key: 'apiToken', type: 'password', value: placeholder }),
];

describe('buildCredentialsPayload', () => {
describe('when editing an account', () => {
it('sends a field the form showed with a value and the user emptied as null', () => {
const actual = buildCredentialsPayload({
formData: { apiUrl: 'https://api.example.com', region: '', retries: '' },
fields: shownFields,
editingSecrets: new Set(),
editing: true,
});

expect(actual).toEqual({
apiUrl: 'https://api.example.com',
region: null,
retries: null,
});
});

it('sends a field showing a connector default the user emptied as null', () => {
const actual = buildCredentialsPayload({
formData: { apiUrl: 'https://api.example.com', timeout: '' },
fields: [...shownFields, field({ key: 'timeout', type: 'number', value: 0 })],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The hook never produces this state. The prefill in useIntegrationPicker does if (!prefilledValue) return { ...field } (around line 274, and again in the legacy branch near 341), so a 0 default reaches the form with no value, shows empty, and emptying it sends nothing. The test passes while the real flow doesn't clear the field.

Either use a truthy default like '30' here, or change the prefill check to prefilledValue === undefined || prefilledValue === '' and keep the test. That check predates this PR, but it also hides stored falsy values on edit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That state is reachable for a connector default, as opposed to a stored value. When nothing is stored, prefilledValue is the config's field.value. With 0, !prefilledValue takes the return { ...field, key: field.key } branch, and the spread keeps the config's own value: 0. IntegrationFields' defaults render that as "0" through shownFieldValue, so the user sees "0", empties it, and null goes out. That's what the test covers.

What you're describing is the stored-value case: a stored 0 or '' with a truthy default shows the default, because !prefilledValue falls back to { ...field }. That's real but predates this PR. s2d2 flagged it too, and it's being kept as a separate ticket. The end state is still correct, since emptying what the user sees removes the key.

editingSecrets: new Set(),
editing: true,
});

expect(actual).toEqual({ apiUrl: 'https://api.example.com', timeout: null });
});

it('never clears a secret field, even one showing a real value', () => {
const actual = buildCredentialsPayload({
formData: { apiUrl: 'https://api.example.com', apiToken: '' },
fields: [
...shownFields.filter((f) => f.key !== 'apiToken'),
field({ key: 'apiToken', type: 'password', value: 'legacy-token' }),
],
editingSecrets: new Set(['apiToken']),
editing: true,
});

expect(actual).toEqual({ apiUrl: 'https://api.example.com' });
});

it('treats a field without a secret flag as a secret, as the API stores it', () => {
const actual = buildCredentialsPayload({
formData: { apiUrl: 'https://api.example.com', webhookKey: '' },
fields: [
...shownFields,
field({ key: 'webhookKey', secret: undefined, value: placeholder }),
],
editingSecrets: new Set(['webhookKey']),
editing: true,
});

expect(actual).toEqual({ apiUrl: 'https://api.example.com' });
});

it('never clears a required field, leaving the stored value to the API', () => {
const actual = buildCredentialsPayload({
formData: { apiUrl: '', region: 'eu' },
fields: shownFields,
editingSecrets: new Set(),
editing: true,
});

expect(actual).toEqual({ region: 'eu' });
});

it('leaves out an empty field the form showed without a value', () => {
const actual = buildCredentialsPayload({
formData: { apiUrl: 'https://api.example.com', subdomain: '' },
fields: shownFields,
editingSecrets: new Set(),
editing: true,
});

expect(actual).toEqual({ apiUrl: 'https://api.example.com' });
});

it('leaves out a secret the user did not open, so the stored one is kept', () => {
const actual = buildCredentialsPayload({
formData: { apiUrl: 'https://api.example.com', apiKey: placeholder },
fields: shownFields,
editingSecrets: new Set(),
editing: true,
});

expect(actual).toEqual({ apiUrl: 'https://api.example.com' });
});

it('leaves out a secret the user opened and left empty, so the stored one is kept', () => {
const actual = buildCredentialsPayload({
formData: { apiUrl: 'https://api.example.com', apiToken: '' },
fields: shownFields,
editingSecrets: new Set(['apiToken']),
editing: true,
});

expect(actual).toEqual({ apiUrl: 'https://api.example.com' });
});

it('sends a placeholder the user opened as it stands', () => {
const actual = buildCredentialsPayload({
formData: { apiUrl: 'https://api.example.com', apiKey: placeholder },
fields: shownFields,
editingSecrets: new Set(['apiKey']),
editing: true,
});

expect(actual).toEqual({ apiUrl: 'https://api.example.com', apiKey: placeholder });
});

it('sends a value the user entered', () => {
const actual = buildCredentialsPayload({
formData: { apiUrl: 'https://api.example.com', apiKey: 'new-key', region: 'us' },
fields: shownFields,
editingSecrets: new Set(['apiKey']),
editing: true,
});

expect(actual).toEqual({
apiUrl: 'https://api.example.com',
apiKey: 'new-key',
region: 'us',
});
});
});

describe('when nothing stored is shown, as on a new account or another profile', () => {
it('leaves out an empty field', () => {
const actual = buildCredentialsPayload({
formData: { apiUrl: 'https://api.example.com', region: '', apiToken: '' },
fields: shownFields,
editingSecrets: new Set(),
editing: false,
});

expect(actual).toEqual({ apiUrl: 'https://api.example.com' });
});
});
});

describe('withoutClearedFields', () => {
it('drops the cleared fields and keeps the rest', () => {
const actual = withoutClearedFields({ apiUrl: 'https://api.example.com', region: null });

expect(actual).toEqual({ apiUrl: 'https://api.example.com' });
});
});
69 changes: 69 additions & 0 deletions src/modules/integration-picker/utils/credentialsPayload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type { ConnectorConfigField } from '../types';
import { isSecretPlaceholder } from './secretPlaceholder';

export type CredentialsPayload = Record<string, string | null>;

export const shownFieldValue = (field: ConnectorConfigField): string =>
field.value === undefined ? '' : field.value.toString();

const isSecretField = (field: ConnectorConfigField) =>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Optional secrets still can't be cleared. emptiedKeys skips every field where isSecretField holds, so if the user clicks Edit on an optional secret (key joins editingSecrets) and saves it empty, the key is left out and the API keeps the stored secret. Same for a type: 'password' field with secret: false.

Should an optional secret in editingSecrets that is left empty go out as null? If not clearing secrets is intended, worth noting on ENG-902.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not clearing secrets is intentional. The Edit button on a secret runs setValue(key, '') and adds the key to editingSecrets before the user types anything, and there's no cancel. So "opened and left empty" looks exactly like "deliberately cleared". Sending null there would turn one stray click into a deleted credential, which is worse than not supporting the clear. The API side refuses it as well (uc-api#9777 only clears optional secret: false fields), and logs the refused key names.

A type: 'password' field with secret: false is treated as a secret here because the hub prefills it as one (field.secret !== false || field.type === 'password' in the prefill), so it gets the same Edit-button behaviour. Agreed this belongs on ENG-902; I'll get it noted there.

field.secret !== false || field.type === 'password';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Does /connector config always send secret as a boolean? With secret !== false, a field that omits it counts as a secret, so it can never be cleared. Every fixture sets secret: false, so the tests don't cover that case. If it can be missing, secret === true || type === 'password' plus a fixture without secret would close it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A field with no secret flag is a secret everywhere else, so it shouldn't be clearable as a setup field:

  • The API's partitionSecrets stores it in the secret bundle (!isSetupField, where isSetupField is secret === false).
  • The hub prefill reads it from prefillAccount.secrets (field.secret !== false || field.type === 'password').

Switching to secret === true would send null for a field the API keeps as a secret, and the API refuses that clear anyway. I kept the rule and added the fixture you suggested in 9407133: "treats a field without a secret flag as a secret, as the API stores it".


const untouchedSecretKeys = (
formData: Record<string, string>,
fields: ConnectorConfigField[],
editingSecrets: Set<string>,
) =>
new Set(
fields
.filter(
(field) =>
isSecretField(field) &&
isSecretPlaceholder(formData[field.key]) &&
!editingSecrets.has(field.key),
)
.map((field) => field.key),
);

const emptiedKeys = (formData: Record<string, string>, fields: ConnectorConfigField[]) =>
new Set(
fields
.filter(
(field) =>
!isSecretField(field) &&
field.required !== true &&
shownFieldValue(field) !== '' &&
formData[field.key] === '',
)
.map((field) => field.key),
);

/**
* On an edit, a field the form showed with a value and the user emptied is sent as `null`, so
* the API removes it. Secrets are never cleared this way: opening one empties it before any typing.
*/
export const buildCredentialsPayload = ({
formData,
fields,
editingSecrets,
editing,
}: {
formData: Record<string, string>;
fields: ConnectorConfigField[];
editingSecrets: Set<string>;
editing: boolean;
}): CredentialsPayload => {
const keptSecrets = untouchedSecretKeys(formData, fields, editingSecrets);
const cleared = editing ? emptiedKeys(formData, fields) : new Set<string>();

return Object.fromEntries(
Object.entries(formData)
.filter(([key, value]) => !keptSecrets.has(key) && (value !== '' || cleared.has(key)))
.map(([key, value]) => [key, cleared.has(key) ? null : value]),
);
};

export const withoutClearedFields = (credentials: CredentialsPayload): Record<string, string> =>
Object.fromEntries(
Object.entries(credentials).filter((entry): entry is [string, string] => entry[1] !== null),
);