danny0405 commented on code in PR #20021: URL: https://github.com/apache/hudi/pull/20021#discussion_r4139832291
########## hudi-agent-gateway/skills/hudi-architect/redact_sensitive_values.py: ########## @@ -0,0 +1,181 @@ +#!/usr/bin/env python3 +# +# 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. +# +"""Redact common credentials from evidence before the skill quotes it. + +This utility deliberately treats its input as opaque text. It does not parse or +execute DDL, shell commands, URLs, or configuration supplied by the user. +""" + +from __future__ import annotations + +import argparse +import re +import sys +from pathlib import Path + +REDACTED = "<redacted>" + +SENSITIVE_KEY = re.compile( + r"(?ix)(?:" + r"authorization|password|passwd|pwd|secret|token|credential|" + r"api[._-]?key|access[._-]?key|secret[._-]?key|client[._-]?secret|" + r"private[._-]?key" + r")" +) + +ASSIGNMENT = re.compile( + r"(?P<prefix>(?P<key_quote>[\"']?)(?P<key>[A-Za-z0-9_.-]+)" + r"(?P=key_quote)\s*(?:=|:)\s*)" Review Comment: [P2] Bound assignment matching to token starts to avoid quadratic scans Removing the anchor without adding a key-start boundary makes `ASSIGNMENT.search` retry `[A-Za-z0-9_.-]+` at every character of a long token that has no following `=` or `:`. This also occurs inside ordinary non-sensitive values, because `_redact_assignments` resumes searching immediately after their prefixes. I reproduced it with `redact_sensitive_text('payload=' + 'x' * n + '\n')`: on Python 3.12, 8,000 characters took about 0.63 s and 16,000 took 2.48 s; the previous implementation handled the same 16,000-character input in under 1 ms on this machine. Long payloads/encoded strings in supplied logs or configuration can therefore stall the mandatory sanitization step. Please restrict candidate key starts or use a scanner that avoids re-scanning every suffix, and cover a long non-secret value in regression testing. ########## hudi-agent-gateway/skills/hudi-architect/redact_sensitive_values.py: ########## @@ -0,0 +1,181 @@ +#!/usr/bin/env python3 +# +# 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. +# +"""Redact common credentials from evidence before the skill quotes it. + +This utility deliberately treats its input as opaque text. It does not parse or +execute DDL, shell commands, URLs, or configuration supplied by the user. +""" + +from __future__ import annotations + +import argparse +import re +import sys +from pathlib import Path + +REDACTED = "<redacted>" + +SENSITIVE_KEY = re.compile( + r"(?ix)(?:" + r"authorization|password|passwd|pwd|secret|token|credential|" + r"api[._-]?key|access[._-]?key|secret[._-]?key|client[._-]?secret|" + r"private[._-]?key" + r")" +) + +ASSIGNMENT = re.compile( + r"(?P<prefix>(?P<key_quote>[\"']?)(?P<key>[A-Za-z0-9_.-]+)" + r"(?P=key_quote)\s*(?:=|:)\s*)" +) + +URI_USERINFO = re.compile( + r"(?P<scheme>\b[a-zA-Z][a-zA-Z0-9+.-]*://)(?P<userinfo>[^/@\s]+@)" +) + +SENSITIVE_QUERY_PARAMETER = re.compile( + r"(?i)(?P<prefix>[?&](?:access_token|api_key|apikey|client_secret|password|" + r"secret|secret_key|token)=)(?P<value>[^&#\s\"']+)" +) + +SENSITIVE_CLI_FLAG = re.compile( + r"(?i)(?P<prefix>--(?:access[-_]?key|api[-_]?key|client[-_]?secret|password|" + r"secret|secret[-_]?key|token)(?:=|\s+))" +) + +AUTHORIZATION_HEADER = re.compile( + r"(?im)(?P<prefix>^[ \t]*authorization[ \t]*:[ \t]*)[^\r\n]*" +) + +PRIVATE_KEY_BLOCK = re.compile( + r"-----BEGIN(?: [A-Z0-9]+)* PRIVATE KEY-----.*?" + r"-----END(?: [A-Z0-9]+)* PRIVATE KEY-----", + re.DOTALL, +) + +AWS_ACCESS_KEY_ID = re.compile(r"\b(?:AKIA|ASIA)[A-Z0-9]{16}\b") + + +def _consume_quoted_value(text: str, start: int) -> tuple[int, str]: + """Consume one complete quoted value, including escapes and SQL doubled quotes.""" + quote = text[start] + index = start + 1 + while index < len(text): + character = text[index] + if character in "\r\n": + # Fail closed for malformed input without swallowing unrelated later lines. + return index, f"{quote}{REDACTED}" + if character == "\\": + index = min(index + 2, len(text)) + continue + if character == quote: + if index + 1 < len(text) and text[index + 1] == quote: + index += 2 + continue + return index + 1, f"{quote}{REDACTED}{quote}" + index += 1 + return index, f"{quote}{REDACTED}" + + +def _consume_assignment_value(text: str, start: int) -> tuple[int, str]: + if start < len(text) and text[start] in "\"'": + return _consume_quoted_value(text, start) + + index = start + while index < len(text) and text[index] not in ",;)}]&\r\n": Review Comment: [P1] Preserve full-value redaction for unquoted properties These delimiters are not necessarily assignment boundaries in the property/config evidence this helper accepts; they can be literal password characters. At this revision, `password=first&second\n` becomes `password=<redacted>&second\n`, and `s3.secret-key=first,second;third}fourth\n` becomes `s3.secret-key=<redacted>,second;third}fourth\n`. Both expose the credential suffix; the previous implementation redacted the entire line in these cases. Since the skill uses this output before quoting evidence into responses/ADRs, please distinguish standalone property values from inline structured syntax, or conservatively redact the whole ambiguous value rather than returning its suffix. Add regression cases for punctuation-bearing unquoted credentials. -- 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]
