fix(dify-builder): ground only credentials and host endpoints, never data inputs

The test form and mock data skipped every start variable an http node
read, so a search query or a language header was never mocked. Only a
host-position URL template, credential-keyed header/param lines and an
api-key auth now count, with one shared is_credential_key in core. A
bare "key" param, an empty credential and a bare "Bearer" are grounded
too, and an existing Dify template is never re-grounded.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Charles YaoandClaude Opus 5 committed 2026-09-22 09:43:00 -07:00
1 parent e741ce902b
commit e54a2cbd2a
7 files changed
+470 -91

No files matched your search

+61 -25
View File
@@ -833,22 +833,44 @@ def _ground_placeholder_endpoints(intents: list[MutationIntent], *, trusted_text
return grounded
# A value that IS one of these words and nothing else (no content after it)
# is a bare auth-scheme with no credential attached -- ``strip_auth_scheme``
# alone does not catch this: its regex only strips a scheme word FOLLOWED BY
# whitespace, so a bare "Bearer" (nothing after it) comes back unstripped,
# not empty.
_BARE_SCHEME_WORDS = frozenset({"bearer", "basic", "token"})
# Mirrors user_supplied._TEMPLATE_MARKER: a Dify template reference names a
# variable, not a literal, so it is never "invented" and must never be
# re-grounded -- checked BEFORE the placeholder-shape check below, since a
# grounded variable's own NAME (e.g. "your_api_key") can coincidentally match
# CREDENTIAL_PLACEHOLDER_RE's "YOUR...KEY" pattern.
_TEMPLATE_MARKER = "{{#"
def _is_invented_credential(value: str, trusted_text: str) -> bool:
"""Mirrors ``_ground_placeholder_endpoints``'s OR-of-two-checks shape, but
for a credential value (a header/param 'Value' half, or an
``authorization.config.api_key``): an unmistakable placeholder
(``CREDENTIAL_PLACEHOLDER_RE``, after stripping a leading ``Bearer
``/``Basic ``/``Token `` scheme) grounds even in placeholder-only mode
(``trusted_text == ""``, mirroring how a shaped-like-invented URL grounds
with no goal text at all); otherwise, with ``trusted_text`` non-empty, a
value that never appears verbatim in it also grounds (S5b-style
plausible-looking secret the model invented). An empty value has nothing
to ground.
``authorization.config.api_key``):
1. Already a Dify template reference (contains ``{{#``) -- a real
reference, never re-grounded, checked first (see ``_TEMPLATE_MARKER``).
2. Empty, or nothing beyond a bare ``Bearer``/``Basic``/``Token`` scheme
word -- never a valid credential either way, so it grounds
unconditionally.
3. An unmistakable placeholder (``CREDENTIAL_PLACEHOLDER_RE``) -- grounds
even in placeholder-only mode (``trusted_text == ""``, mirroring how a
shaped-like-invented URL grounds with no goal text at all).
4. Otherwise, with ``trusted_text`` non-empty, a value that never appears
verbatim in it also grounds (S5b-style plausible-looking secret the
model invented).
"""
stripped_value = value.strip()
remainder = user_supplied.strip_auth_scheme(stripped_value)
if not remainder:
if _TEMPLATE_MARKER in remainder:
return False
if not remainder or remainder.lower() in _BARE_SCHEME_WORDS:
return True
if user_supplied.CREDENTIAL_PLACEHOLDER_RE.search(remainder):
return True
return bool(trusted_text) and not user_supplied.is_user_supplied_secret(stripped_value, trusted_text)
@@ -856,26 +878,34 @@ def _is_invented_credential(value: str, trusted_text: str) -> bool:
def _replace_credential_value(value: str, template_ref: str) -> str:
"""Rebuild a header/param 'Value' half with its secret swapped for
``template_ref``, preserving surrounding whitespace and a leading
``Bearer ``/``Basic ``/``Token `` auth-scheme prefix exactly as written
(e.g. ``" Bearer YOUR_API_KEY"`` -> ``" Bearer {{#s.h_api_key#}}"``)."""
lstripped = value.lstrip(" \t")
``template_ref``, preserving surrounding whitespace (including a ``\\r``
left by a ``\\r\\n`` line ending) and a leading ``Bearer ``/``Basic ``/
``Token `` auth-scheme prefix exactly as written (e.g. ``" Bearer
YOUR_API_KEY"`` -> ``" Bearer {{#s.h_api_key#}}"``). A BARE scheme word
with nothing after it (``"Bearer"``) keeps the word, adding the
separating space the grounded value now needs."""
lstripped = value.lstrip(" \t\r")
leading_ws = value[: len(value) - len(lstripped)]
core = lstripped.rstrip(" \t")
core = lstripped.rstrip(" \t\r")
trailing_ws = lstripped[len(core) :]
remainder = user_supplied.strip_auth_scheme(core)
scheme_prefix = core[: len(core) - len(remainder)]
if remainder == core and core.lower() in _BARE_SCHEME_WORDS:
scheme_prefix = f"{core} "
else:
scheme_prefix = core[: len(core) - len(remainder)]
return f"{leading_ws}{scheme_prefix}{template_ref}{trailing_ws}"
def _ground_credential_text(text: str, template_ref: str, *, trusted_text: str) -> tuple[str, bool]:
def _ground_credential_text(text: str, template_ref: str, *, trusted_text: str, key_is_credential) -> tuple[str, bool]:
"""Ground each ``Key: Value`` line of an http-request ``headers``/
``params`` text block whose key names a credential
(``user_supplied.is_credential_key``, decided by the key's LAST segment)
and whose value the user didn't supply (``_is_invented_credential``),
replacing only the secret part of that line with ``template_ref``. Every
other line -- including a credential line the user DID supply -- passes
through byte-identical. Returns ``(new_text, changed)``.
``params`` text block whose key names a credential per
``key_is_credential`` (``user_supplied.is_credential_key`` for headers;
``user_supplied.is_credential_param_key`` for params, which also treats a
bare ``key`` as one) and whose value the user didn't supply
(``_is_invented_credential``), replacing only the secret part of that
line with ``template_ref``. Every other line -- including a credential
line the user DID supply -- passes through byte-identical. Returns
``(new_text, changed)``.
"""
if not text:
return text, False
@@ -887,7 +917,7 @@ def _ground_credential_text(text: str, template_ref: str, *, trusted_text: str)
new_lines.append(line)
continue
key, _sep, value = line.partition(":")
if not user_supplied.is_credential_key(key.strip()) or not _is_invented_credential(value, trusted_text):
if not key_is_credential(key.strip()) or not _is_invented_credential(value, trusted_text):
new_lines.append(line)
continue
new_lines.append(f"{key}:{_replace_credential_value(value, template_ref)}")
@@ -936,14 +966,20 @@ def _ground_placeholder_credentials(intents: list[MutationIntent], *, trusted_te
changed = False
new_headers, headers_changed = _ground_credential_text(
str(node_config.get("headers") or ""), template_ref, trusted_text=trusted_text
str(node_config.get("headers") or ""),
template_ref,
trusted_text=trusted_text,
key_is_credential=user_supplied.is_credential_key,
)
if headers_changed:
node_config["headers"] = new_headers
changed = True
new_params, params_changed = _ground_credential_text(
str(node_config.get("params") or ""), template_ref, trusted_text=trusted_text
str(node_config.get("params") or ""),
template_ref,
trusted_text=trusted_text,
key_is_credential=user_supplied.is_credential_param_key,
)
if params_changed:
node_config["params"] = new_params
@@ -19,6 +19,19 @@ from collections.abc import Mapping
from typing import Any
from urllib.parse import urlsplit
from core.dify_builder.credentials import is_credential_key, is_credential_param_key
__all__ = [
"CREDENTIAL_PLACEHOLDER_RE",
"is_credential_key",
"is_credential_param_key",
"is_user_supplied_secret",
"is_user_supplied_url",
"strip_auth_scheme",
"trusted_text_for",
"url_hosts",
]
# A URL literal as it appears embedded in freeform prose. Restricted to the
# ASCII characters RFC 3986 allows in a URI, so it stops before CJK or other
# non-URL text glued onto the URL with no separating space (e.g. a Chinese
@@ -51,21 +64,6 @@ CREDENTIAL_PLACEHOLDER_RE = re.compile(
re.IGNORECASE,
)
# A dict/field key's last word-segment that, by itself, names a credential
# regardless of what comes before it (so "access_token" and "auth" both
# qualify, but "token_limit" does not -- "limit" is the last segment).
_CREDENTIAL_LAST_SEGMENTS = {"authorization", "auth", "token", "secret", "password", "passwd"}
# A last segment of "key" only counts as a credential when the segment (or
# merged word) before it names what kind of key it is -- otherwise ordinary
# fields like "key_points" would match on "key" alone.
_CREDENTIAL_KEY_PREFIXES = {"api", "access", "secret", "private", "auth"}
# Splits an identifier into camelCase words: an uppercase run immediately
# followed by "Xy" (e.g. the "API" in "APIKey"), or an optional leading
# capital plus a run of lowercase/digits (e.g. "api", "Key", "3").
_CAMEL_SEGMENT_RE = re.compile(r"[A-Z]+(?=[A-Z][a-z])|[A-Z]?[a-z0-9]+|[A-Z]+|[0-9]+")
def url_hosts(text: str) -> set[str]:
"""Lowercase hostnames of every ``http(s)://…`` URL literally present in
@@ -125,34 +123,6 @@ def is_user_supplied_secret(value: str, trusted_text: str) -> bool:
return remainder in (trusted_text or "")
def _key_segments(key: str) -> list[str]:
"""Lowercase word-segments of a dict/field key, split on ``_``, ``-``,
``.``, whitespace, and camelCase boundaries -- so ``x-api-key``,
``apiKey`` and ``api_key`` all segment to ``["api", "key"]``."""
segments: list[str] = []
for part in re.split(r"[_\-.\s]+", key or ""):
if not part:
continue
segments.extend(match.lower() for match in _CAMEL_SEGMENT_RE.findall(part))
return segments
def is_credential_key(key: str) -> bool:
"""True iff ``key`` names a credential, decided by its LAST segment --
not by containing a credential-ish substring anywhere. So
``password_policy``, ``token_limit``, ``session_token_expiry`` and
``max_tokens`` are NOT credential keys, but ``api_key``, ``apiKey``,
``x-api-key``, ``access_token`` and ``Authorization`` are.
"""
segments = _key_segments(key)
if not segments:
return False
last = segments[-1]
if last in _CREDENTIAL_LAST_SEGMENTS or last == "apikey":
return True
return last == "key" and len(segments) >= 2 and segments[-2] in _CREDENTIAL_KEY_PREFIXES
def trusted_text_for(goal_text: str, requirements: Mapping[str, Any]) -> str:
"""The text a candidate URL/credential is checked against: what the user
actually submitted -- the goal prose plus the requirements form they