Qiao-yq commented on code in PR #1594: URL: https://github.com/apache/dubbo-admin/pull/1594#discussion_r4400816400
########## ai/security/sanitizer.go: ########## @@ -0,0 +1,365 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package security + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "net/http" + "net/url" + "regexp" + "sort" + "strings" +) + +const ( + SanitizerRuleVersion = "2026-10-10.v1" + redactedValue = "[REDACTED]" + maxStructuredBytes = 1 << 20 + maxStructuredDepth = 32 +) + +const ( + RuleSensitiveField = "secret.sensitive-field" + RuleAuthorization = "secret.authorization" + RuleCredentialAssignment = "secret.credential-assignment" + RulePrivateKey = "secret.private-key" + RuleJWT = "secret.jwt" + RuleAPIKey = "secret.api-key" + RuleEncodedSecret = "secret.encoded" +) + +var ( + privateKeyPattern = regexp.MustCompile(`(?is)-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----.*?-----END [A-Z0-9 ]*PRIVATE KEY-----`) + authorizationPattern = regexp.MustCompile(`(?i)\b(?:bearer|basic)\s+[A-Za-z0-9._~+/=-]+`) + jwtPattern = regexp.MustCompile(`\beyJ[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}\b`) + apiKeyPattern = regexp.MustCompile(`\b(?:sk-[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{16,}|AIza[A-Za-z0-9_-]{20,}|xox[baprs]-[A-Za-z0-9-]{16,})\b`) + credentialAssignmentPattern = regexp.MustCompile(`(?i)\b(authorization|proxy[-_]?authorization|cookie|set[-_]?cookie|token|access[-_]?token|refresh[-_]?token|api[-_]?key|apikey|password|passwd|secret|client[-_]?secret|client[-_]?key[-_]?data|client[-_]?certificate[-_]?data)\b(\s*"?\s*(?::|=|%3[dD])\s*)(?:"[^"\r\n]*"|'[^'\r\n]*'|[^\s,;&\r\n]+)`) Review Comment: Fixed at `8eaa867`. The quoted-value branches of the credential-assignment rule now consume escape pairs, so `\"` and `\\` are treated as part of the value instead of terminating it: ```go (?:"(?:[^"\\\r\n]|\\.)*"|'(?:[^'\\\r\n]|\\.)*'|[^\s,;&\r\n]+) ``` Because `SanitizeText` is the shared boundary, this fix applies to the model-input view, logging, and derived-index validation. Regressions added: - `TestSanitizeTextRedactsEscapedQuotesInJSONCredentials` — escaped quote and escaped backslash - `TestModelInputViewRedactsEscapedCredentialValues` — escaped quote and escaped backslash at the actual `modelInputView` boundary, asserting the canonical message is unchanged `go test ./security/... ./component/agent/react/...`, `go vet`, and `gofmt` pass at `8eaa867`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
