Skip to content

Commit 60947e2

Browse files
dennykucopyberry
authored andcommitted
Add account security setup reminders to the TUI (#49715)
## What changed Fetch optional security setup notices asynchronously for local ChatGPT sessions using the OpenAI provider. Verify that saved credentials match the connected app server, ignore stale account responses, and validate notice text and HTTPS actions on `chatgpt.com`. Show notices as dismissible inline banners with a browser action, while giving applicable usage banners priority. Preserve notice state and dismissal across thread changes, reconnects, and banner replacement for the same account and user. ## Testing Add authenticated HTTP fixture tests, notice validation tests, and TUI tests covering stale responses, dismissal persistence, usage banner priority, browser actions, and wrapping in narrow terminals. GitOrigin-RevId: b8635fcf6a26b42b6a06640601c8517d8d5e47e8
1 parent f151a0f commit 60947e2

23 files changed

Lines changed: 832 additions & 4 deletions

‎codex-rs/tui/src/app/app_server_events.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ impl App {
326326
self.agents_overview.usage_disabled = false;
327327
self.repaint_agents_overview();
328328
self.chat_widget.cyber_policy_notice = Default::default();
329+
self.chat_widget.invalidate_security_setup();
329330
if let Some(crate::pager_overlay::Overlay::Analytics(view)) = &mut self.overlay {
330331
view.refresh();
331332
}
@@ -360,6 +361,12 @@ impl App {
360361
has_codex_backend_auth,
361362
);
362363
if self.chat_widget.has_chatgpt_account() {
364+
crate::security_setup::prefetch(
365+
&self.config,
366+
app_server_client,
367+
self.app_event_tx.clone(),
368+
self.chat_widget.security_setup_request_id,
369+
);
363370
crate::daybreak::prefetch_notice(
364371
&self.config,
365372
app_server_client,

‎codex-rs/tui/src/app/event_dispatch.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,12 @@ impl App {
12571257
elicitation_target: None,
12581258
});
12591259
}
1260+
AppEvent::SecuritySetupLoaded { request_id, identity, notice } => {
1261+
tracing::debug!(current = request_id == self.chat_widget.security_setup_request_id, "handling security setup notice");
1262+
if request_id == self.chat_widget.security_setup_request_id {
1263+
self.chat_widget.show_security_setup(identity, notice);
1264+
}
1265+
}
12601266
AppEvent::OpenUrlInBrowser { url } => {
12611267
self.open_url_in_browser(url);
12621268
}

‎codex-rs/tui/src/app/reconnect.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,12 @@ impl App {
484484
matches!(bootstrap.auth_mode, Some(TelemetryAuthMode::Chatgpt)),
485485
);
486486
if self.chat_widget.has_chatgpt_account() {
487+
crate::security_setup::prefetch(
488+
&self.config,
489+
app_server,
490+
self.app_event_tx.clone(),
491+
self.chat_widget.security_setup_request_id,
492+
);
487493
crate::daybreak::prefetch_notice(
488494
&self.config,
489495
app_server,

‎codex-rs/tui/src/app/session_lifecycle.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ impl App {
520520
AppServerTarget::LocalDaemon { .. }
521521
));
522522
chat_widget.inherit_backend_banner_state(&mut self.chat_widget);
523+
chat_widget.inherit_security_setup(&mut self.chat_widget);
523524
for (thread_id, entry) in self.agent_navigation.ordered_threads() {
524525
chat_widget.set_collab_agent_metadata(
525526
thread_id,

‎codex-rs/tui/src/app/startup.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,12 @@ See the Codex keymap documentation for supported actions and examples."
10211021
// already has data and available reset credits can be surfaced, without
10221022
// delaying the initial frame render.
10231023
if requires_openai_auth && has_chatgpt_account {
1024+
crate::security_setup::prefetch(
1025+
&app.config,
1026+
&app_server,
1027+
app.app_event_tx.clone(),
1028+
app.chat_widget.security_setup_request_id,
1029+
);
10241030
crate::daybreak::prefetch_notice(
10251031
&app.config,
10261032
&app_server,

‎codex-rs/tui/src/app/tests.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ mod mcp_login_tests;
77
mod daybreak_tests;
88
#[path = "tests/math_interruption_tests.rs"]
99
mod math_interruption_tests;
10+
#[path = "tests/security_setup_tests.rs"]
11+
mod security_setup_tests;
1012

1113
#[path = "tests/advanced_reasoning_tests.rs"]
1214
mod advanced_reasoning_tests;
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
//! Exercises the authenticated reminder transport against a local HTTP fixture.
2+
use super::disconnect::serve_reconnect_requests;
3+
use super::*;
4+
use crate::app_server_session::ThreadParamsMode;
5+
use crate::security_setup::Identity;
6+
use app_test_support::ChatGptAuthFixture;
7+
use app_test_support::write_chatgpt_auth;
8+
use codex_app_server_client::AppServerEvent;
9+
use codex_app_server_protocol::AccountUpdatedNotification;
10+
use codex_app_server_protocol::AuthMode;
11+
use codex_login::AuthCredentialsStoreMode;
12+
use pretty_assertions::assert_eq;
13+
use serde_json::json;
14+
use tokio::net::TcpListener;
15+
16+
#[tokio::test]
17+
async fn security_setup_fetch_with_default_features_uses_authenticated_codex_endpoint() -> Result<()>
18+
{
19+
let (mut app, mut events, _ops) = make_test_app_with_channels().await;
20+
let backend = wiremock::MockServer::start().await;
21+
app.config.chatgpt_base_url = backend.uri();
22+
app.config.cli_auth_credentials_store_mode = AuthCredentialsStoreMode::File;
23+
std::fs::write(
24+
app.config.codex_home.join("config.toml"),
25+
format!("chatgpt_base_url = {:?}\n", backend.uri()),
26+
)?;
27+
write_chatgpt_auth(
28+
&app.config.codex_home,
29+
ChatGptAuthFixture::new("test-token")
30+
.account_id("account")
31+
.chatgpt_user_id("user"),
32+
AuthCredentialsStoreMode::File,
33+
)
34+
.expect("write synthetic auth");
35+
app_test_support::mount_workspace_routing(&backend).await;
36+
let mut server = Box::pin(crate::start_embedded_app_server_for_picker(&app.config)).await?;
37+
wiremock::Mock::given(wiremock::matchers::path("/wham/security-setup"))
38+
.and(wiremock::matchers::header(
39+
"authorization",
40+
"Bearer test-token",
41+
))
42+
.and(wiremock::matchers::header("ChatGPT-Account-ID", "account"))
43+
.and(wiremock::matchers::header(
44+
"User-Agent",
45+
codex_login::default_client::get_codex_user_agent(),
46+
))
47+
.respond_with(wiremock::ResponseTemplate::new(200).set_body_json(json!({
48+
"notice": {
49+
"title": "Keep using Daybreak mode", "description": "Set up security.",
50+
"action": {"label": "Set up security", "url": "https://chatgpt.com/cyber"}
51+
}
52+
})))
53+
.expect(3)
54+
.mount(&backend)
55+
.await;
56+
let request_id = app.chat_widget.security_setup_request_id;
57+
let mut tui = crate::tui::test_support::make_test_tui()?;
58+
let init = app.chatwidget_init_for_forked_or_resumed_thread(
59+
&mut tui,
60+
app.config.clone(),
61+
/*initial_user_message*/ None,
62+
);
63+
app.replace_chat_widget(ChatWidget::new_with_app_event(init));
64+
assert_eq!(app.chat_widget.security_setup_request_id, request_id);
65+
crate::security_setup::prefetch(&app.config, &server, app.app_event_tx.clone(), request_id);
66+
let notice = tokio::time::timeout(Duration::from_secs(5), async {
67+
loop {
68+
if let AppEvent::SecuritySetupLoaded {
69+
request_id: actual,
70+
identity,
71+
notice,
72+
} = events.recv().await.unwrap()
73+
{
74+
assert_eq!(actual, request_id);
75+
assert_eq!(
76+
identity,
77+
Identity {
78+
account: "account".into(),
79+
user: "user".into()
80+
}
81+
);
82+
break notice;
83+
}
84+
}
85+
})
86+
.await?;
87+
assert!(notice.valid());
88+
let identity = Identity {
89+
account: "account".into(),
90+
user: "user".into(),
91+
};
92+
app.handle_event(
93+
&mut tui,
94+
&mut server,
95+
AppEvent::SecuritySetupLoaded {
96+
request_id,
97+
identity: identity.clone(),
98+
notice: notice.clone(),
99+
},
100+
)
101+
.await?;
102+
assert!(render_bottom_popup(&app.chat_widget, /*width*/ 70).contains("Set up security"));
103+
app.chat_widget
104+
.handle_key_event(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
105+
assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 70).contains("Set up security"));
106+
107+
// Reconnect can emit AccountUpdated for the same identity more than once.
108+
for _ in 0..2 {
109+
let stale_request_id = app.chat_widget.security_setup_request_id;
110+
app.handle_app_server_event(
111+
&server,
112+
AppServerEvent::ServerNotification(Box::new(ServerNotification::AccountUpdated(
113+
AccountUpdatedNotification {
114+
auth_mode: Some(AuthMode::Chatgpt),
115+
plan_type: None,
116+
},
117+
))),
118+
)
119+
.await;
120+
let request_id = app.chat_widget.security_setup_request_id;
121+
assert_ne!(request_id, stale_request_id);
122+
app.handle_event(
123+
&mut tui,
124+
&mut server,
125+
AppEvent::SecuritySetupLoaded {
126+
request_id: stale_request_id,
127+
identity: Identity {
128+
account: "stale-account".into(),
129+
..identity.clone()
130+
},
131+
notice: notice.clone(),
132+
},
133+
)
134+
.await?;
135+
assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 70).contains("Set up security"));
136+
137+
let init = app.chatwidget_init_for_forked_or_resumed_thread(
138+
&mut tui,
139+
app.config.clone(),
140+
/*initial_user_message*/ None,
141+
);
142+
app.replace_chat_widget(ChatWidget::new_with_app_event(init));
143+
let event = tokio::time::timeout(Duration::from_secs(5), async {
144+
loop {
145+
let event = events.recv().await.unwrap();
146+
if let AppEvent::SecuritySetupLoaded {
147+
request_id: actual,
148+
identity: actual_identity,
149+
..
150+
} = &event
151+
{
152+
assert_eq!(*actual, request_id);
153+
assert_eq!(actual_identity, &identity);
154+
break event;
155+
}
156+
}
157+
})
158+
.await?;
159+
app.handle_event(&mut tui, &mut server, event).await?;
160+
assert!(!render_bottom_popup(&app.chat_widget, /*width*/ 70).contains("Set up security"));
161+
}
162+
backend.verify().await;
163+
server.shutdown().await?;
164+
Ok(())
165+
}
166+
167+
#[tokio::test]
168+
async fn security_setup_skips_fetch_when_server_auth_does_not_match_saved_login() -> Result<()> {
169+
for (auth_method, auth_token) in [
170+
(
171+
Some(AuthMode::ChatgptAuthTokens),
172+
Some("other-account-token"),
173+
),
174+
(Some(AuthMode::ChatgptAuthTokens), Some("saved-token")),
175+
(Some(AuthMode::Chatgpt), Some("other-account-token")),
176+
(Some(AuthMode::Chatgpt), None),
177+
(None, None),
178+
] {
179+
let (mut app, _events, _ops) = make_test_app_with_channels().await;
180+
let backend = wiremock::MockServer::start().await;
181+
app.config.chatgpt_base_url = backend.uri();
182+
app.config.cli_auth_credentials_store_mode = AuthCredentialsStoreMode::File;
183+
write_chatgpt_auth(
184+
&app.config.codex_home,
185+
ChatGptAuthFixture::new("saved-token")
186+
.account_id("saved-account")
187+
.chatgpt_user_id("saved-user"),
188+
AuthCredentialsStoreMode::File,
189+
)
190+
.expect("write synthetic auth");
191+
wiremock::Mock::given(wiremock::matchers::path("/wham/security-setup"))
192+
.respond_with(wiremock::ResponseTemplate::new(200).set_body_json(json!({
193+
"notice": {
194+
"title": "Keep using Daybreak mode", "description": "Set up security.",
195+
"action": {"label": "Set up security", "url": "https://chatgpt.com/cyber"}
196+
}
197+
})))
198+
.expect(0)
199+
.mount(&backend)
200+
.await;
201+
202+
let listener = TcpListener::bind("127.0.0.1:0").await?;
203+
let endpoint = crate::resolve_remote_addr(&format!("ws://{}", listener.local_addr()?))?;
204+
let daemon = tokio::spawn(async move {
205+
let (stream, _) = listener.accept().await?;
206+
serve_reconnect_requests(tokio_tungstenite::accept_async(stream).await?, |request| {
207+
assert_eq!(request.method, "getAuthStatus");
208+
assert_eq!(
209+
request.params,
210+
Some(json!({"includeToken": true, "refreshToken": false}))
211+
);
212+
std::future::ready(Some(json!({"result": {
213+
"authMethod": auth_method, "authToken": auth_token,
214+
"requiresOpenaiAuth": true
215+
}})))
216+
})
217+
.await
218+
});
219+
let server = AppServerSession::new(
220+
crate::connect_remote_app_server(endpoint).await?,
221+
ThreadParamsMode::Embedded,
222+
);
223+
let (tx, mut events) = mpsc::unbounded_channel();
224+
crate::security_setup::prefetch(
225+
&app.config,
226+
&server,
227+
AppEventSender::new(tx),
228+
app.chat_widget.security_setup_request_id,
229+
);
230+
// The fetch owns the only sender, so channel closure proves it completed.
231+
assert!(
232+
tokio::time::timeout(Duration::from_secs(5), events.recv())
233+
.await?
234+
.is_none()
235+
);
236+
backend.verify().await;
237+
server.shutdown().await?;
238+
let methods = daemon.await??;
239+
assert!(methods.iter().any(|method| method == "getAuthStatus"));
240+
}
241+
Ok(())
242+
}

‎codex-rs/tui/src/app_event.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ pub(crate) struct AgentsOverviewThreadRefresh {
279279
#[allow(clippy::large_enum_variant)]
280280
#[derive(Debug, IntoStaticStr)]
281281
pub(crate) enum AppEvent {
282+
SecuritySetupLoaded {
283+
request_id: uuid::Uuid,
284+
identity: crate::security_setup::Identity,
285+
notice: crate::security_setup::Notice,
286+
},
282287
OpenDaemonMenu,
283288
ConfirmDaemonUpdate(crate::update_action::DaemonUpdateSource),
284289
RunDaemonUpdate(crate::update_action::DaemonUpdateSource),

‎codex-rs/tui/src/bottom_pane/actionable_banner.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,16 @@ impl From<ActionableBanner> for SelectionViewParams {
159159
}
160160

161161
impl BottomPane {
162+
pub(crate) fn transfer_inline_banner_from(&mut self, previous: &mut Self) {
163+
self.inline_banner = previous.inline_banner.take();
164+
if let Some(banner) = &mut self.inline_banner
165+
&& let InlineBannerContent::Actions(view) = &mut banner.content
166+
{
167+
view.app_event_tx = self.app_event_tx.clone();
168+
}
169+
self.request_redraw();
170+
}
171+
162172
pub(crate) fn show_actionable_banner(&mut self, banner: ActionableBanner) {
163173
self.show_selection_view(banner.into());
164174
}

‎codex-rs/tui/src/bottom_pane/list_selection_view.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ pub(crate) struct ListSelectionView {
304304
state: ScrollState,
305305
completion: Option<ViewCompletion>,
306306
pub(super) dismiss_after_child_accept: bool,
307-
app_event_tx: AppEventSender,
307+
pub(super) app_event_tx: AppEventSender,
308308
is_searchable: bool,
309309
search_query: String,
310310
search_placeholder: Option<String>,

0 commit comments

Comments
 (0)