Skip to content

fix(swift,extract): harden WebView security, concurrency, Readwise, and Readability - #3

Merged
willtmc merged 1 commit into
mainfrom
fix/swift-security-concurrency-and-readability
Nov 25, 2026
Merged

willtmc merged 1 commit into
mainfrom
fix/swift-security-concurrency-and-readability

Conversation

@willtmc

@willtmc willtmc commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Summary

Hardens TwoUp's Swift host, WebView boundaries, Readwise integration, and article extraction pipeline while extracting pure testable helpers into Core.swift:

  • Pure testable helpers (Core.swift):
    • normalizeURL(_:) validates http/https schemes and rejects non-host strings (bare words like "hello" or CLI flags like "en") while allowing valid domains, localhost, IPv4, and bracketed IPv6.
    • parseLaunchURL(arguments:) skips macOS LaunchServices / defaults flag pairs (-AppleLanguages en, -NS*, -psn_*) and accepts only valid URLs.
    • isAllowedExternalURL(_:) strictly allowlists http, https, and mailto before handing links to NSWorkspace.shared.open.
    • readwiseEndpointURL(path:queryItems:), readwiseUpdateURL(docID:), and readwiseReaderBaseURL() eliminate all Readwise URL force-unwraps (!) and reject path traversal / percent-encode docID.
    • matchesExpectedReaderURL(_:expected:), slimDoc(_:), jsonLiteral(_:), and themeWindowAppearance(theme:).
  • WebView security & Swift concurrency (main.swift):
    • Resolves all Swift 5 -warn-concurrency warnings (@MainActor on Keychain, readwiseRequest using async/await URLSession.shared.data(for:), @MainActor WKNavigationDelegate decision handler, Task { @MainActor in ... } workspace observer, MainActor.assumeIsolated entry point).
    • Locks down reader WKNavigationDelegate to only allow TwoUp's own loadHTMLString loads and cancel unexpected navigations; checks message.webView === reader and message.frameInfo.isMainFrame in WKScriptMessageHandler.
    • Isolates loader with WKWebsiteDataStore.nonPersistent().
    • Moves synchronous NSAppleScript calls in grabBrowserTab() off the main thread (Task.detached) after rendering the loading screen.
    • Calls stopLoader() in showStart() and openDoc(), checks evaluateJavaScript errors in extract(), removes dead extracting set and unused htmlError, adds queueFetchToken to drop stale queue responses, and checks OSStatus + kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly in Keychain.set.
    • Persists window frame (TwoUpMainWindow), fullscreen preference (defaulting to fullscreen on first run), minimum window size (640x480), queueLocation preference, View menu Zoom items (⌘+/⌘-/⌘0), focusUrl on ⌘L, article.dir / D.dir, and theme/appearance chrome sync (drawsBackground = false).
    • Breaks WKUserContentController retain cycle with WeakScriptMessageHandler and applicationWillTerminate cleanup.
  • Readability & extraction (Resources/Readability.js, Resources/extract.js):
    • Upgrades and pins Resources/Readability.js to @mozilla/readability@0.6.0 (fixing the JSON-LD @graph this.REGEXPS bug) with version/source header.
    • Preserves #hash anchor/footnote links when parsing browser-grabbed HTML, promotes lazy <picture><source data-srcset> and <img> attributes, and returns dir: article.dir || "".

Verification

  • swiftc -typecheck -swift-version 5 -warn-concurrency Core.swift main.swift — 0 warnings
  • node --check Resources/extract.js && node --check Resources/Readability.js — passed
  • ./build.sh --build-only — passed (build/TwoUp.app built cleanly)
  • codesign --verify --deep --strict build/TwoUp.app — passed
  • Ran standalone edge-case smoke tests against Core.swift (URL normalization, CLI flags, external scheme allowlist, Readwise URL safety, slimDoc, jsonLiteral, themeWindowAppearance) — passed

@willtmc
willtmc merged commit 1ef36b6 into main Nov 25, 2026
1 check passed
@willtmc
willtmc deleted the fix/swift-security-concurrency-and-readability branch November 25, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant