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:
@@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"mime/multipart"
|
||||
"net/textproto"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/datasource"
|
||||
@@ -15,6 +16,7 @@ import (
|
||||
"github.com/Tencent/WeKnora/internal/tracing/langfuse"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
@@ -153,8 +155,55 @@ func (s *DataSourceService) UpdateDataSource(ctx context.Context, ds *types.Data
|
||||
return nil, datasource.ErrDataSourceInvalid
|
||||
}
|
||||
|
||||
// Validate new configuration if changed
|
||||
if ds.Type != existing.Type || string(ds.Config) != string(existing.Config) {
|
||||
// Apply write-only secret merge semantics to the Config jsonb:
|
||||
// empty / redacted credential strings preserve existing values,
|
||||
// ClearCredentials wipes the whole map, other values replace. This
|
||||
// blocks the NELO-1299-style round-trip bug where a frontend that
|
||||
// echoed back the redacted placeholder would overwrite real secrets.
|
||||
//
|
||||
// We also remember the parsed configs for the post-merge "changed?"
|
||||
// comparison — a raw-bytes compare against ds.Config is unreliable
|
||||
// because Go map iteration order makes the re-serialized JSON's key
|
||||
// ordering nondeterministic, causing string(merged) != string(existing)
|
||||
// to spuriously trip every time.
|
||||
var mergedCfg, existingParsedCfg *types.DataSourceConfig
|
||||
if len(ds.Config) > 0 {
|
||||
incomingCfg, parseIncErr := ds.ParseConfig()
|
||||
existingCfg, parseExErr := existing.ParseConfig()
|
||||
if parseIncErr == nil && parseExErr == nil && incomingCfg != nil {
|
||||
baseExisting := types.DataSourceConfig{}
|
||||
if existingCfg != nil {
|
||||
baseExisting = *existingCfg
|
||||
}
|
||||
if incomingCfg.ClearCredentials {
|
||||
logger.Infof(ctx, "DataSource credentials cleared by user: id=%s",
|
||||
secutils.SanitizeForLog(ds.ID))
|
||||
}
|
||||
merged := incomingCfg.MergeUpdate(baseExisting)
|
||||
if blob, err := merged.ToJSON(); err == nil {
|
||||
ds.Config = blob
|
||||
}
|
||||
mergedCfg = &merged
|
||||
existingParsedCfg = existingCfg
|
||||
}
|
||||
}
|
||||
|
||||
// Validate new configuration if changed. Compare parsed structures to
|
||||
// avoid re-running (potentially expensive) OAuth / live-connection
|
||||
// validators on every update that only re-orders JSON keys.
|
||||
//
|
||||
// Skip validation entirely when the user is explicitly revoking
|
||||
// credentials (ClearCredentials=true). Live validators (e.g. Notion's
|
||||
// /me API) require a working secret by definition; trying to validate
|
||||
// after a wipe would always fail and leave the row un-cleared, defeating
|
||||
// the whole point of the "Remove all credentials" affordance.
|
||||
configActuallyChanged := true
|
||||
if mergedCfg != nil && existingParsedCfg != nil {
|
||||
configActuallyChanged = !reflect.DeepEqual(*mergedCfg, *existingParsedCfg)
|
||||
}
|
||||
clearingCredentials := mergedCfg != nil && !mergedCfg.HasCredentials() &&
|
||||
existingParsedCfg != nil && existingParsedCfg.HasCredentials()
|
||||
if !clearingCredentials && (ds.Type != existing.Type || configActuallyChanged) {
|
||||
if err := s.validateDataSourceConfig(ctx, ds); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user