fix: redact sensitive fields and prevent credential loss on edit

Closes #988

Unify the "write-only secrets" pattern across MCP services, Models,
WebSearch providers, and DataSources. Fix two related defects:

1. GET responses leaked plaintext credentials — GetMCPServiceByID
   while List masked, all Model / WebSearchProvider / DataSource GET
   responses unconditionally.
2. When the frontend echoed a masked placeholder back on save, the
   update path overwrote the stored secret with the mask — silently
   wiping the real credential. MCP surfaced this most visibly:
   ListMCPServices masked Token to "first4****last4" and
   McpServiceDialog round-tripped that masked string back into the DB.

- internal/types/secret.go introduces the shared sentinel
  RedactedSecretPlaceholder = "***" and two helpers:
  - IsRedactedOrEmpty(s): true for "" or the sentinel
  - PreserveIfRedacted(incoming, existing): merge predicate
- Each resource adds a RedactSensitiveData (mutates-in-place) or
  Redacted (returns-a-copy) method and a MergeUpdate contract:
  empty or placeholder preserves, any other value replaces, and a
  write-only Clear* bool flag (gorm:"-", json omitempty) wipes with
  precedence over preserve / replace. Clear* never persists.
- Every GET / List / Create / Update response path now redacts.
  VectorStore's pre-existing "***" literal is refactored onto the
  shared constant for consistency.

All four dialogs — McpServiceDialog, ModelEditorDialog,
WebSearchSettings, DataSourceEditorDialog — never pre-fill secret
inputs. The stored-state signal is collapsed into the input
placeholder: "•••••••• (Enter new value to replace)" when the server
reports a stored value, falling back to the connector-specific
placeholder otherwise. The input itself carries the "something is
there" cue, matching how password dots render.

An opt-in "Remove this credential" checkbox appears in edit mode when
a value is stored; checking it disables the input and triggers a
danger-themed confirmation dialog before submit. DataSource rolls the
clear up to a connector-level "Remove all credentials" flag since its
credentials are per-connector atomic bundles (e.g. Feishu app_id +
app_secret).

i18n updates across en-US, zh-CN, ko-KR, ru-RU:
secret.storedPlaceholder / clearHint / confirmClearTitle /
confirmClearBody.

- MCP update handler's manual map-to-struct projection was silently
  dropping clear_api_key / clear_token / custom_headers on partial
  updates. Pull them off the map explicitly.
- MCP configChanged detection now snapshots URL / StdioConfig /
  TransportType before the merge so connection recycling still
  triggers on real transport changes (not just whenever AuthConfig
  is present).
- MCP CustomHeaders: nil preserves existing, non-nil replaces. Was
  always overwriting, silently wiping headers on any auth-bearing
  update.
- GetMCPServiceByID now redacts (was plaintext while List masked).
- WebSearchProvider update preserves Name / Description when empty
  instead of clobbering them.
- WebSearchProvider TestProviderRaw rejects the "***" placeholder up
  front with an actionable message rather than forwarding it to the
  upstream provider API as a literal key.
- Model builtin hideSensitiveInfo now preserves non-secret metadata
  (SupportsVision, Provider, ExtraConfig, InterfaceType,
  ParameterSize, EmbeddingParameters) that were previously zeroed
  out for builtin model cards.
- Replace local equalStringSlices / equalStringMaps helpers with
  stdlib slices.Equal / maps.Equal (Go 1.21+).

- internal/types/{secret,mcp,model,web_search_provider,datasource}_test.go:
  table-driven coverage of merge, redact, clear, and write-only flag
  invariants for every resource.
- internal/application/service/mcp_service_test.go: full service layer
  with a fake repo; covers ListMCPServices / GetMCPServiceByID
  redaction, UpdateMCPService partial-update honoring of Clear*,
  CustomHeaders preserve-on-nil / replace-on-non-nil, and
  clear-flag precedence over submitted values.

Additive only. No changes to request or response JSON shape beyond
the write-only Clear* flags (omitted by default via omitempty). No DB
schema changes. No SDK struct field removals. Existing clients that
don't know about the new flags continue to function unchanged.

- DataSource.Config jsonb at-rest encryption. Pre-existing residual
  risk; tracked as a follow-up.
- Renaming VectorStore.MaskSensitiveFields to match the new
  RedactSensitiveData / Redacted naming convention. Left alone to
  minimize diff; a follow-up rename PR can unify.
This commit is contained in:
ochan.kwon
2026-04-18 21:48:50 +09:00
committed by lyingbug
parent a3411899cf
commit d869cc1ff6
32 changed files with 2039 additions and 130 deletions
+66 -4
View File
@@ -211,8 +211,16 @@
<t-input
v-model="formData.apiKey"
type="password"
:placeholder="$t('model.editor.apiKeyPlaceholder')"
:disabled="formData.clearApiKey"
:placeholder="apiKeyPlaceholder"
/>
<t-checkbox
v-if="isEdit && formData.hasExistingApiKey"
v-model="formData.clearApiKey"
class="clear-credential"
>
{{ t('secret.clearHint') }}
</t-checkbox>
</div>
<!-- 自定义 HTTP Header(类似 OpenAI Python SDK 的 extra_headers) -->
@@ -331,7 +339,7 @@
<script setup lang="ts">
import { ref, watch, computed, onUnmounted, nextTick } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
import { MessagePlugin, DialogPlugin } from 'tdesign-vue-next'
import { checkOllamaModels, checkRemoteModel, testEmbeddingModel, checkRerankModel, checkASRModel, listOllamaModels, downloadOllamaModel, getDownloadProgress, checkOllamaStatus, listModelProviders, type OllamaModelInfo, type ModelProviderOption } from '@/api/initialization'
import { getWeKnoraCloudStatus } from '@/api/model'
import { useI18n } from 'vue-i18n'
@@ -352,6 +360,12 @@ interface ModelFormData {
modelName: string
baseUrl?: string
apiKey?: string
// True when the server returned '***' for parameters.api_key, meaning a
// key is currently stored server-side. Drives the "Set / Not set" badge.
hasExistingApiKey?: boolean
// Write-only flag set by the user to explicitly remove the stored key on
// save. Reset to false on every dialog open.
clearApiKey?: boolean
dimension?: number
interfaceType?: 'ollama' | 'openai'
isDefault: boolean
@@ -539,6 +553,17 @@ const dialogVisible = computed({
const isEdit = computed(() => !!props.modelData)
// Placeholder hint for the API key field. On edit with a stored key, swap
// in the shared "bullets + Enter new value to replace" placeholder so the
// input itself carries the "something is there" signal. Otherwise fall back
// to the generic creation-mode placeholder.
const apiKeyPlaceholder = computed(() => {
if (isEdit.value && formData.value.hasExistingApiKey) {
return t('secret.storedPlaceholder')
}
return t('model.editor.apiKeyPlaceholder')
})
const formRef = ref()
const saving = ref(false)
const modelChecked = ref(false)
@@ -745,8 +770,14 @@ watch(() => props.visible, (val) => {
if (props.modelData) {
// 编辑:始终用最新的 modelData 覆盖
// Always reset apiKey / clearApiKey on every dialog open so the
// invariant "non-empty apiKey means the user typed it" holds.
// hasExistingApiKey flows through unchanged so the badge keeps showing
// "Set" when the server said '***'.
formData.value = {
...props.modelData,
apiKey: '',
clearApiKey: false,
customHeaders: Array.isArray(props.modelData.customHeaders)
? props.modelData.customHeaders.map(h => ({ key: h.key, value: h.value }))
: []
@@ -781,6 +812,8 @@ const resetForm = () => {
modelName: '',
baseUrl: '',
apiKey: '',
hasExistingApiKey: false,
clearApiKey: false,
dimension: undefined, // 默认不填,让用户手动输入或通过检测按钮获取
interfaceType: undefined,
isDefault: false,
@@ -1117,13 +1150,30 @@ const handleConfirm = async () => {
// 执行表单验证
await formRef.value?.validate()
// Confirm irrevocable credential removal before emitting.
if (formData.value.clearApiKey) {
const ok = await new Promise<boolean>((resolve) => {
const d = DialogPlugin.confirm({
header: t('secret.confirmClearTitle'),
body: t('secret.confirmClearBody'),
confirmBtn: { content: t('common.confirm'), theme: 'danger' },
cancelBtn: t('common.cancel'),
onConfirm: () => { d.hide(); resolve(true) },
onCancel: () => { d.hide(); resolve(false) },
onClose: () => { d.hide(); resolve(false) },
})
})
if (!ok) return
}
saving.value = true
// 如果是新增且没有 id,生成一个
if (!formData.value.id) {
formData.value.id = generateId()
}
emit('confirm', { ...formData.value })
dialogVisible.value = false
// 保存成功后重置草稿,下次打开新增模型时是空白
@@ -1658,6 +1708,18 @@ const handleCancel = () => {
}
}
}
// Destructive-action checkbox for "Remove this credential". Styled to match
// the pattern used in McpServiceDialog so the two dialogs read identically.
.clear-credential {
display: inline-flex;
margin-top: 8px;
:deep(.t-checkbox__label) {
color: var(--td-error-color);
font-size: 13px;
}
}
</style>
<!-- 非 scoped 样式:t-select popup 渲染到 body 下,scoped 样式无法覆盖 -->