跳至主要內容

SensitiveDataFilter

從 span 欄位中遮蔽敏感資訊的 SpanOutputProcessor。

預設自動套用
「預設自動套用」的直接連結

Observability 會自動將 SensitiveDataFilter 附加至每個已設定執行個體的 spanOutputProcessors,讓機密資料在送達 Mastra cloud exporter 等 exporter 前先經過遮蔽。此 filter 會最後執行(在使用者提供的所有 processor 之後),因此上游 processor 所引入或揭露的敏感資料仍會被遮蔽。除非您要自訂選項,否則不需要手動加入。

若要選擇停用或自訂自動套用的 filter,請使用 Observability registry 設定中的 sensitiveDataFilter 選項:

import { Observability } from '@mastra/observability'

new Observability({
configs: {/* ... */},
// disable the auto-applied filter
sensitiveDataFilter: false,
// or customize it
// sensitiveDataFilter: { sensitiveFields: ['mySecret'], redactionStyle: 'partial' },
})

如果某設定已在 spanOutputProcessors 中包含 SensitiveDataFilter,則會略過自動套用的 filter,以避免重複遮蔽。預先執行個體化的 ObservabilityInstance 值不會修改。如有需要,請自行將 SensitiveDataFilter 加入其 processor。

建構函式
「建構函式」的直接連結

new SensitiveDataFilter(options?: SensitiveDataFilterOptions)

SensitiveDataFilterOptions
「sensitivedatafilteroptions」的直接連結

interface SensitiveDataFilterOptions {
/**
* List of sensitive field names to redact.
* Matching is case-insensitive and normalizes separators
* (api-key, api_key, Api Key → apikey).
* Defaults include: password, token, secret, key, apikey, auth,
* authorization, bearer, bearertoken, jwt, credential,
* clientsecret, privatekey, refresh, ssn.
*/
sensitiveFields?: string[]

/**
* The token used for full redaction.
* Default: "[REDACTED]"
*/
redactionToken?: string

/**
* Style of redaction to use:
* - "full": always replace with redactionToken
* - "partial": show 3 characters from the start and end, redact the middle
* Default: "full"
*/
redactionStyle?: RedactionStyle
}

RedactionStyle
「redactionstyle」的直接連結

type RedactionStyle = 'full' | 'partial'

方法
「方法」的直接連結

process
「process」的直接連結

process(span: AnySpan): AnySpan

篩選 span 關鍵欄位中的敏感資料以處理 span,這些欄位包括 attributes、metadata、input、output 與 errorInfo。

**傳回:**敏感值已遮蔽的新 span。

shutdown
「shutdown」的直接連結

async shutdown(): Promise<void>

此 processor 不需要清理。

屬性
「屬性」的直接連結

readonly name = 'sensitive-data-filter';

預設敏感欄位
「預設敏感欄位」的直接連結

未提供自訂欄位時:

[
'password',
'token',
'secret',
'key',
'apikey',
'auth',
'authorization',
'bearer',
'bearertoken',
'jwt',
'credential',
'clientsecret',
'privatekey',
'refresh',
'ssn',
]

處理行為
「處理行為」的直接連結

欄位比對
「欄位比對」的直接連結

  • 不區分大小寫APIKeyapikeyApiKey 都會相符
  • 不受分隔符號影響api-keyapi_keyapiKey 會視為相同
  • 完全比對:正規化後,欄位必須完全相符
    • token 會與 tokenTokenTOKEN 相符
    • token 不會與 promptTokenstokenCount 相符

遮蔽樣式
「遮蔽樣式」的直接連結

完整遮蔽(預設)
「完整遮蔽(預設)」的直接連結

所有相符值都會替換為 redactionToken。

部分遮蔽
「部分遮蔽」的直接連結

  • 顯示開頭與結尾各 3 個字元
  • 長度 ≤ 6 個字元的值會完整遮蔽
  • 非字串值會先轉換為字串,再進行部分遮蔽

錯誤處理
「錯誤處理」的直接連結

若篩選欄位失敗,該欄位會替換為:

{
error: {
processor: 'sensitive-data-filter'
}
}

處理的欄位
「處理的欄位」的直接連結

Filter 會遞迴處理:

  • span.attributes - Span metadata 與屬性
  • span.metadata - 自訂 metadata
  • span.input - 輸入資料
  • span.output - 輸出資料
  • span.errorInfo - 錯誤資訊

能安全處理巢狀物件、array 與循環參照。