Skip to content

Commit 237e522

Browse files
ayylemaoMatthias Voglerhayescode
authored andcommitted
feat: add configurable user_env persistence to database (Chainlit#2397)
From Chainlit#2335 Simple clear of user env before writing to db with .chainlit/config.toml default parameter **Also changed:** API key fields in frontend are now masked for improved security. --------- Co-authored-by: Matthias Vogler <[email protected]> Co-authored-by: Josh Hayes <[email protected]>
1 parent 940a838 commit 237e522

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

backend/chainlit/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@
7878
# Enable third parties caching (e.g., LangChain cache)
7979
cache = false
8080
81+
# Whether to persist user environment variables (API keys) to the database
82+
# Set to true to store user env vars in DB, false to exclude them for security
83+
persist_user_env = false
84+
85+
# Whether to mask user environment variables (API keys) in the UI with password type
86+
# Set to true to show API keys as ***, false to show them as plain text
87+
mask_user_env = false
88+
8189
# Authorized origins
8290
allow_origins = ["*"]
8391
@@ -399,6 +407,10 @@ class ProjectSettings(BaseModel):
399407
user_session_timeout: int = 1296000 # 15 days
400408
# Enable third parties caching (e.g LangChain cache)
401409
cache: bool = False
410+
# Whether to persist user environment variables (API keys) to the database
411+
persist_user_env: Optional[bool] = False
412+
# Whether to mask user environment variables (API keys) in the UI with password type
413+
mask_user_env: Optional[bool] = False
402414

403415

404416
class ChainlitConfigOverrides(BaseModel):

backend/chainlit/server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@ async def project_settings(
838838
"ui": cfg.ui.model_dump(),
839839
"features": cfg.features.model_dump(),
840840
"userEnv": cfg.project.user_env,
841+
"maskUserEnv": cfg.project.mask_user_env,
841842
"dataPersistence": data_layer is not None,
842843
"threadResumable": bool(config.code.on_chat_resume),
843844
"markdown": markdown,

backend/chainlit/session.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,21 @@ async def persist_file(
141141
return {"id": file_id}
142142

143143
def to_persistable(self) -> Dict:
144+
from chainlit.config import config
144145
from chainlit.user_session import user_sessions
145146

146147
user_session = user_sessions.get(self.id) or {} # type: Dict
147148
user_session["chat_settings"] = self.chat_settings
148149
user_session["chat_profile"] = self.chat_profile
149150
user_session["client_type"] = self.client_type
150-
metadata = clean_metadata(user_session)
151+
152+
# Check config setting for whether to persist user environment variables
153+
user_session_copy = user_session.copy()
154+
if not config.project.persist_user_env:
155+
# Remove user environment variables (API keys) before persisting to database
156+
user_session_copy["env"] = {}
157+
158+
metadata = clean_metadata(user_session_copy)
151159
return metadata
152160

153161

frontend/src/pages/Env.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const Env = () => {
8686
<Label htmlFor={key}>{key}</Label>
8787
<Input
8888
id={key}
89+
type={config?.maskUserEnv !== false ? "password" : "text"}
8990
{...register(key)}
9091
className={
9192
touchedFields[key] && errors[key] ? 'border-red-500' : ''

libs/react-client/src/types/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export interface IChainlitConfig {
8282
};
8383
debugUrl?: string;
8484
userEnv: string[];
85+
maskUserEnv?: boolean;
8586
dataPersistence: boolean;
8687
threadResumable: boolean;
8788
chatProfiles: ChatProfile[];

0 commit comments

Comments
 (0)