From 16eb242469369359899cc9c31a059d2be0562d09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20St=C3=A9ffano?= Date: Wed, 18 Nov 2026 20:34:08 -0300 Subject: [PATCH 1/3] fix(settings): keep stored API keys out of the capabilities response A key stored in config.json was sent to the browser in clear text and filled a plain input. The capabilities response now carries a mask in its place (model lists, stored chat_model and inline_completion_model entries, claude_settings and acp_settings), ConfigHandler maps a posted mask back to the stored key, and the key inputs are password fields. LLMProviderProperty gains an opt-in `secret` flag. Closes #542 --- CHANGELOG.md | 1 + docs/admin-guide.md | 2 +- notebook_intelligence/api.py | 3 + notebook_intelligence/credential_mask.py | 132 ++++++++++ notebook_intelligence/extension.py | 58 +++-- .../litellm_compatible_llm_provider.py | 4 +- .../openai_compatible_llm_provider.py | 4 +- src/components/settings-panel.tsx | 28 ++ tests/test_cell_output_features_response.py | 18 +- tests/test_config_handler_claude_settings.py | 52 ++++ tests/test_credential_mask.py | 246 ++++++++++++++++++ tests/ts/settings-property-field.test.tsx | 83 ++++++ 12 files changed, 604 insertions(+), 27 deletions(-) create mode 100644 notebook_intelligence/credential_mask.py create mode 100644 tests/test_credential_mask.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 93d8147f..373b3709 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ For each release we list user-facing changes grouped as **Added**, **Changed**, ### Fixed +- **Stored API keys no longer reach the browser in clear text.** A key kept in `config.json`, whether typed in Settings or written by hand, used to come back in the capabilities response and fill a plain text input. The OpenAI-compatible and LiteLLM-compatible `api_key` properties (in the model lists and in the stored `chat_model` and `inline_completion_model` entries), `claude_settings.api_key`, and `acp_settings.api_key` are now sent as `********`, and the Settings inputs are password fields. Saving the panel with the mask untouched keeps the stored key; typing a new value replaces it and clearing the field removes it. Extension providers can opt a property in with `LLMProviderProperty(..., secret=True)`. Keys supplied through environment variables were already withheld. - **Chat Markdown images no longer load automatically** (#533). Images in user, model, and tool Markdown now display a visible, translatable blocked-image notice, preserving safe image descriptions. Both local and remote image URLs are blocked to prevent requests that could disclose chat context without a click. Links containing images become non-clickable content so the notice cannot act as a load-image link. Dedicated image results still render; notebook Markdown cells use a separate renderer. See [Chat Markdown images](docs/admin-guide.md#chat-markdown-images). ## [6.3.0] - 2026-10-28 diff --git a/docs/admin-guide.md b/docs/admin-guide.md index 0201a0cf..5254cc3b 100644 --- a/docs/admin-guide.md +++ b/docs/admin-guide.md @@ -190,7 +190,7 @@ Dedicated image results, such as figures delivered through image response events ## API-key handling -By default, custom-provider API keys (Anthropic, OpenAI-compatible, LiteLLM-compatible) are stored plaintext in `~/.jupyter/nbi/config.json`. This is acceptable for single-tenant developer workstations and unacceptable for multi-tenant clusters. +By default, custom-provider API keys (Anthropic, OpenAI-compatible, LiteLLM-compatible) are stored plaintext in `~/.jupyter/nbi/config.json`. The Settings dialog never receives a stored key: the server sends `********` in its place and keeps the stored key when the mask is saved back unchanged. This is acceptable for single-tenant developer workstations and unacceptable for multi-tenant clusters. Recommended approach for clusters: diff --git a/notebook_intelligence/api.py b/notebook_intelligence/api.py index 39d57359..37236d78 100644 --- a/notebook_intelligence/api.py +++ b/notebook_intelligence/api.py @@ -1139,6 +1139,9 @@ class LLMProviderProperty: description: str value: str optional: bool = False + # A credential: the capabilities response masks it and the settings panel + # renders it as a password field. + secret: bool = False def to_dict(self): return asdict(self) diff --git a/notebook_intelligence/credential_mask.py b/notebook_intelligence/credential_mask.py new file mode 100644 index 00000000..b1cbbfcd --- /dev/null +++ b/notebook_intelligence/credential_mask.py @@ -0,0 +1,132 @@ +# Copyright (c) Mehmet Bektas + +"""Keep stored API keys out of the capabilities response. + +A key kept in config.json, whether typed in the settings panel or written by +hand, used to travel to the browser in clear text and fill a plain input. The +capabilities handler now sends ``SECRET_MASK`` in its place, and the config +handler maps the mask posted back onto the stored key. The panel can then +re-save its form untouched without overwriting the key it never saw. +""" + +from __future__ import annotations + +from typing import Any, Optional + +SECRET_MASK = "********" + +# Property ids treated as credentials even when a provider did not set +# ``secret``, so an extension that names its key ``api_key`` cannot leak it by +# omission. +SECRET_PROPERTY_IDS = frozenset({"api_key"}) + + +def _is_secret_property(prop: dict) -> bool: + return bool(prop.get("secret")) or prop.get("id") in SECRET_PROPERTY_IDS + + +def mask_secret(value: Any) -> Any: + """The mask for a non-empty string; anything else unchanged. + + An empty value stays empty so the panel can tell "no key" from "a key". + """ + if isinstance(value, str) and value: + return SECRET_MASK + return value + + +def mask_settings_api_key(settings: Optional[dict]) -> Optional[dict]: + """A copy of a Claude or ACP settings dict with its ``api_key`` masked.""" + if not isinstance(settings, dict) or not settings.get("api_key"): + return settings + result = dict(settings) + result["api_key"] = mask_secret(result["api_key"]) + return result + + +def restore_masked_api_key(posted: Any, stored: Any) -> Any: + """Swap a posted mask for the stored ``api_key``. + + ``stored`` without a key yields an empty one: a mask with nothing behind it + must not be saved as though it were the key. + """ + if not isinstance(posted, dict) or posted.get("api_key") != SECRET_MASK: + return posted + result = dict(posted) + stored_key = stored.get("api_key", "") if isinstance(stored, dict) else "" + result["api_key"] = stored_key if isinstance(stored_key, str) else "" + return result + + +def mask_model_properties(models: list[dict]) -> list[dict]: + """Copies of the model dicts with each secret property's value masked. + + The ``secret`` flag is set on what is sent, so the panel renders a + password field for a property a provider left unflagged. + """ + masked = [] + for model in models: + properties = model.get("properties") + if not isinstance(properties, list): + masked.append(model) + continue + masked.append( + { + **model, + "properties": [ + {**prop, "value": mask_secret(prop.get("value")), "secret": True} + if _is_secret_property(prop) + else prop + for prop in properties + ], + } + ) + return masked + + +def mask_model_config(model_config: Any) -> Any: + """A copy of a stored ``chat_model`` / ``inline_completion_model`` entry + (``{provider, model, properties: [{id, value}]}``) with secret property + values masked. The capabilities response carries this entry as well as the + model list, and both must mask.""" + properties = model_config.get("properties") if isinstance(model_config, dict) else None + if not isinstance(properties, list): + return model_config + return { + **model_config, + "properties": [ + {**prop, "value": mask_secret(prop.get("value"))} + if isinstance(prop, dict) and _is_secret_property(prop) + else prop + for prop in properties + ], + } + + +def restore_masked_model_properties(posted: Any, stored: Any) -> Any: + """Swap posted property masks for the values stored for the same model. + + Only the model the config already names can legitimately hold a key, since + the server loads property values for the selected model alone. A mask for + any other model has no key behind it and becomes empty. + """ + if not isinstance(posted, dict) or not isinstance(posted.get("properties"), list): + return posted + same_model = ( + isinstance(stored, dict) + and stored.get("provider") == posted.get("provider") + and stored.get("model") == posted.get("model") + ) + stored_values = {} + if same_model and isinstance(stored.get("properties"), list): + stored_values = { + prop.get("id"): prop.get("value", "") + for prop in stored["properties"] + if isinstance(prop, dict) + } + properties = [] + for prop in posted["properties"]: + if isinstance(prop, dict) and prop.get("value") == SECRET_MASK: + prop = {**prop, "value": stored_values.get(prop.get("id"), "")} + properties.append(prop) + return {**posted, "properties": properties} diff --git a/notebook_intelligence/extension.py b/notebook_intelligence/extension.py index 93cba2dd..97b2a142 100644 --- a/notebook_intelligence/extension.py +++ b/notebook_intelligence/extension.py @@ -98,6 +98,13 @@ from notebook_intelligence.built_in_toolsets import built_in_toolsets, toolset_description from notebook_intelligence.base_chat_participant import BaseChatParticipant from notebook_intelligence.llm_providers.property_labels import translate_model_properties +from notebook_intelligence.credential_mask import ( + mask_model_config, + mask_model_properties, + mask_settings_api_key, + restore_masked_api_key, + restore_masked_model_properties, +) from notebook_intelligence.util import ThreadSafeWebSocketConnector, get_claude_config_dir, get_jupyter_root_dir, set_jupyter_root_dir, is_builtin_tool_enabled_in_env, is_provider_enabled_in_env, filter_models_by_enabled_providers, VALID_CODING_AGENT_LAUNCHERS, compute_effective_disabled_launchers, validate_coding_agent_launcher_ids, resolve_claude_cli_path, resolve_opencode_cli_path, resolve_pi_cli_path, resolve_copilot_cli_path, resolve_codex_cli_path, safe_anchor_uri, has_dangerous_text_codepoints, split_csv from notebook_intelligence.context_factory import RuleContextFactory from notebook_intelligence.skillset import SKILL_NAME_REGEX @@ -741,14 +748,16 @@ def _build_setting_locks_response(string_overrides: dict) -> dict: def _scrub_credentials_for_wire( settings: dict, string_overrides: dict, api_key_lock: str = "claude_api_key" ) -> dict: - """Strip the api_key from the capabilities response when locked by env. + """Keep the api_key out of the capabilities response. The SDKs read their key env var directly (ANTHROPIC_API_KEY for Claude, OPENAI_API_KEY for the Codex ACP agent); surfacing the value through the frontend would - leak the credential. + leak the credential, so a key locked by env is blanked. A key stored in + config.json is masked instead: the panel shows that one is set, and + ConfigHandler maps the mask posted back onto the stored key. """ if not string_overrides.get(api_key_lock): - return settings + return mask_settings_api_key(settings) result = dict(settings or {}) result["api_key"] = "" return result @@ -871,26 +880,31 @@ def is_provider_enabled(provider_id: str) -> bool: # Filtered by the same predicate as `llm_providers` above: the # model lists walk every registered provider, so without this a # disabled provider kept shipping its models (#431). - "chat_models": translate_model_properties( - trans, - filter_models_by_enabled_providers( - ai_service_manager.chat_model_ids, is_provider_enabled - ), + "chat_models": mask_model_properties( + translate_model_properties( + trans, + filter_models_by_enabled_providers( + ai_service_manager.chat_model_ids, is_provider_enabled + ), + ) ), - "inline_completion_models": translate_model_properties( - trans, - filter_models_by_enabled_providers( - ai_service_manager.inline_completion_model_ids, is_provider_enabled - ), + "inline_completion_models": mask_model_properties( + translate_model_properties( + trans, + filter_models_by_enabled_providers( + ai_service_manager.inline_completion_model_ids, + is_provider_enabled, + ), + ) ), "embedding_models": filter_models_by_enabled_providers( ai_service_manager.embedding_model_ids, is_provider_enabled ), - "chat_model": nbi_config.chat_model, + "chat_model": mask_model_config(nbi_config.chat_model), "chat_model_supports_vision": _resolve_supports_vision( ai_service_manager ), - "inline_completion_model": nbi_config.inline_completion_model, + "inline_completion_model": mask_model_config(nbi_config.inline_completion_model), "embedding_model": nbi_config.embedding_model, "chat_participants": [], "store_github_access_token": nbi_config.store_github_access_token, @@ -1251,6 +1265,9 @@ def post(self): # Re-apply the env override after the user's POST so locked fields # stay pinned; non-locked fields keep the user's value. if key == "chat_model": + value = restore_masked_model_properties( + value, ai_service_manager.nbi_config.get("chat_model") + ) value = apply_string_overrides( value, self.string_overrides, CHAT_MODEL_OVERRIDES ) @@ -1258,6 +1275,9 @@ def post(self): continue has_model_change = True elif key == "inline_completion_model": + value = restore_masked_model_properties( + value, ai_service_manager.nbi_config.get("inline_completion_model") + ) value = apply_string_overrides( value, self.string_overrides, INLINE_COMPLETION_MODEL_OVERRIDES ) @@ -1276,6 +1296,11 @@ def post(self): stored = ai_service_manager.nbi_config.get("claude_settings") if isinstance(stored, dict): value = {**stored, **value} + # The capabilities response masks a stored key; a mask posted + # back means "unchanged", not a new key. + value = restore_masked_api_key( + value, ai_service_manager.nbi_config.get("claude_settings") + ) value = apply_claude_policies(value, self.feature_policies) value = apply_string_overrides( value, self.string_overrides, CLAUDE_SETTINGS_OVERRIDES @@ -1288,6 +1313,9 @@ def post(self): value = dict(value) value["api_key"] = "" elif key == "acp_settings": + value = restore_masked_api_key( + value, ai_service_manager.nbi_config.get("acp_settings") + ) value = apply_acp_policies(value, self.feature_policies) value = apply_string_overrides( value, self.string_overrides, ACP_SETTINGS_OVERRIDES diff --git a/notebook_intelligence/llm_providers/litellm_compatible_llm_provider.py b/notebook_intelligence/llm_providers/litellm_compatible_llm_provider.py index 851d1414..c5545256 100644 --- a/notebook_intelligence/llm_providers/litellm_compatible_llm_provider.py +++ b/notebook_intelligence/llm_providers/litellm_compatible_llm_provider.py @@ -20,7 +20,7 @@ def __init__(self, provider: "LiteLLMCompatibleLLMProvider"): self._properties = [ LLMProviderProperty("model_id", "Model", "Model (must support streaming)", "", False), LLMProviderProperty("base_url", "Base URL", "Base URL", "", False), - LLMProviderProperty("api_key", "API key", "API key", "", True), + LLMProviderProperty("api_key", "API key", "API key", "", True, secret=True), LLMProviderProperty("context_window", "Context window", "Context window length", "", True), ] @@ -120,7 +120,7 @@ def __init__(self, provider: "LiteLLMCompatibleLLMProvider"): self._properties = [ LLMProviderProperty("model_id", "Model", "Model", "", False), LLMProviderProperty("base_url", "Base URL", "Base URL", "", False), - LLMProviderProperty("api_key", "API key", "API key", "", True), + LLMProviderProperty("api_key", "API key", "API key", "", True, secret=True), LLMProviderProperty("context_window", "Context window", "Context window length", "", True), ] diff --git a/notebook_intelligence/llm_providers/openai_compatible_llm_provider.py b/notebook_intelligence/llm_providers/openai_compatible_llm_provider.py index ba2e8f24..a9a517f4 100644 --- a/notebook_intelligence/llm_providers/openai_compatible_llm_provider.py +++ b/notebook_intelligence/llm_providers/openai_compatible_llm_provider.py @@ -33,7 +33,7 @@ def __init__(self, provider: "OpenAICompatibleLLMProvider"): super().__init__(provider) self._provider = provider self._properties = [ - LLMProviderProperty("api_key", "API key", "API key", "", False), + LLMProviderProperty("api_key", "API key", "API key", "", False, secret=True), LLMProviderProperty("model_id", "Model", "Model (must support streaming)", "", False), LLMProviderProperty("base_url", "Base URL", "Base URL", "", True), LLMProviderProperty("context_window", "Context window", "Context window length", "", True), @@ -134,7 +134,7 @@ def __init__(self, provider: "OpenAICompatibleLLMProvider"): super().__init__(provider) self._provider = provider self._properties = [ - LLMProviderProperty("api_key", "API key", "API key", "", False), + LLMProviderProperty("api_key", "API key", "API key", "", False, secret=True), LLMProviderProperty("model_id", "Model", "Model", "", False), LLMProviderProperty("base_url", "Base URL", "Base URL", "", True), LLMProviderProperty("context_window", "Context window", "Context window length", "", True), diff --git a/src/components/settings-panel.tsx b/src/components/settings-panel.tsx index b12263ac..163a4512 100644 --- a/src/components/settings-panel.tsx +++ b/src/components/settings-panel.tsx @@ -117,6 +117,23 @@ function useNbiPolicies() { const OPENAI_COMPATIBLE_CHAT_MODEL_ID = 'openai-compatible-chat-model'; +/** + * What the server sends in place of a stored API key (see + * credential_mask.py). Posting it back leaves the stored key as it is. + */ +const SECRET_MASK = '********'; + +/** + * Select the mask when a key field gains focus, so the first keystroke or + * paste replaces it. Without this, typing appends to the mask and saves + * `********abc` as the key. + */ +function selectMask(event: React.FocusEvent): void { + if (event.target.value === SECRET_MASK) { + event.target.select(); + } +} + /** * A required provider property left empty. The value persists as an empty * string and requests then go out with that field blank, which the endpoint @@ -149,6 +166,7 @@ export function ModelPropertyList(props: { description: string; value: string; optional?: boolean; + secret?: boolean; }[]; providerId: string; modelId: string; @@ -187,6 +205,7 @@ export function ModelPropertyField(props: { description: string; value: string; optional?: boolean; + secret?: boolean; }; inputName: string; onChange: (value: string) => void; @@ -204,6 +223,8 @@ export function ModelPropertyField(props: { setTouched(true)} /> {showError && ( @@ -2243,6 +2265,8 @@ function SettingsPanelComponentClaude(props: any) { setApiKey(event.target.value)} /> @@ -2478,6 +2503,8 @@ function SettingsPanelComponentAcp(props: any) { setApiKey(event.target.value)} /> diff --git a/tests/test_cell_output_features_response.py b/tests/test_cell_output_features_response.py index 21055730..6924b052 100644 --- a/tests/test_cell_output_features_response.py +++ b/tests/test_cell_output_features_response.py @@ -5,6 +5,7 @@ from types import SimpleNamespace +from notebook_intelligence.credential_mask import SECRET_MASK from notebook_intelligence.extension import ( FEATURE_POLICY_NAMES, _build_cell_output_features_response, @@ -271,8 +272,14 @@ def test_response_includes_every_known_lock_name(self): class TestScrubCredentialsForWire: - def test_unlocked_settings_pass_through_unchanged(self): + def test_unlocked_key_is_masked_not_sent(self): settings = {"api_key": "sk-secret", "base_url": "https://x"} + masked = _scrub_credentials_for_wire(settings, {}) + assert masked == {"api_key": SECRET_MASK, "base_url": "https://x"} + assert settings["api_key"] == "sk-secret" + + def test_settings_without_a_key_pass_through_unchanged(self): + settings = {"api_key": "", "base_url": "https://x"} assert _scrub_credentials_for_wire(settings, {}) is settings def test_claude_key_scrubbed_when_env_locks_it(self): @@ -288,12 +295,9 @@ def test_claude_key_scrubbed_when_env_locks_it(self): def test_acp_key_scrubbed_with_its_own_lock_name(self): settings = {"api_key": "sk-openai"} # The Claude lock must not scrub the Codex key. - assert ( - _scrub_credentials_for_wire( - settings, {"claude_api_key": "sk-env"}, "acp_api_key" - ) - is settings - ) + assert _scrub_credentials_for_wire( + settings, {"claude_api_key": "sk-env"}, "acp_api_key" + ) == {"api_key": SECRET_MASK} scrubbed = _scrub_credentials_for_wire( settings, {"acp_api_key": "sk-env"}, "acp_api_key" ) diff --git a/tests/test_config_handler_claude_settings.py b/tests/test_config_handler_claude_settings.py index e4b7c4b7..d66596ea 100644 --- a/tests/test_config_handler_claude_settings.py +++ b/tests/test_config_handler_claude_settings.py @@ -356,3 +356,55 @@ def test_a_malformed_config_file_fails_without_overwriting_it(self, config): with open(config.user_config_file) as f: assert f.read() == '{"claude_settings": {"enabled": true,}}' + + +class TestApiKeyMaskRoundTrip: + """The capabilities response masks a stored key; saving it back must not + overwrite the key with the mask.""" + + def test_claude_key_survives_a_posted_mask(self, config): + from notebook_intelligence.credential_mask import SECRET_MASK + + _store(config, claude_settings={"enabled": True, "api_key": "sk-real"}) + + stored = _post(config, {"claude_settings": {**PANEL_PAYLOAD, "api_key": SECRET_MASK}}) + + assert stored["api_key"] == "sk-real" + + def test_claude_key_can_still_be_replaced_or_cleared(self, config): + _store(config, claude_settings={"api_key": "sk-real"}) + assert _post(config, {"claude_settings": {**PANEL_PAYLOAD, "api_key": "sk-new"}})["api_key"] == "sk-new" + assert _post(config, {"claude_settings": {**PANEL_PAYLOAD, "api_key": ""}})["api_key"] == "" + + def test_acp_key_survives_a_posted_mask(self, config): + from notebook_intelligence.credential_mask import SECRET_MASK + + _store(config, acp_settings={"enabled": False, "api_key": "sk-openai"}) + + _post(config, {"acp_settings": {"enabled": False, "api_key": SECRET_MASK}}) + + assert _on_disk(config)["acp_settings"]["api_key"] == "sk-openai" + + def test_model_property_key_survives_a_posted_mask(self, config): + from notebook_intelligence.credential_mask import SECRET_MASK + + _store(config, chat_model={ + "provider": "openai-compatible", + "model": "openai-compatible-chat-model", + "properties": [ + {"id": "api_key", "value": "sk-real"}, + {"id": "base_url", "value": "https://old"}, + ], + }) + + _post(config, {"chat_model": { + "provider": "openai-compatible", + "model": "openai-compatible-chat-model", + "properties": [ + {"id": "api_key", "value": SECRET_MASK}, + {"id": "base_url", "value": "https://new"}, + ], + }}) + + props = {p["id"]: p["value"] for p in _on_disk(config)["chat_model"]["properties"]} + assert props == {"api_key": "sk-real", "base_url": "https://new"} diff --git a/tests/test_credential_mask.py b/tests/test_credential_mask.py new file mode 100644 index 00000000..8c3b5096 --- /dev/null +++ b/tests/test_credential_mask.py @@ -0,0 +1,246 @@ +# Copyright (c) Mehmet Bektas + +"""A stored API key reaches the browser masked and survives a round trip.""" + +import json + +from notebook_intelligence.api import LLMProviderProperty +from notebook_intelligence.credential_mask import ( + SECRET_MASK, + mask_model_config, + mask_model_properties, + mask_secret, + mask_settings_api_key, + restore_masked_api_key, + restore_masked_model_properties, +) +from notebook_intelligence.llm_providers.litellm_compatible_llm_provider import ( + LiteLLMCompatibleLLMProvider, +) +from notebook_intelligence.llm_providers.openai_compatible_llm_provider import ( + OpenAICompatibleLLMProvider, +) + + +def _model(provider="openai-compatible", model="m", key="sk-real"): + return { + "provider": provider, + "id": model, + "properties": [ + {"id": "api_key", "value": key, "secret": True}, + {"id": "base_url", "value": "https://x", "secret": False}, + ], + } + + +class TestMasking: + def test_mask_secret_keeps_empty_values_empty(self): + assert mask_secret("") == "" + assert mask_secret(None) is None + assert mask_secret("sk-real") == SECRET_MASK + + def test_settings_key_masked_on_a_copy(self): + settings = {"api_key": "sk-real", "enabled": True} + assert mask_settings_api_key(settings) == { + "api_key": SECRET_MASK, + "enabled": True, + } + assert settings["api_key"] == "sk-real" + + def test_only_secret_properties_are_masked(self): + original = _model() + masked = mask_model_properties([original])[0] + values = {p["id"]: p["value"] for p in masked["properties"]} + assert values == {"api_key": SECRET_MASK, "base_url": "https://x"} + assert original["properties"][0]["value"] == "sk-real" + + def test_unflagged_api_key_property_is_masked_and_flagged(self): + model = { + "provider": "ext", + "id": "m", + "properties": [ + {"id": "api_key", "value": "sk-real"}, + {"id": "base_url", "value": "https://x"}, + ], + } + masked = mask_model_properties([model])[0]["properties"] + assert masked[0] == {"id": "api_key", "value": SECRET_MASK, "secret": True} + assert masked[1] == {"id": "base_url", "value": "https://x"} + + def test_blank_secret_property_stays_blank(self): + masked = mask_model_properties([_model(key="")])[0] + assert masked["properties"][0]["value"] == "" + + def test_built_in_api_key_properties_are_flagged_secret(self): + for provider in (OpenAICompatibleLLMProvider(), LiteLLMCompatibleLLMProvider()): + for model in provider.chat_models + provider.inline_completion_models: + assert model.get_property("api_key").secret is True + assert model.get_property("model_id").secret is False + + def test_property_to_dict_carries_the_flag(self): + assert LLMProviderProperty("k", "K", "K", "v", secret=True).to_dict()[ + "secret" + ] is True + + +class TestMaskModelConfig: + def test_masks_secret_properties_of_a_stored_entry(self): + stored = { + "provider": "litellm-compatible", + "model": "m", + "properties": [ + {"id": "api_key", "value": "sk-real"}, + {"id": "base_url", "value": "https://x"}, + ], + } + assert mask_model_config(stored) == { + "provider": "litellm-compatible", + "model": "m", + "properties": [ + {"id": "api_key", "value": SECRET_MASK}, + {"id": "base_url", "value": "https://x"}, + ], + } + assert stored["properties"][0]["value"] == "sk-real" + + def test_entries_without_properties_pass_through(self): + copilot = {"provider": "github-copilot", "model": "gpt-4.1"} + assert mask_model_config(copilot) is copilot + assert mask_model_config(None) is None + + def test_a_real_nbi_config_entry_is_masked(self, tmp_path): + from notebook_intelligence.config import NBIConfig + + config = NBIConfig.__new__(NBIConfig) + config._feature_policies, config._string_overrides = {}, {} + config.env_config = {} + config.user_config = { + "chat_model": { + "provider": "openai-compatible", + "model": "openai-compatible-chat-model", + "properties": [{"id": "api_key", "value": "sk-LEAK"}], + } + } + assert "sk-LEAK" in json.dumps(config.chat_model) + assert "sk-LEAK" not in json.dumps(mask_model_config(config.chat_model)) + + +class TestRestore: + def test_posted_mask_becomes_the_stored_key(self): + assert restore_masked_api_key( + {"api_key": SECRET_MASK, "enabled": False}, {"api_key": "sk-real"} + ) == {"api_key": "sk-real", "enabled": False} + + def test_new_key_is_kept(self): + assert restore_masked_api_key( + {"api_key": "sk-new"}, {"api_key": "sk-real"} + ) == {"api_key": "sk-new"} + + def test_cleared_key_stays_cleared(self): + assert restore_masked_api_key( + {"api_key": ""}, {"api_key": "sk-real"} + ) == {"api_key": ""} + + def test_mask_with_nothing_stored_is_never_saved(self): + assert restore_masked_api_key({"api_key": SECRET_MASK}, None) == { + "api_key": "" + } + + def test_model_mask_restored_for_the_same_model(self): + stored = { + "provider": "openai-compatible", + "model": "m", + "properties": [{"id": "api_key", "value": "sk-real"}], + } + posted = { + "provider": "openai-compatible", + "model": "m", + "properties": [ + {"id": "api_key", "value": SECRET_MASK}, + {"id": "base_url", "value": "https://y"}, + ], + } + restored = restore_masked_model_properties(posted, stored) + assert restored["properties"] == [ + {"id": "api_key", "value": "sk-real"}, + {"id": "base_url", "value": "https://y"}, + ] + + def test_model_mask_for_another_model_is_dropped(self): + stored = { + "provider": "openai-compatible", + "model": "m", + "properties": [{"id": "api_key", "value": "sk-real"}], + } + posted = { + "provider": "litellm-compatible", + "model": "other", + "properties": [{"id": "api_key", "value": SECRET_MASK}], + } + restored = restore_masked_model_properties(posted, stored) + assert restored["properties"] == [{"id": "api_key", "value": ""}] + + def test_model_without_properties_passes_through(self): + posted = {"provider": "github-copilot", "model": "gpt-4.1"} + assert restore_masked_model_properties(posted, None) is posted + + +class TestCapabilitiesResponse: + """GetCapabilitiesHandler.get is where the masks are applied.""" + + def test_no_stored_key_reaches_the_response(self, monkeypatch): + from unittest.mock import MagicMock + + from notebook_intelligence import extension as ext_module + + manager = MagicMock() + manager.chat_model_ids = [_model(key="sk-chat")] + manager.inline_completion_model_ids = [_model(key="sk-inline")] + manager.nbi_config.claude_settings = {"api_key": "sk-claude", "enabled": False} + manager.nbi_config.acp_settings = {"api_key": "sk-acp", "enabled": False} + # The stored model entries travel in the response too, apart from the + # model lists; a mock here would hide a leak through them. + manager.nbi_config.chat_model = { + "provider": "openai-compatible", + "model": "m", + "properties": [{"id": "api_key", "value": "sk-stored-chat"}], + } + manager.nbi_config.inline_completion_model = { + "provider": "openai-compatible", + "model": "m", + "properties": [{"id": "api_key", "value": "sk-stored-inline"}], + } + monkeypatch.setattr(ext_module, "ai_service_manager", manager) + # Capture the dict before it is serialized; most fields are mocks. + monkeypatch.setattr(ext_module.json, "dumps", lambda obj, *a, **k: obj) + handler = MagicMock() + handler.tour_config_path = "" + handler.string_overrides = {} + for name in ( + "disabled_tools", + "disabled_providers", + "disabled_coding_agent_launchers", + "additional_skipped_workspace_directories", + ): + setattr(handler, name, []) + + ext_module.GetCapabilitiesHandler.get(handler) + response = handler.finish.call_args[0][0] + + for models in (response["chat_models"], response["inline_completion_models"]): + key = next(p for p in models[0]["properties"] if p["id"] == "api_key") + assert key["value"] == SECRET_MASK + assert response["claude_settings"]["api_key"] == SECRET_MASK + assert response["acp_settings"]["api_key"] == SECRET_MASK + for stored in (response["chat_model"], response["inline_completion_model"]): + assert stored["properties"] == [{"id": "api_key", "value": SECRET_MASK}] + assert stored["provider"] == "openai-compatible" + for secret in ( + "sk-chat", + "sk-inline", + "sk-claude", + "sk-acp", + "sk-stored-chat", + "sk-stored-inline", + ): + assert secret not in repr(response) diff --git a/tests/ts/settings-property-field.test.tsx b/tests/ts/settings-property-field.test.tsx index b76ce3f5..ee0200da 100644 --- a/tests/ts/settings-property-field.test.tsx +++ b/tests/ts/settings-property-field.test.tsx @@ -112,3 +112,86 @@ describe('ModelPropertyField', () => { ).toBeNull(); }); }); + +describe('ModelPropertyField secret', () => { + it('renders a secret property as a password field', () => { + const { container } = render( + undefined} + /> + ); + + const input = container.querySelector('input') as HTMLInputElement; + expect(input.type).toBe('password'); + expect(input.autocomplete).toBe('new-password'); + }); + + it('keeps a plain property as text', () => { + const { container } = render( + undefined} + /> + ); + + expect((container.querySelector('input') as HTMLInputElement).type).toBe( + 'text' + ); + }); + + it('selects the mask on focus so typing replaces it', () => { + const { container } = render( + undefined} + /> + ); + const input = container.querySelector('input') as HTMLInputElement; + const select = jest.spyOn(input, 'select'); + + fireEvent.focus(input); + + expect(select).toHaveBeenCalledTimes(1); + }); + + it('leaves a typed key alone on focus', () => { + const { container } = render( + undefined} + /> + ); + const input = container.querySelector('input') as HTMLInputElement; + const select = jest.spyOn(input, 'select'); + + fireEvent.focus(input); + + expect(select).not.toHaveBeenCalled(); + }); +}); From 0f5c431cc348f876f130476175003223ca490689 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20St=C3=A9ffano?= Date: Wed, 18 Nov 2026 20:34:08 -0300 Subject: [PATCH 2/3] fix(mcp): keep MCP server credentials out of responses and workspace files The Claude-mode server list masks every env and headers value. The NBI MCP editor masks credential-named values in the temp copy it writes to the workspace and restores them on save; a mask with nothing stored behind it is refused with a 400. Closes #543 --- CHANGELOG.md | 1 + docs/admin-guide.md | 4 + notebook_intelligence/claude_mcp_manager.py | 7 + notebook_intelligence/credential_mask.py | 110 +++++++++++++ notebook_intelligence/extension.py | 32 +++- .../locale/notebook_intelligence.pot | 4 + tests/test_credential_surfaces.py | 148 ++++++++++++++++++ 7 files changed, 301 insertions(+), 5 deletions(-) create mode 100644 tests/test_credential_surfaces.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 373b3709..1355ae81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ For each release we list user-facing changes grouped as **Added**, **Changed**, ### Fixed - **Stored API keys no longer reach the browser in clear text.** A key kept in `config.json`, whether typed in Settings or written by hand, used to come back in the capabilities response and fill a plain text input. The OpenAI-compatible and LiteLLM-compatible `api_key` properties (in the model lists and in the stored `chat_model` and `inline_completion_model` entries), `claude_settings.api_key`, and `acp_settings.api_key` are now sent as `********`, and the Settings inputs are password fields. Saving the panel with the mask untouched keeps the stored key; typing a new value replaces it and clearing the field removes it. Extension providers can opt a property in with `LLMProviderProperty(..., secret=True)`. Keys supplied through environment variables were already withheld. +- **MCP credentials no longer reach the browser or the workspace in clear text.** The Claude MCP panel's server list now masks every `env` and `headers` value, which it never displayed. The MCP config editor writes a temporary copy of `mcp.json` into the workspace; `env` and `headers` values whose names look like credentials (`Authorization`, `*_TOKEN`, `X-Api-Key`, and similar) are now masked in that copy, and a mask saved back keeps the stored value. A mask with no stored value behind it, such as a renamed server, is refused with a 400 instead of being saved. Values in `args` and `url` are not masked. - **Chat Markdown images no longer load automatically** (#533). Images in user, model, and tool Markdown now display a visible, translatable blocked-image notice, preserving safe image descriptions. Both local and remote image URLs are blocked to prevent requests that could disclose chat context without a click. Links containing images become non-clickable content so the notice cannot act as a load-image link. Dedicated image results still render; notebook Markdown cells use a separate renderer. See [Chat Markdown images](docs/admin-guide.md#chat-markdown-images). ## [6.3.0] - 2026-10-28 diff --git a/docs/admin-guide.md b/docs/admin-guide.md index 5254cc3b..5735b5dc 100644 --- a/docs/admin-guide.md +++ b/docs/admin-guide.md @@ -200,6 +200,10 @@ Recommended approach for clusters: `${ENV_VAR}`-style interpolation inside `config.json` is not currently supported. Tracked as a feature request. +### MCP server credentials + +Tokens in an MCP server's `env` or `headers` are stored in plaintext: in `mcp.json` for NBI's own MCP Servers tab, and in Claude's own config (`~/.claude.json` or `.mcp.json`) for the Claude-mode MCP Servers tab. NBI never sends those values to the browser in clear text. The Claude-mode server list masks them, and the `mcp.json` editor masks credential-named values and keeps the stored value when the mask is saved back. + --- ## Self-hosted LLM endpoints diff --git a/notebook_intelligence/claude_mcp_manager.py b/notebook_intelligence/claude_mcp_manager.py index 5818eaa2..7da8ca93 100644 --- a/notebook_intelligence/claude_mcp_manager.py +++ b/notebook_intelligence/claude_mcp_manager.py @@ -29,6 +29,8 @@ from pathlib import Path from typing import Iterable, Literal, Optional +from notebook_intelligence.credential_mask import mask_mcp_server_credentials + from notebook_intelligence._claude_cli import ( CLAUDE_SCOPES, reject_flag_smuggling, @@ -78,6 +80,11 @@ def to_dict(self) -> dict: "disabled_for_workspace": self.disabled_for_workspace, } + def to_wire_dict(self) -> dict: + """``to_dict`` for the browser: ``env`` and ``headers`` values are + masked, since they hold tokens and the panel never shows them.""" + return mask_mcp_server_credentials(self.to_dict()) + def _read_json(path: Path) -> Optional[dict]: try: diff --git a/notebook_intelligence/credential_mask.py b/notebook_intelligence/credential_mask.py index b1cbbfcd..8bcdb58e 100644 --- a/notebook_intelligence/credential_mask.py +++ b/notebook_intelligence/credential_mask.py @@ -11,8 +11,11 @@ from __future__ import annotations +import copy from typing import Any, Optional +from notebook_intelligence.util import _SECRET_ENV_NAME_PATTERNS + SECRET_MASK = "********" # Property ids treated as credentials even when a provider did not set @@ -130,3 +133,110 @@ def restore_masked_model_properties(posted: Any, stored: Any) -> Any: prop = {**prop, "value": stored_values.get(prop.get("id"), "")} properties.append(prop) return {**posted, "properties": properties} + + +# MCP server entries carry credentials in two maps: stdio ``env`` and remote +# ``headers``. Names are matched case-insensitively with ``-`` read as ``_``, +# using the shell-output redactor's list plus the bare words an MCP config +# tends to use (``X-Api-Key``, ``MY_KEY``). +_MCP_SECRET_NAME_PATTERNS = (*_SECRET_ENV_NAME_PATTERNS, "KEY", "PASS") +_MCP_CREDENTIAL_FIELDS = ("env", "headers") + + +def _is_secret_name(name: Any) -> bool: + upper = str(name).upper().replace("-", "_") + return any(pattern in upper for pattern in _MCP_SECRET_NAME_PATTERNS) + + +def _mcp_servers(config: Any) -> dict: + servers = config.get("mcpServers") if isinstance(config, dict) else None + return servers if isinstance(servers, dict) else {} + + +def mask_mcp_server_credentials(server: dict) -> dict: + """A copy of a Claude MCP server dict with every ``env`` and ``headers`` + value masked. The panel lists names only, so nothing there needs a value.""" + result = dict(server) + for field in _MCP_CREDENTIAL_FIELDS: + values = result.get(field) + if isinstance(values, dict): + result[field] = {key: mask_secret(value) for key, value in values.items()} + return result + + +def mask_mcp_config(config: Any) -> Any: + """A copy of an ``mcp.json`` document with credential-named ``env`` and + ``headers`` values masked, for the editor that shows the file. + + Only names that look like credentials are masked, so settings such as + ``PYTHONPATH`` stay editable. ``args`` and ``url`` are left as they are: + a secret inside one cannot be told from the rest of the string. + """ + result = copy.deepcopy(config) + for server in _mcp_servers(result).values(): + if not isinstance(server, dict): + continue + for field in _MCP_CREDENTIAL_FIELDS: + values = server.get(field) + if isinstance(values, dict): + for key, value in values.items(): + if _is_secret_name(key): + values[key] = mask_secret(value) + return result + + +def has_masked_mcp_values(config: Any) -> bool: + """Whether a posted ``mcp.json`` carries any mask to restore.""" + return any( + isinstance(server, dict) + and any( + isinstance(server.get(field), dict) + and SECRET_MASK in server[field].values() + for field in _MCP_CREDENTIAL_FIELDS + ) + for server in _mcp_servers(config).values() + ) + + +class UnresolvedMaskError(ValueError): + """A posted mask has no stored value behind it. ``names`` lists each as + ``server.field.key`` so the handler can word the message in the user's + language.""" + + def __init__(self, names: list): + super().__init__( + "Masked MCP values with no saved value: " + ", ".join(names) + ) + self.names = list(names) + + +def restore_masked_mcp_config(posted: Any, stored: Any) -> Any: + """Swap masks in a posted ``mcp.json`` for the stored values. + + Raises ``UnresolvedMaskError`` naming each mask with no stored value + behind it (a renamed server, or a key added as the mask), so the save is + refused instead of persisting ``********`` as a credential. + """ + result = copy.deepcopy(posted) + stored_servers = _mcp_servers(stored) + unresolved = [] + for name, server in _mcp_servers(result).items(): + if not isinstance(server, dict): + continue + for field in _MCP_CREDENTIAL_FIELDS: + values = server.get(field) + if not isinstance(values, dict): + continue + stored_values = (stored_servers.get(name) or {}).get(field) + for key, value in values.items(): + if value != SECRET_MASK: + continue + if isinstance(stored_values, dict) and isinstance( + stored_values.get(key), str + ): + values[key] = stored_values[key] + else: + unresolved.append(f"{name}.{field}.{key}") + if unresolved: + raise UnresolvedMaskError(unresolved) + return result diff --git a/notebook_intelligence/extension.py b/notebook_intelligence/extension.py index 97b2a142..80818eec 100644 --- a/notebook_intelligence/extension.py +++ b/notebook_intelligence/extension.py @@ -99,9 +99,13 @@ from notebook_intelligence.base_chat_participant import BaseChatParticipant from notebook_intelligence.llm_providers.property_labels import translate_model_properties from notebook_intelligence.credential_mask import ( + UnresolvedMaskError, + has_masked_mcp_values, + mask_mcp_config, mask_model_config, mask_model_properties, mask_settings_api_key, + restore_masked_mcp_config, restore_masked_api_key, restore_masked_model_properties, ) @@ -1469,7 +1473,10 @@ def get(self): mcp_config = ai_service_manager.nbi_config.mcp.copy() if "mcpServers" not in mcp_config: mcp_config["mcpServers"] = {} - self.finish(json.dumps(mcp_config)) + # The editor writes this to a file in the workspace, where the agent's + # file tools and a sync client can reach it, so credential values are + # masked here and restored when the edited file is posted back. + self.finish(json.dumps(mask_mcp_config(mcp_config))) @tornado.web.authenticated def post(self): @@ -1498,6 +1505,9 @@ def post(self): # on every restart. Apply the same env-key denylist that # blocks PATH / LD_PRELOAD / etc. bypasses. allowlist = ai_service_manager.get_mcp_stdio_command_allowlist() + # Same source as the GET above, so a mask maps to what was shown. + if has_masked_mcp_values(data): + data = restore_masked_mcp_config(data, ai_service_manager.nbi_config.mcp) servers = data.get("mcpServers") if isinstance(data, dict) else None if isinstance(servers, dict): for name, server in servers.items(): @@ -1510,6 +1520,18 @@ def post(self): ai_service_manager.nbi_config.load() ai_service_manager.update_mcp_servers() self.finish(json.dumps({"status": "ok"})) + except UnresolvedMaskError as exc: + # A masked value with nothing saved behind it: refused, not saved. + trans = i18n.request_bundle(self) + self.set_status(400) + self.finish(json.dumps({ + "status": "error", + "message": trans.__( + "These MCP values are masked and have no saved value to keep: %1. Enter the real value.", + ", ".join(exc.names), + ), + })) + return except ValueError as exc: # Policy rejection: surface as HTTP 400 so the Settings UI # shows the operator's policy message instead of a generic @@ -1794,7 +1816,7 @@ class ClaudeMCPListHandler(ClaudeMCPBaseHandler): @tornado.web.authenticated def get(self): try: - servers = [s.to_dict() for s in self.manager.list_servers()] + servers = [s.to_wire_dict() for s in self.manager.list_servers()] except (FileNotFoundError, TimeoutError, ValueError) as e: self._error(e) return @@ -1815,7 +1837,7 @@ async def post(self): env=data.get("env"), headers=data.get("headers"), ) - self.finish(json.dumps({"server": srv.to_dict()})) + self.finish(json.dumps({"server": srv.to_wire_dict()})) except (FileNotFoundError, TimeoutError, ValueError) as e: self._error(e) @@ -1831,7 +1853,7 @@ def get(self, scope, name): "error": trans.__("MCP server '%1' not found in %2 scope", name, scope) })) return - self.finish(json.dumps({"server": srv.to_dict()})) + self.finish(json.dumps({"server": srv.to_wire_dict()})) @tornado.web.authenticated async def delete(self, scope, name): @@ -1870,7 +1892,7 @@ async def patch(self, scope, name): return try: srv = await self.manager.set_server_disabled(name=name, disabled=raw) - self.finish(json.dumps({"server": srv.to_dict()})) + self.finish(json.dumps({"server": srv.to_wire_dict()})) except (FileNotFoundError, TimeoutError, ValueError) as e: self._error(e) diff --git a/notebook_intelligence/locale/notebook_intelligence.pot b/notebook_intelligence/locale/notebook_intelligence.pot index 24c00923..19419cb3 100644 --- a/notebook_intelligence/locale/notebook_intelligence.pot +++ b/notebook_intelligence/locale/notebook_intelligence.pot @@ -2816,6 +2816,10 @@ msgstr "" msgid "The turn ended in an error." msgstr "" +#: notebook_intelligence/extension.py +msgid "These MCP values are masked and have no saved value to keep: %1. Enter the real value." +msgstr "" + #: src/components/claude-mcp-panel.tsx msgid "These are Claude Code's own MCP servers (read from %1, project %2, and the per-user-per-project block). Independent of the NBI MCP Servers tab." msgstr "" diff --git a/tests/test_credential_surfaces.py b/tests/test_credential_surfaces.py new file mode 100644 index 00000000..ee02f28b --- /dev/null +++ b/tests/test_credential_surfaces.py @@ -0,0 +1,148 @@ +# Copyright (c) Mehmet Bektas + +"""MCP credentials must not reach the browser or the workspace in clear text.""" + +import json +from types import SimpleNamespace +from unittest.mock import MagicMock + +import pytest + +from notebook_intelligence import extension as ext_module +from notebook_intelligence.claude_mcp_manager import ClaudeMCPServer +from notebook_intelligence.credential_mask import ( + SECRET_MASK, + UnresolvedMaskError, + mask_mcp_config, + restore_masked_mcp_config, +) + +MCP = { + "mcpServers": { + "remote": { + "url": "https://mcp.example", + "headers": {"Authorization": "Bearer tok-abcdef123456", "X-Tenant": "acme"}, + }, + "local": { + "command": "uvx", + "env": {"GITHUB_TOKEN": "ghp-local-secret-1", "LOG_LEVEL": "verbose-debug"}, + }, + } +} + + +class TestClaudeMcpWire: + def _server(self): + return ClaudeMCPServer( + name="x", + scope="user", + transport="http", + url="https://m", + headers={"Authorization": "Bearer abc123", "X-Tenant": "acme"}, + env={"API_KEY": "k-secret-value"}, + ) + + def test_wire_dict_masks_every_env_and_header_value(self): + wire = self._server().to_wire_dict() + assert wire["headers"] == {"Authorization": SECRET_MASK, "X-Tenant": SECRET_MASK} + assert wire["env"] == {"API_KEY": SECRET_MASK} + assert wire["url"] == "https://m" + assert "abc123" not in json.dumps(wire) + + def test_internal_dict_keeps_the_values(self): + assert self._server().to_dict()["headers"]["Authorization"] == "Bearer abc123" + + +class TestMcpEditorMask: + def test_only_credential_named_values_are_masked(self): + masked = mask_mcp_config(MCP)["mcpServers"] + assert masked["remote"]["headers"] == { + "Authorization": SECRET_MASK, + "X-Tenant": "acme", + } + assert masked["local"]["env"] == { + "GITHUB_TOKEN": SECRET_MASK, + "LOG_LEVEL": "verbose-debug", + } + assert MCP["mcpServers"]["local"]["env"]["GITHUB_TOKEN"] == "ghp-local-secret-1" + + def test_masked_document_round_trips_to_the_original(self): + assert restore_masked_mcp_config(mask_mcp_config(MCP), MCP) == MCP + + def test_an_edited_value_replaces_the_stored_one(self): + posted = mask_mcp_config(MCP) + posted["mcpServers"]["local"]["env"]["GITHUB_TOKEN"] = "ghp-new-value-22" + restored = restore_masked_mcp_config(posted, MCP) + assert restored["mcpServers"]["local"]["env"]["GITHUB_TOKEN"] == "ghp-new-value-22" + + def test_a_mask_with_nothing_stored_is_refused(self): + posted = mask_mcp_config(MCP) + posted["mcpServers"]["renamed"] = posted["mcpServers"].pop("remote") + with pytest.raises(UnresolvedMaskError, match=r"renamed\.headers\.Authorization") as caught: + restore_masked_mcp_config(posted, MCP) + assert caught.value.names == ["renamed.headers.Authorization"] + + def test_non_server_documents_pass_through(self): + assert mask_mcp_config({}) == {} + assert restore_masked_mcp_config({"mcpServers": {}}, None) == {"mcpServers": {}} + + +class FakeConfig: + def __init__(self, mcp): + self.mcp = mcp + self.user_mcp = None + self.saved = False + + def load(self): + pass + + def save(self): + self.saved = True + + +def _handler(): + handler = MagicMock(spec=ext_module.MCPConfigFileHandler) + handler.request = MagicMock() + return handler + + +class TestMcpConfigHandler: + @pytest.fixture + def config(self, monkeypatch): + config = FakeConfig(json.loads(json.dumps(MCP))) + manager = SimpleNamespace( + nbi_config=config, + get_mcp_stdio_command_allowlist=lambda: [], + update_mcp_servers=lambda: None, + ) + monkeypatch.setattr(ext_module, "ai_service_manager", manager) + return config + + def test_get_sends_no_credential(self, config): + handler = _handler() + ext_module.MCPConfigFileHandler.get.__wrapped__(handler) + body = handler.finish.call_args[0][0] + assert "tok-abcdef123456" not in body + assert "ghp-local-secret-1" not in body + assert "acme" in body + + def test_saving_the_masked_document_keeps_the_stored_values(self, config): + handler = _handler() + handler.request.body = json.dumps(mask_mcp_config(MCP)).encode() + ext_module.MCPConfigFileHandler.post.__wrapped__(handler) + assert config.saved + assert config.user_mcp == MCP + + def test_an_unresolvable_mask_is_a_400_and_nothing_is_saved(self, config): + posted = mask_mcp_config(MCP) + posted["mcpServers"]["renamed"] = posted["mcpServers"].pop("remote") + handler = _handler() + handler.request.body = json.dumps(posted).encode() + ext_module.MCPConfigFileHandler.post.__wrapped__(handler) + handler.set_status.assert_called_with(400) + message = json.loads(handler.finish.call_args[0][0])["message"] + assert "renamed.headers.Authorization" in message + assert "Enter the real value" in message + assert not config.saved + + From ff4a3d273734b2198c011cd118f3696869a2ca39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20St=C3=A9ffano?= Date: Wed, 18 Nov 2026 20:34:08 -0300 Subject: [PATCH 3/3] fix(claude): scrub credentials from CLI and Claude-mode error text run_claude_cli scrubs the env and argv values it was given, then runs the shared redactor. Claude-mode stream errors, the bail-out message and the related log lines are redacted too. Closes #544 --- CHANGELOG.md | 1 + docs/admin-guide.md | 2 + notebook_intelligence/_claude_cli.py | 30 ++++- notebook_intelligence/claude.py | 23 ++-- notebook_intelligence/claude_mcp_manager.py | 19 ++- notebook_intelligence/credential_mask.py | 9 ++ tests/test_credential_surfaces.py | 125 +++++++++++++++++++- 7 files changed, 192 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1355ae81..8c6ecfe3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ For each release we list user-facing changes grouped as **Added**, **Changed**, - **Stored API keys no longer reach the browser in clear text.** A key kept in `config.json`, whether typed in Settings or written by hand, used to come back in the capabilities response and fill a plain text input. The OpenAI-compatible and LiteLLM-compatible `api_key` properties (in the model lists and in the stored `chat_model` and `inline_completion_model` entries), `claude_settings.api_key`, and `acp_settings.api_key` are now sent as `********`, and the Settings inputs are password fields. Saving the panel with the mask untouched keeps the stored key; typing a new value replaces it and clearing the field removes it. Extension providers can opt a property in with `LLMProviderProperty(..., secret=True)`. Keys supplied through environment variables were already withheld. - **MCP credentials no longer reach the browser or the workspace in clear text.** The Claude MCP panel's server list now masks every `env` and `headers` value, which it never displayed. The MCP config editor writes a temporary copy of `mcp.json` into the workspace; `env` and `headers` values whose names look like credentials (`Authorization`, `*_TOKEN`, `X-Api-Key`, and similar) are now masked in that copy, and a mask saved back keeps the stored value. A mask with no stored value behind it, such as a renamed server, is refused with a 400 instead of being saved. Values in `args` and `url` are not masked. +- **Claude CLI and Claude-mode error text no longer echoes credentials.** `claude mcp add` takes `env` and header values only as command-line flags, and the CLI's error output is forwarded to the browser, so those values are now scrubbed from it. The same scrub applies to Claude-mode stream errors shown in chat and in the inline editor. Passing a token to `claude mcp add` still exposes it in the process list while the CLI runs; the admin guide's new MCP server credentials section describes the trade-off. - **Chat Markdown images no longer load automatically** (#533). Images in user, model, and tool Markdown now display a visible, translatable blocked-image notice, preserving safe image descriptions. Both local and remote image URLs are blocked to prevent requests that could disclose chat context without a click. Links containing images become non-clickable content so the notice cannot act as a load-image link. Dedicated image results still render; notebook Markdown cells use a separate renderer. See [Chat Markdown images](docs/admin-guide.md#chat-markdown-images). ## [6.3.0] - 2026-10-28 diff --git a/docs/admin-guide.md b/docs/admin-guide.md index 5735b5dc..774349ab 100644 --- a/docs/admin-guide.md +++ b/docs/admin-guide.md @@ -204,6 +204,8 @@ Recommended approach for clusters: Tokens in an MCP server's `env` or `headers` are stored in plaintext: in `mcp.json` for NBI's own MCP Servers tab, and in Claude's own config (`~/.claude.json` or `.mcp.json`) for the Claude-mode MCP Servers tab. NBI never sends those values to the browser in clear text. The Claude-mode server list masks them, and the `mcp.json` editor masks credential-named values and keeps the stored value when the mask is saved back. +The Claude-mode **Add MCP server** form hands values to `claude mcp add` as command-line flags, because the CLI has no other input for them. For the second or so the CLI runs, another local user on the same machine can read them from the process list. NBI scrubs those values from the CLI's error output. On a shared machine, avoid typing a real token into that form. Instead, add the server without the secret and supply it another way, for example with a `${VAR}` reference in the value. Check that your Claude CLI version expands `${VAR}` in MCP `env` and `headers` before relying on it. + --- ## Self-hosted LLM endpoints diff --git a/notebook_intelligence/_claude_cli.py b/notebook_intelligence/_claude_cli.py index 8ad775e0..68ba945e 100644 --- a/notebook_intelligence/_claude_cli.py +++ b/notebook_intelligence/_claude_cli.py @@ -17,9 +17,13 @@ import asyncio import logging import os -from typing import Optional +from typing import Iterable, Optional -from notebook_intelligence.util import resolve_claude_cli_path +from notebook_intelligence.util import ( + _replace_values, + redact_env_secrets, + resolve_claude_cli_path, +) log = logging.getLogger(__name__) @@ -102,6 +106,7 @@ async def run_claude_cli( timeout: float = CLI_TIMEOUT_DEFAULT_SECONDS, label: str = "claude", env_overrides: Optional[dict[str, str]] = None, + secret_args: Optional[Iterable[str]] = None, ) -> str: """Run ``claude `` and return its stdout. @@ -111,6 +116,11 @@ async def run_claude_cli( Values pass via env, not argv, so they don't appear in the redacted DEBUG log. + ``secret_args`` are values that have to travel in argv (``claude mcp add`` + takes env and header values only as flags). The CLI's error text is + scrubbed of them before it is raised, since the handlers forward it to the + browser. + Raises: FileNotFoundError: when the CLI can't be resolved. TimeoutError: when the call exceeds ``timeout``. @@ -148,10 +158,24 @@ async def run_claude_cli( # The CLI's stderr is forwarded to the browser via the handlers' # `_error()`; scrub any injected secret that the underlying tool # might have echoed (git in verbose mode, curl 401 challenges, etc.). - raise ValueError(_scrub_env_overrides(raw, env_overrides)) + raise ValueError(_scrub_error_text(raw, env_overrides, secret_args)) return out +def _scrub_error_text( + text: str, + env_overrides: Optional[dict[str, str]], + secret_args: Optional[Iterable[str]], +) -> str: + """Scrub CLI error text of every credential this module knows about: + injected env values, secret argv values, then the shared redactor (process + env, stored credentials, known token prefixes).""" + out = _scrub_env_overrides(text, env_overrides) + if secret_args: + out = _replace_values(out, secret_args) + return redact_env_secrets(out) + + def _scrub_env_overrides(text: str, env_overrides: Optional[dict[str, str]]) -> str: """Replace any env_overrides values that appear in `text` with ``. diff --git a/notebook_intelligence/claude.py b/notebook_intelligence/claude.py index 769d3361..5bdf8dda 100644 --- a/notebook_intelligence/claude.py +++ b/notebook_intelligence/claude.py @@ -31,7 +31,7 @@ import logging from claude_agent_sdk import AssistantMessage, PermissionResultAllow, PermissionResultDeny, ResultMessage, SdkMcpTool, TextBlock, ToolResultBlock, ToolUseBlock, UserMessage, create_sdk_mcp_server, ClaudeAgentOptions, ClaudeSDKClient, tool -from notebook_intelligence.util import ThreadSafeWebSocketConnector, _emit, get_jupyter_root_dir, import_litellm, resolve_claude_cli_path, safe_jupyter_path, terminate_process_tree +from notebook_intelligence.util import ThreadSafeWebSocketConnector, _emit, get_jupyter_root_dir, import_litellm, redact_env_secrets, resolve_claude_cli_path, safe_jupyter_path, terminate_process_tree from notebook_intelligence.inline_completion import ( extract_inline_completion, inline_completion_system_prompt, @@ -1343,14 +1343,17 @@ def completions(self, messages: list[dict], tools: list[dict] = None, response: # detect the failure and skip writing the partial buffer to the # user's cell. Re-raise so the caller logs and runs finish(). if response is not None: + # Scrubbed: the text goes to the browser, and SDK and proxy + # errors can echo part of the credential they rejected. + error_text = redact_env_secrets(str(e)) response.stream({ "choices": [{ "delta": { "role": "assistant", - "content": f"\n\n[Stream interrupted: {e}]" + "content": f"\n\n[Stream interrupted: {error_text}]" } }], - "nbi_stream_error": str(e) + "nbi_stream_error": error_text }) raise @@ -1563,7 +1566,7 @@ def _start_worker_thread(self) -> bool: return True except Exception as e: self._client_thread = None - log.error(f"Error occurred while connecting to Claude agent client: {str(e)}") + log.error(f"Error occurred while connecting to Claude agent client: {redact_env_secrets(str(e))}") self._set_status(ClaudeAgentClientStatus.FailedToConnect) self._connect_resolved.set() return False @@ -1879,12 +1882,12 @@ def _tok(key: str) -> int: )) in_flight_tools.clear() except Exception as e: - err_msg = f"Error communicating with Claude agent: {str(e)}" - log.error(err_msg) + error_text = redact_env_secrets(str(e)) + log.error(f"Error communicating with Claude agent: {error_text}") if not self._reconnect_required: trans = i18n.response_bundle(response) response.stream(MarkdownData( - trans.__("Error communicating with Claude agent: %1", str(e)) + trans.__("Error communicating with Claude agent: %1", error_text) )) finally: _emit(signal, {"id": event_id, "data": "query completed"}) @@ -2910,11 +2913,11 @@ async def handle_chat_request(self, request: ChatRequest, response: ChatResponse # worker thread died. Surface it so the user sees why instead of a # silent spinner stop. if isinstance(result, str) and result: - response.stream(MarkdownData(trans.__("**Claude agent error:** %1", result))) + response.stream(MarkdownData(trans.__("**Claude agent error:** %1", redact_env_secrets(result)))) except Exception as e: - log.error(f"Error while handling Claude chat request: {e}", exc_info=True) + log.error(f"Error while handling Claude chat request: {redact_env_secrets(str(e))}", exc_info=True) try: - response.stream(MarkdownData(trans.__("**Error:** %1", e))) + response.stream(MarkdownData(trans.__("**Error:** %1", redact_env_secrets(str(e))))) except Exception as stream_err: log.debug(f"Could not stream error to client (likely closed websocket): {stream_err}") finally: diff --git a/notebook_intelligence/claude_mcp_manager.py b/notebook_intelligence/claude_mcp_manager.py index 7da8ca93..73131775 100644 --- a/notebook_intelligence/claude_mcp_manager.py +++ b/notebook_intelligence/claude_mcp_manager.py @@ -29,7 +29,10 @@ from pathlib import Path from typing import Iterable, Literal, Optional -from notebook_intelligence.credential_mask import mask_mcp_server_credentials +from notebook_intelligence.credential_mask import ( + credential_parts, + mask_mcp_server_credentials, +) from notebook_intelligence._claude_cli import ( CLAUDE_SCOPES, @@ -284,8 +287,15 @@ async def add_server( tail.append("--") tail.extend(args) + # env and header values can only travel as flags, so the CLI's error + # text, which the handlers forward to the browser, is scrubbed of them. + secret_args = [ + part + for value in [*(env or {}).values(), *(headers or {}).values()] + for part in credential_parts(value) + ] async with self._write_lock: - await self._run_cli(tail) + await self._run_cli(tail, secret_args=secret_args) srv = self.get_server(name, scope) if srv is None: # Surfaced if Claude wrote elsewhere or our reads missed it. The CLI @@ -390,10 +400,13 @@ async def remove_server(self, name: str, scope: ClaudeMCPScope) -> None: # --- internals ----------------------------------------------------- - async def _run_cli(self, tail: list[str]) -> str: + async def _run_cli( + self, tail: list[str], secret_args: Optional[list[str]] = None + ) -> str: return await run_claude_cli( tail, cwd=str(self._working_dir), timeout=CLI_TIMEOUT_SECONDS, label="claude mcp", + secret_args=secret_args, ) diff --git a/notebook_intelligence/credential_mask.py b/notebook_intelligence/credential_mask.py index 8bcdb58e..efb78f4e 100644 --- a/notebook_intelligence/credential_mask.py +++ b/notebook_intelligence/credential_mask.py @@ -240,3 +240,12 @@ def restore_masked_mcp_config(posted: Any, stored: Any) -> Any: if unresolved: raise UnresolvedMaskError(unresolved) return result + + +def credential_parts(value: str) -> list: + """The value, plus the token of a ``Bearer abc`` style header value.""" + parts = [value] + scheme, _, token = value.partition(" ") + if token and scheme.isalpha(): + parts.append(token.strip()) + return parts diff --git a/tests/test_credential_surfaces.py b/tests/test_credential_surfaces.py index ee02f28b..ae70b25c 100644 --- a/tests/test_credential_surfaces.py +++ b/tests/test_credential_surfaces.py @@ -1,7 +1,9 @@ # Copyright (c) Mehmet Bektas -"""MCP credentials must not reach the browser or the workspace in clear text.""" +"""MCP credentials, and the CLI and Claude-mode error text that may echo them, +must not reach the browser or the workspace in clear text.""" +import asyncio import json from types import SimpleNamespace from unittest.mock import MagicMock @@ -146,3 +148,124 @@ def test_an_unresolvable_mask_is_a_400_and_nothing_is_saved(self, config): assert not config.saved +class TestCliErrorScrubbing: + """`claude mcp add` takes env and header values as flags, and the + handlers forward the CLI's error text to the browser.""" + + def _manager(self, tmp_path, monkeypatch, stderr): + from pathlib import Path + + from notebook_intelligence.claude_mcp_manager import ClaudeMCPManager + + home = tmp_path / "home" + home.mkdir() + monkeypatch.setattr(Path, "home", classmethod(lambda cls: home)) + monkeypatch.delenv("CLAUDE_CONFIG_DIR", raising=False) + monkeypatch.setattr( + "notebook_intelligence._claude_cli.resolve_claude_cli_path", + lambda: "/usr/local/bin/claude", + ) + + async def fake_subprocess(*argv, **kwargs): + proc = MagicMock() + + async def communicate(): + return (b"", stderr) + + proc.communicate = communicate + proc.returncode = 1 + return proc + + monkeypatch.setattr(asyncio, "create_subprocess_exec", fake_subprocess) + project = tmp_path / "project" + project.mkdir() + return ClaudeMCPManager(working_dir=str(project)) + + def test_header_and_env_values_are_scrubbed_from_the_error( + self, tmp_path, monkeypatch + ): + stderr = ( + b"error: invalid header 'Authorization: Bearer tok-abcdef123456'; " + b"env GITHUB_TOKEN=ghp-local-secret-1 rejected" + ) + manager = self._manager(tmp_path, monkeypatch, stderr) + with pytest.raises(ValueError) as caught: + asyncio.run( + manager.add_server( + name="srv", + scope="user", + transport="http", + command_or_url="https://mcp.example", + headers={"Authorization": "Bearer tok-abcdef123456"}, + env={"GITHUB_TOKEN": "ghp-local-secret-1"}, + ) + ) + message = str(caught.value) + assert "tok-abcdef123456" not in message + assert "ghp-local-secret-1" not in message + assert "invalid header" in message + + +class TestClaudeStreamErrorScrubbing: + def test_inline_stream_error_marker_is_scrubbed(self, monkeypatch): + from notebook_intelligence.claude import ClaudeChatModel + + monkeypatch.setenv("PROXY_API_KEY", "proxy-master-key-0042") + model = ClaudeChatModel.__new__(ClaudeChatModel) + model._model_id = "claude-sonnet-test" + model._model_name = "Claude Sonnet (test)" + model._context_window = 200000 + model._supports_tools = True + model._client = MagicMock() + boom = RuntimeError("401 Received API Key = proxy-master-key-0042") + model._client.messages.stream = MagicMock(side_effect=boom) + response = MagicMock() + + with pytest.raises(RuntimeError): + model.completions( + messages=[{"role": "user", "content": "x"}], response=response + ) + + payload = response.stream.call_args_list[-1].args[0] + assert "proxy-master-key-0042" not in repr(payload) + assert "Stream interrupted" in payload["choices"][0]["delta"]["content"] + assert "proxy-master-key-0042" not in payload["nbi_stream_error"] + + +class TestClaudeChatErrorScrubbing: + def _stream(self, participant): + from notebook_intelligence.api import ChatResponse, MarkdownData + from notebook_intelligence import i18n + from tests.test_claude_client import _make_chat_request + + response = MagicMock(spec=ChatResponse, trans=i18n.english()) + asyncio.run(participant.handle_chat_request(_make_chat_request(), response)) + return [ + call.args[0].content + for call in response.stream.call_args_list + if isinstance(call.args[0], MarkdownData) + ] + + @pytest.fixture(autouse=True) + def _proxy_key(self, monkeypatch): + monkeypatch.setenv("PROXY_API_KEY", "proxy-master-key-0042") + + def test_the_bail_out_string_is_scrubbed(self): + from tests.test_claude_client import _make_participant + + participant = _make_participant() + participant._client.query.return_value = "401 Received API Key = proxy-master-key-0042" + messages = self._stream(participant) + assert any("Claude agent error" in m for m in messages) + assert "proxy-master-key-0042" not in " ".join(messages) + + def test_an_exception_is_scrubbed(self): + from tests.test_claude_client import _make_participant + + participant = _make_participant() + participant._client.query.side_effect = RuntimeError( + "401 Received API Key = proxy-master-key-0042" + ) + messages = self._stream(participant) + assert any("Error" in m for m in messages) + assert "proxy-master-key-0042" not in " ".join(messages)