Skip to content

fix(api): validate cached OAuth device flow state - #44567

Open
0204zll wants to merge 5 commits into
langgenius:mainfrom
0204zll:fix/33092-device-flow-state-validation
Open

0204zll wants to merge 5 commits into
langgenius:mainfrom
0204zll:fix/33092-device-flow-state-validation

Conversation

@0204zll

@0204zll 0204zll commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part of #33092.

OAuth device state is currently decoded with json.loads and unpacked into a dataclass without validating its fields. A cached null, array, or object missing required fields raises an exception outside the reader's corrupt-state handler; incorrectly typed fields and incomplete poll payloads can instead reach callers.

Validate JSON with a cached Pydantic TypeAdapter over the existing DeviceFlowState dataclass, including its nested PollPayload. Preserve the existing serialization and the reader's compatibility with legacy approval fields. Both read paths treat validation failures as unavailable state and log only the device identifier, avoiding validation messages that may contain plaintext tokens.

This addresses one remaining JSON parsing boundary; it does not close the repository-wide migration issue.

Validation

  • Added regressions covering malformed JSON, non-object values, missing fields, invalid status, incorrect field types, unexpected fields, incomplete poll payloads, and approved-state round trips. On unchanged main, 12 regression cases fail; all pass with this change.
  • 173 related unit tests passed across device state, token minting, application service, adapters, HTTP controllers, and device-flow security.
  • Ruff lint and format checks passed on both changed files.
  • Pyrefly reported 0 diagnostics for the implementation and test file with their respective project configurations.
  • git diff --check passed.
  • CI guards passed locally: no new getattr, controller SQLAlchemy, session mocks, base models, or test doubles; the full no-spec-mock scan also passed.
  • Remote CI for 6caaba5587546ffd3a06f46cee47b2dc5c55207e: 30 successful checks, 12 conditionally skipped, no failures or pending checks (2026-10-23). This includes Python Style, API unit/controller/integration tests, browser E2E, and Codecov.

Local checks used Python 3.13 through uv run --project api --no-sync with the existing installed dependencies. The full repository lint/type suites and Docker integration tests were not run locally.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion or issues.
  • I've verified the change and added regression tests.
  • This change requires a documentation update.
  • I ran the full make lint && make type-check suites.

From Codex

@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Oct 22, 2026
@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 86.82% 86.82% +0.00%
Strict coverage 86.48% 86.48% +0.00%
Typed symbols 95,315 95,320 +5
Untyped symbols 14,525 14,525 0
Modules 4630 4630 0

@codecov

codecov Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.26%. Comparing base (110ed83) to head (1209930).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #44567      +/-   ##
==========================================
- Coverage   90.26%   90.26%   -0.01%     
==========================================
  Files        6452     6454       +2     
  Lines      407063   407068       +5     
  Branches    81800    81799       -1     
==========================================
+ Hits       367450   367454       +4     
- Misses      33210    33212       +2     
+ Partials     6403     6402       -1     
Flag Coverage Δ
api 90.09% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asukaminato0721 asukaminato0721 self-assigned this Nov 16, 2026

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 inline suggestions at 6caaba5587, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

Comment thread api/services/oauth_device_flow.py Resolved
Comment thread api/services/oauth_device_flow.py Resolved
@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 16, 2026
@github-actions github-actions Bot added the e2e End-to-end tests and E2E test infrastructure. label Nov 18, 2026

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI is failing: Style Check / Python Style. Please fix it. 15 inline suggestions at 2e8dca1500, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

Comment thread api/services/tidb_binding_service.py Resolved
Comment thread api/controllers/console/__init__.py Resolved
@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 18, 2026
@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 18, 2026
@github-actions github-actions Bot removed size:L This PR changes 100-499 lines, ignoring generated files. e2e End-to-end tests and E2E test infrastructure. size:M This PR changes 30-99 lines, ignoring generated files. labels Nov 18, 2026

@asukaminato0721 asukaminato0721 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing to flag at 120993024b.

dify-codereview

@asukaminato0721
asukaminato0721 dismissed their stale review November 18, 2026 09:03

Nothing left to flag at 1209930.

@asukaminato0721 asukaminato0721 added waiting-for-reviewer dify-codereview: checked, nothing left for the author; a reviewer's turn and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-for-reviewer dify-codereview: checked, nothing left for the author; a reviewer's turn

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants