Commit cd3acd1
[EARS] Fix OAuth "Cancel authorization" to delete pending server-side state (#270224)
## Summary
When a user clicked "Cancel authorization" during an OAuth connector
flow, the UI reset correctly but the server-side `oauth_state` saved
object was left behind. If the OAuth provider tab was still open and the
user completed authorization there, the callback would succeed and the
connector would silently become connected — even though the user had
already cancelled. The orphan state would otherwise be reaped by the
10-minute TTL and the periodic cleanup task, but the window was real.
This PR makes cancel actually cancel by deleting the pending state
server-side:
- Adds `POST /internal/actions/connector/{connectorId}/_oauth_cancel` —
looks up the `oauth_state` saved object by the `state` value, verifies
ownership via `profile_uid`, and deletes it. Returns 204 (idempotent:
deleted and not-found both succeed), 403 if the state was created by a
different user.
- Adds `OAuthStateClient.deleteByState(stateParam, profileUid)` with
explicit ownership handling, including the case where `createdBy` is
absent (treated as forbidden).
- Wires `cancelConnect()` in `useConnectorOAuthConnect` to call the new
endpoint as a fire-and-forget: UI state resets synchronously;
server-side cancel is best-effort cleanup.
- Adds `state` to `StartOAuthFlowResponse` so the hook can pass the
exact state string to the cancel endpoint.
Closes elastic/search-team#14488
### Proof
https://github.com/user-attachments/assets/2dad9f8d-0911-4e6e-b4d8-9c811f0474ff
### Checklist
Check the PR satisfies following conditions.
Reviewers should verify this PR satisfies this list as well.
- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [x] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [x] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
- [x] Review the [backport
guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)
and apply applicable `backport:*` labels.
### Identify risks
- **Race condition (low):** A user could complete the OAuth flow in the
provider tab between clicking Cancel and the server processing the
cancel request. This is acceptable — cancel is best-effort and the
existing 10-minute TTL provides a backstop. The UI already shows
"disconnected" by the time any race could matter.
- **Ownership check on legacy states:** `oauth_state` objects without a
`createdBy` field (e.g. created before this change) cannot be cancelled
and return 403. This is intentional — without ownership data we cannot
verify the requester's right to cancel.
- **No breaking changes:** The new endpoint uses `access: 'internal'`.
The `state` field added to `StartOAuthFlowResponse` is additive and
backward compatible.
### Release note
Fixes a bug where cancelling an in-progress OAuth connector
authorization flow left the pending server-side state intact, allowing
the authorization to complete silently if the provider tab remained
open.
---------
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>1 parent b5d7f60 commit cd3acd1
12 files changed
Lines changed: 714 additions & 14 deletions
File tree
- x-pack/platform
x-pack/platform/packages/shared/response-ops/oauth-hooks/hooks/use_connector_oauth_connect.test.ts
Lines changed: 195 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
182 | 182 | | |
183 | 183 | | |
184 | 184 | | |
| 185 | + | |
185 | 186 | | |
186 | 187 | | |
187 | 188 | | |
188 | | - | |
| 189 | + | |
189 | 190 | | |
190 | 191 | | |
191 | 192 | | |
192 | 193 | | |
193 | 194 | | |
194 | 195 | | |
195 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
196 | 368 | | |
197 | 369 | | |
198 | 370 | | |
| |||
206 | 378 | | |
207 | 379 | | |
208 | 380 | | |
| 381 | + | |
209 | 382 | | |
210 | | - | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
211 | 386 | | |
212 | 387 | | |
213 | 388 | | |
| |||
236 | 411 | | |
237 | 412 | | |
238 | 413 | | |
| 414 | + | |
239 | 415 | | |
240 | | - | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
241 | 419 | | |
242 | 420 | | |
243 | 421 | | |
| |||
265 | 443 | | |
266 | 444 | | |
267 | 445 | | |
| 446 | + | |
268 | 447 | | |
269 | | - | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
270 | 451 | | |
271 | 452 | | |
272 | 453 | | |
| |||
300 | 481 | | |
301 | 482 | | |
302 | 483 | | |
| 484 | + | |
303 | 485 | | |
304 | | - | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
305 | 489 | | |
306 | 490 | | |
307 | 491 | | |
| |||
325 | 509 | | |
326 | 510 | | |
327 | 511 | | |
| 512 | + | |
328 | 513 | | |
329 | | - | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
330 | 517 | | |
331 | 518 | | |
332 | 519 | | |
| |||
356 | 543 | | |
357 | 544 | | |
358 | 545 | | |
| 546 | + | |
359 | 547 | | |
360 | 548 | | |
361 | 549 | | |
362 | | - | |
| 550 | + | |
363 | 551 | | |
364 | 552 | | |
365 | 553 | | |
| |||
Lines changed: 21 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
92 | 93 | | |
93 | 94 | | |
94 | 95 | | |
| |||
110 | 111 | | |
111 | 112 | | |
112 | 113 | | |
113 | | - | |
| 114 | + | |
114 | 115 | | |
115 | 116 | | |
116 | 117 | | |
117 | 118 | | |
118 | 119 | | |
119 | | - | |
| 120 | + | |
| 121 | + | |
120 | 122 | | |
121 | 123 | | |
122 | 124 | | |
| |||
126 | 128 | | |
127 | 129 | | |
128 | 130 | | |
| 131 | + | |
129 | 132 | | |
130 | 133 | | |
131 | 134 | | |
| |||
139 | 142 | | |
140 | 143 | | |
141 | 144 | | |
| 145 | + | |
| 146 | + | |
142 | 147 | | |
143 | | - | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
144 | 160 | | |
145 | 161 | | |
146 | 162 | | |
| |||
149 | 165 | | |
150 | 166 | | |
151 | 167 | | |
| 168 | + | |
152 | 169 | | |
153 | 170 | | |
154 | 171 | | |
| |||
173 | 190 | | |
174 | 191 | | |
175 | 192 | | |
| 193 | + | |
176 | 194 | | |
177 | 195 | | |
178 | 196 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
| 99 | + | |
| 100 | + | |
99 | 101 | | |
100 | 102 | | |
101 | 103 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
| 21 | + | |
| 22 | + | |
19 | 23 | | |
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
0 commit comments