Repository navigation
fix(oauth): harden notion services with timeouts, defensive null guards, and safe database validation (#14257) - #14258
Conversation
…ds, and safe database validation (BasedHardware#14257) Harden Notion CRM OAuth integration against network hangs, unhandled JSON decode errors on HTTP 502/504 responses, null/malformed conversation fields, and fragile database schema handling. Fixes BasedHardware#14257 Failure-Class: none
Git-on-my-level
left a comment
There was a problem hiding this comment.
Thanks @TheRWX — this is a genuinely useful hardening pass. I verified the claims locally at head: all 23 tests in the new suite pass under both python3 -S plugins/oauth/test_oauth_hardening.py and plain python3, and the existing plugins/oauth/test_client_log_safety.py (5 tests) still passes from repo root, so the log-safety invariant from #13778 holds.
Per-file notes:
plugins/oauth/client.py
DEFAULT_TIMEOUT = 10.0correctly reaches all three request calls (get_database,get_access_token,get_databases_edited_time_desc), each now catchingrequests.RequestExceptioninto the structured{"error": ...}envelope callers already understand. Real fix for the unbounded hangs in #14257._safe_jsonfixes the actual 502/504 HTML-page crash, and thefrom_dict/multi_from_dicthardening preserves behavior for well-formed payloads (empty-string defaults only replace what were previously hardKeyError/TypeErrors). The correctedNotionOAuthModel.from_dictreturn annotation is a good catch.- Nit:
Optionalinfrom typing import Any, List, Optionalis unused — please drop it.
plugins/oauth/conversation_created.py
_build_notion_conversation_propertieskeeps the exact property shape on the happy path while adding the null guards from the issue;max(0, int(delta))clamping and the None-speaker dedup are the right calls.- Two wire-visible behavior changes worth naming in the PR body (both look intentional and reasonable):
POST /notion-crmpreviously letvalidate_database'sHTTPExceptionpropagate as a 400 to the caller, whilecreate_notion_rownow returnsFalseso the endpoint returns{}— better for a webhook, but it is a visible change; and the OAuth callback previously bare-returnedNoneon validation failure, it now renders the error page (code 400005) — clear improvement. - The
try: from .client ... except (ImportError, ValueError): from oauth.client ...dual import only serves importing the module withplugins/directly onsys.path(as the new test does); production (plugins/main.pyusesfrom oauth import conversation_created) takes the relative path. Not blocking —test_client_log_safety.pysolves the same need viaimportlib.util.spec_from_file_locationwithout touching production imports, which would also work here.
plugins/oauth/test_oauth_hardening.py
- Good coverage mapping to the issue claims: timeout kwarg assertions, exception envelopes, non-JSON 502, model hardening, payload nulls, and both
validate_databasefailure paths. - Blocking gap: the file is wired into no CI lane.
.github/checks-manifest.yamlrunsplugin-oauth-client-log-safety-testsforplugins/**/*.pychanges, but nothing executes this new 481-line suite, so it will silently rot after merge. Please add an entry mirroring the log-safety one (under the existingchecks:list — entries appended at EOF afterexempt:do not register), e.g.:
- id: plugin-oauth-hardening-tests
command: ["python3", "plugins/oauth/test_oauth_hardening.py"]
triggers: ["plugins/oauth/**", ".github/checks-manifest.yaml"]
lanes: ["local", "ci"]
reason: "regression suite for oauth plugin timeouts, exception envelopes, non-JSON error pages, and null-guarded payload building"That is the repo's contract for hermetic tests (they must run in CI), and a standalone-process manifest command is also the safest way to run this suite given it installs sys.modules stubs at import time.
Once the manifest entry (and the Optional nit) land, I have no further technical concerns. Leaving the merge call for human maintainer review: this is the Notion OAuth integration surface, and bounty eligibility for #14257 is a maintainer decision (claims are handled via [email protected] per the contribution docs rather than the PR body).
by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.
…n up imports (BasedHardware#14257) - Register plugin-oauth-hardening-tests in .github/checks-manifest.yaml directly below plugin-oauth-client-log-safety-tests. - Drop unused Optional import in plugins/oauth/client.py. Product invariants affected: none Failure-Class: none
|
Thanks @Git-on-my-level for the thorough review. Addressed both requested items in commit
Both local and CI preflight gates pass with zero warnings or errors (15/15 checks pass, 23/23 hermetic tests pass under PayPal: [email protected] |
Git-on-my-level
left a comment
There was a problem hiding this comment.
Re-review at head c3daf2a — both requested changes are in; verdict: approve.
plugin-oauth-hardening-testsis now registered in .github/checks-manifest.yaml (commandpython3 plugins/oauth/test_oauth_hardening.py, triggers plugins/oauth/** + the manifest, lanes local+ci, with reason) — appended under the existing checks list, so it registers, per my earlier note.- Unused
Optionalimport dropped from plugins/oauth/client.py. - Re-verified locally at head:
python3 -S plugins/oauth/test_oauth_hardening.py→ 23/23 OK, andpython3 plugins/oauth/test_client_log_safety.py→ 5/5 OK (#13778 invariant holds). No other files changed since my prior file-by-file review (delta was exactly the manifest entry + the import line).
Merge authorization is covered: David has explicitly signed off on landing the remaining plugin-hardening PRs in this batch. CI green on this head, merge-tree clean, no open threads.
Summary
Fixes #14257
Harden Notion CRM OAuth plugin (
plugins/oauth) against network hangs, unhandled JSON decode errors on HTTP 502/504 responses, null/malformed conversation fields, and fragile database schema handling:plugins/oauth/client.py:• Added
DEFAULT_TIMEOUT = 10.0acrossNotionClient.get_database,get_access_token, andget_databases_edited_time_desc, preventing worker threads from blocking indefinitely on unresponsive endpoints.• Wrapped network requests in
try ... except requests.RequestExceptionto return structured 500 error envelopes rather than uncaught server crashes.• Added
_safe_jsonhelper to guard againstJSONDecodeErrorwhen Notion or intermediary proxies return HTML error pages (e.g. 502 Bad Gateway).• Hardened
from_dictandmulti_from_dictinNotionDatabasePropertyModel,NotionDatabaseModel, andNotionOAuthModelagainstNone, empty dicts, missing keys, and non-list structures.• Corrected return type annotation in
NotionOAuthModel.from_dict.plugins/oauth/conversation_created.py:• Added
_build_notion_conversation_propertieshelper with complete null guards forstructured,title,emoji,speakers,category, andoverview.• Guarded
emojidecoding against bothUnicodeEncodeErrorandUnicodeDecodeError, safely falling back to raw emoji or default🧠.• Handled
Nonesegments and missing/Nonespeakerattributes during speaker set deduplication.• Clamped conversation
durationto non-negative values (max(0, int(delta))) and guarded against missingstarted_atorfinished_at.• Added
DEFAULT_TIMEOUTand request exception handling tocreate_notion_row.• Hardened
validate_databaseagainst non-dict responses and missing schema properties.Hermetic Test Suite:
• Authored
plugins/oauth/test_oauth_hardening.pywith 23 unit tests running under pure standard library Python (python3 -S) in 0.008s with zero third-party dependencies.Product invariants affected
none
How it was verified
• Ran hermetic test suite under stdlib:
python3 -S plugins/oauth/test_oauth_hardening.py
23 tests passed in 0.008s
• Ran standard test suite:
python3 plugins/oauth/test_oauth_hardening.py
23 tests passed in 0.009s
• Ran existing log safety suite:
python3 plugins/oauth/test_client_log_safety.py
5 tests passed in 1.25s
• Ran repo preflight:
bash scripts/pr-preflight --lane ci --pr-body-file /tmp/pr_body_oauth.md
All checks passed
Tests
•
plugins/oauth/test_oauth_hardening.py:•
NotionClientHardeningTests(get_database default & custom timeouts, request exceptions, 502 HTML non-json error; get_access_token default timeout, request exception; get_databases_edited_time_desc timeout, missing results key)•
NotionModelsHardeningTests(property model defensive & valid, database model defensive & malformed properties, multi_from_dict non-list & invalid items, oauth model defensive)•
ConversationPayloadHardeningTests(null structured properties fallback, emoji unicode decoding safety, speaker dedup & null segments, duration clamping & null timestamps, create_notion_row success & timeout, validate_database missing fields & invalid schema)Failure class (fixes)
Failure-Class: none
Claim: PayPal [email protected]