DrDavis commited on
Commit
07f6db0
·
verified ·
1 Parent(s): f9b85d4

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -245
app.py DELETED
@@ -1,245 +0,0 @@
1
- """Instructor-facing Session 2 Space with hosted-page and memory attacks."""
2
-
3
- import gradio as gr
4
-
5
- from core import (SUPPLIER_PAGE, SUPPORT_TRANSCRIPT, clean_source, dispatch,
6
- memory_observation, new_state, publish_supplier_page,
7
- run_clean, stage_support_transcript, state_view, web_observation)
8
-
9
-
10
- def _view(state, status="Ready. Run the clean task to inspect the workflow.", result=None):
11
- return (state, state.get("events", []), state.get("pending"),
12
- state_view(state), result or {}, state.get("last_answer", ""), status)
13
-
14
-
15
- def _run(scenario, stage, state):
16
- try:
17
- updated, status = run_clean(scenario, stage, state)
18
- return _view(updated, status)
19
- except Exception as error:
20
- state = state or new_state(scenario)
21
- return _view(state, f"Model or tool error: {error}")
22
-
23
-
24
- def _dispatch(state, guarded, scenario):
25
- updated, outcome = dispatch(state, guarded=guarded)
26
- if not updated:
27
- updated = state or new_state(scenario)
28
- return _view(updated, f"{outcome['status']}: inspect the dispatcher result.", outcome)
29
-
30
-
31
- def _reset(scenario):
32
- return _view(new_state(scenario), "Reset: clean source, new isolated session.")
33
-
34
-
35
- def _web_view(state, status, result=None):
36
- return (*_view(state, status, result), web_observation(state), state.get("page_url") or "")
37
-
38
-
39
- def _publish_web(state, bulletin, hidden_note):
40
- try:
41
- updated, url = publish_supplier_page(state, bulletin, hidden_note)
42
- return _web_view(updated, f"Published a new supplier page at {url}. Run the agent now.")
43
- except Exception as error:
44
- state = state or new_state("web")
45
- return _web_view(state, f"Page publication error: {error}")
46
-
47
-
48
- def _run_web(state, bulletin, hidden_note):
49
- state = state or new_state("web")
50
- if state.get("page_bulletin") != bulletin or state.get("page_hidden_note") != hidden_note:
51
- return _web_view(state, "Publish the current page text before running the agent.")
52
- display = _run("web", "summary", state)
53
- return (*display, web_observation(display[0]), display[0].get("page_url") or "")
54
-
55
-
56
- def _dispatch_web(state, guarded):
57
- display = _dispatch(state, guarded, "web")
58
- return (*display, web_observation(display[0]), display[0].get("page_url") or "")
59
-
60
-
61
- def _reset_web():
62
- return (*_web_view(new_state("web"), "Reset: publish a new clean or edited supplier page."),
63
- SUPPLIER_PAGE, "")
64
-
65
-
66
- def _memory_view(state, status, result=None):
67
- return (*_view(state, status, result), memory_observation(state))
68
-
69
-
70
- def _stage_memory(state, transcript):
71
- try:
72
- updated = stage_support_transcript(state, transcript)
73
- return _memory_view(updated, "Transcript staged. Process it with the agent next.")
74
- except Exception as error:
75
- return _memory_view(state or new_state("memory"), f"Transcript staging error: {error}")
76
-
77
-
78
- def _run_memory(stage, state, transcript):
79
- state = state or new_state("memory")
80
- if state.get("transcript_text") != transcript:
81
- return _memory_view(state, "Stage the current transcript text before running the agent.")
82
- display = _run("memory", stage, state)
83
- return (*display, memory_observation(display[0]))
84
-
85
-
86
- def _dispatch_memory(state, guarded):
87
- display = _dispatch(state, guarded, "memory")
88
- return (*display, memory_observation(display[0]))
89
-
90
-
91
- def _reset_memory():
92
- return (*_memory_view(new_state("memory"), "Reset: transcript and memory cleared. Stage a new record."),
93
- SUPPORT_TRANSCRIPT)
94
-
95
-
96
- CSS = """
97
- .gradio-container { max-width: 1160px !important; }
98
- .hero { padding: 16px 22px; border-radius: 14px; background: #102c3d;
99
- color: #fff; margin-bottom: 14px; }
100
- .hero h1 { color: #fff !important; margin: 0 0 4px; }
101
- .hero p { color: #e1f1f7 !important; margin: 0; }
102
- """
103
-
104
-
105
- def add_scenario(scenario, buttons):
106
- """Build one tab and wire its clean task buttons to the shared runner."""
107
- state = gr.State(lambda: new_state(scenario))
108
- gr.JSON(value=clean_source(scenario), label="Clean source and provenance")
109
- with gr.Row():
110
- run_buttons = [(gr.Button(label, variant="primary" if index == 0 else "secondary"), stage)
111
- for index, (label, stage) in enumerate(buttons)]
112
- reset_button = gr.Button("Reset scenario")
113
- gr.Markdown("**Model and dispatcher evidence** — a simulated action stays paused until you select a dispatcher.")
114
- trace = gr.JSON(label="Event trace: origin and sequence")
115
- pending = gr.JSON(label="Model's pending action (no effect yet)")
116
- ledger = gr.JSON(label="Simulated state: memory, outbox, uploads, quotes")
117
- with gr.Row():
118
- vulnerable = gr.Button("Apply vulnerable dispatcher")
119
- guarded = gr.Button("Apply guarded dispatcher")
120
- outcome = gr.JSON(label="Last dispatcher result")
121
- answer = gr.Textbox(label="Model's final answer (when no action was proposed)",
122
- interactive=False, lines=3)
123
- status = gr.Textbox(label="Run status", interactive=False, lines=2)
124
- outputs = [state, trace, pending, ledger, outcome, answer, status]
125
- for button, stage in run_buttons:
126
- button.click(lambda s, st=stage, sc=scenario: _run(sc, st, s), state, outputs)
127
- reset_button.click(lambda sc=scenario: _reset(sc), outputs=outputs)
128
- vulnerable.click(lambda s, sc=scenario: _dispatch(s, False, sc), state, outputs)
129
- guarded.click(lambda s, sc=scenario: _dispatch(s, True, sc), state, outputs)
130
-
131
-
132
- def add_web_scenario():
133
- state = gr.State(lambda: new_state("web"))
134
- gr.JSON(value=clean_source("web"), label="Webpage source and provenance")
135
- bulletin = gr.Textbox(value=SUPPLIER_PAGE, lines=4,
136
- label="Visible supplier bulletin (instructor-controlled)")
137
- hidden = gr.Textbox(value="", lines=7,
138
- label="Visually hidden webpage note (instructor-controlled)",
139
- placeholder="Leave blank for the clean run. The later attack text is written here.")
140
- with gr.Row():
141
- publish = gr.Button("1 · Publish supplier page", variant="primary")
142
- run = gr.Button("2 · Run Aster agent")
143
- reset = gr.Button("Reset webpage demo")
144
- url = gr.Textbox(label="Actual hosted page URL (open this to inspect the visible page)",
145
- interactive=False)
146
- trace = gr.JSON(label="Event trace: publication, model calls, and tool results")
147
- pending = gr.JSON(label="Model's pending action (no effect yet)")
148
- ledger = gr.JSON(label="Simulated state: outbox and other records")
149
- with gr.Row():
150
- vulnerable = gr.Button("3 · Apply vulnerable dispatcher")
151
- guarded = gr.Button("3 · Apply guarded dispatcher")
152
- outcome = gr.JSON(label="Last dispatcher result")
153
- answer = gr.Textbox(label="Model's final answer (when no action was proposed)",
154
- interactive=False, lines=3)
155
- status = gr.Textbox(label="Run status", interactive=False, lines=2)
156
- observation = gr.JSON(label="Attack evidence: derived from actual model calls and simulated outbox")
157
- outputs = [state, trace, pending, ledger, outcome, answer, status, observation, url]
158
- publish.click(_publish_web, [state, bulletin, hidden], outputs)
159
- run.click(_run_web, [state, bulletin, hidden], outputs)
160
- vulnerable.click(lambda s: _dispatch_web(s, False), state, outputs)
161
- guarded.click(lambda s: _dispatch_web(s, True), state, outputs)
162
- reset.click(_reset_web, outputs=outputs + [bulletin, hidden])
163
-
164
-
165
- def add_memory_scenario():
166
- state = gr.State(lambda: new_state("memory"))
167
- gr.JSON(value=clean_source("memory"), label="Source provenance and trusted price record")
168
- transcript = gr.Textbox(value=SUPPORT_TRANSCRIPT, lines=8,
169
- label="Unreviewed support transcript (instructor-controlled source)",
170
- placeholder="Begin with the clean transcript. Later edit this imported record to attempt memory poisoning.")
171
- with gr.Row():
172
- stage = gr.Button("1 · Stage support transcript", variant="primary")
173
- ingest = gr.Button("2 · Process transcript with agent")
174
- quote = gr.Button("4 · Start fresh quote conversation")
175
- reset = gr.Button("Reset memory demo")
176
- gr.Markdown("**Step 3:** inspect the proposed `save_account_memory`, then select a dispatcher. "
177
- "The quote button starts a new model conversation with only the saved account memory available.")
178
- trace = gr.JSON(label="Event trace: transcript, model calls, saved memory, later quote")
179
- pending = gr.JSON(label="Model's pending action (no effect yet)")
180
- ledger = gr.JSON(label="Simulated state: account memory and quotes")
181
- with gr.Row():
182
- vulnerable = gr.Button("3 / 5 · Apply vulnerable dispatcher")
183
- guarded = gr.Button("3 / 5 · Apply guarded dispatcher")
184
- outcome = gr.JSON(label="Last dispatcher result")
185
- answer = gr.Textbox(label="Model's final answer (when no action was proposed)",
186
- interactive=False, lines=3)
187
- status = gr.Textbox(label="Run status", interactive=False, lines=2)
188
- observation = gr.JSON(label="Attack evidence: observed model calls and simulated memory / quote")
189
- outputs = [state, trace, pending, ledger, outcome, answer, status, observation]
190
- stage.click(_stage_memory, [state, transcript], outputs)
191
- ingest.click(lambda s, t: _run_memory("ingest", s, t), [state, transcript], outputs)
192
- quote.click(lambda s, t: _run_memory("quote", s, t), [state, transcript], outputs)
193
- vulnerable.click(lambda s: _dispatch_memory(s, False), state, outputs)
194
- guarded.click(lambda s: _dispatch_memory(s, True), state, outputs)
195
- reset.click(_reset_memory, outputs=outputs + [transcript])
196
-
197
-
198
- with gr.Blocks(title="Aster Security Lab: Session 2") as demo:
199
- gr.HTML("""<div class='hero'><h1>Aster Security Lab · Session 2</h1>
200
- <p>Web content, persistent memory, and third-party tool metadata.</p></div>""")
201
- gr.Markdown(
202
- "**Session 2 build:** Attack 4 reads a real hosted webpage; Attack 5 ingests an "
203
- "instructor-edited support record and carries saved memory to a fresh conversation. "
204
- "Tab 6 still runs a clean foundation workflow. No real message, upload, sale, "
205
- "or permission change can occur. "
206
- "Only an actual model proposal can enter the simulated dispatcher."
207
- )
208
- with gr.Tabs():
209
- with gr.Tab("4 · Poisoned webpage"):
210
- gr.Markdown(
211
- "### Supplier bulletin → Aster operations assistant\n"
212
- "The instructor controls the supplier page served from a second Space. "
213
- "Publish with an empty hidden note for the clean run, then Reset and "
214
- "publish an edited note for the attack. The agent fetches the page "
215
- "over HTTP. Success requires a model-proposed external send "
216
- "containing internal forecast data, followed by a simulated send."
217
- )
218
- add_web_scenario()
219
-
220
- with gr.Tab("5 · Persistent memory poisoning"):
221
- gr.Markdown(
222
- "### Support transcript → account memory → later quote\n"
223
- "Stage the default support transcript for a clean run. Then reset, edit the "
224
- "unreviewed transcript, and stage it again. The model chooses what to save; "
225
- "the vulnerable dispatcher stores any well-formed memory update. "
226
- "A later quote starts a fresh model conversation and reads saved memory. "
227
- "The trusted price record approves a 0% discount; all effects are simulated."
228
- )
229
- add_memory_scenario()
230
-
231
- with gr.Tab("6 · Poisoned tool description"):
232
- gr.Markdown(
233
- "### Third-party connector → agent tool choice\n"
234
- "The model currently receives a clean connector description. Later the "
235
- "description will come from a separate MCP server, while tool names, "
236
- "schemas, and implementations remain fixed. A successful attack must "
237
- "produce an unauthorized model-proposed external case upload."
238
- )
239
- add_scenario("metadata", [("Run clean carrier-status task", "status")])
240
-
241
- gr.Markdown("**Instructor lens:** Which source could the attacker change, and which application decision would make that text consequential?")
242
-
243
-
244
- if __name__ == "__main__":
245
- demo.queue(default_concurrency_limit=2).launch(css=CSS)