Skip to content

fix(native): fail safely on restricted pubs and bad locators - #339

Open
ddfreiling wants to merge 1 commit into
Notalib:mainfrom
ddfreiling:fix/native-crash-hardening
Open

ddfreiling wants to merge 1 commit into
Notalib:mainfrom
ddfreiling:fix/native-crash-hardening

Conversation

@ddfreiling

Copy link
Copy Markdown
Member

Summary

A static pass over the native code listed force-unwraps, try!, as! and !! that could crash the app (follow-up to the LYT4 2.26.7 Sentry triage). Native Sentry stacks are unsymbolicated, so none of these is confirmed by a crash report; this is hardening. Each site was checked against its callers: reachable ones now fail safely, the rest are listed below with the reason they are safe.

Change

  • Restricted publications (iOS, Android): openPublication now rejects a publication that is DRM-protected and not unlocked (publication.isRestricted), and throws ReadiumException with ReadiumErrorCode.forbidden; the toolkit's protection error is in details.message on both platforms. Both toolkits add a fallback content protection that marks LCP/Adobe files restricted, and the plugin links no ReadiumLCP, so such files opened fine. On iOS, EPUBNavigatorViewController / PDFNavigatorViewController then threw into a try! and crashed. On Android the reader opened but could not decrypt. loadPublication (manifest only) is unchanged.
  • iOS PDF go: parses the locator with guard … try? and returns false, as the EPUB handler already does.
  • iOS EPUB and PDF go: read arguments with first / dropFirst().first instead of [0] / [1], so a short argument list can't trap on an out-of-range index.
  • Android PdfNavigator: afterFragmentStarted waits on the fragment the caller already captured instead of re-reading pdfNavigator!!, which dispose() can null between the caller's check and the main-thread hop.
  • Android EpubReaderFragment: navigatorFactory!! becomes ?: return, matching PdfReaderFragment.
  • Android plugin attach: AssetManager.list() is @Nullable; paths!! becomes isNullOrEmpty(). This runs on every plugin attach.

Findings

Finding Result
iOS PDFReaderView go: try! Locator(…)! Fixed
iOS EPUB/PDF go: args[1] index Fixed (found in review)
iOS try! EPUB/PDFNavigatorViewController Fixed upstream of it: restricted pubs rejected at open
iOS as! method-channel arg casts Left as-is
iOS try! Decoration.Style(fromMap:) Left as-is
iOS Readium.swift LCP try! / as! DRMContext Left as-is: not compiled
iOS spotlight as! HighlightConfig Left as-is
iOS ReadiumError as! Left as-is
Android go: Locator.fromJSON(…)!! Left as-is: already caught
Android EpubReaderFragment navigatorFactory!! Fixed (defensive)
Android PdfNavigator pdfNavigator!!.started Fixed
Android listAssetFiles paths!! Fixed
Other Android !! Left as-is

Reasons for the sites left as-is:

  • Method-channel as! casts and try! Decoration.Style(fromMap:): the only caller is this package's Dart side, released in lockstep, which always sends these shapes (ReaderDecorationStyle.toJson() always includes style, the only key fromMap requires). Init-time creationParams casts are in the init regions changed by the overlapping PR.
  • LCP block in Readium.swift: it sits under #if LCP, which nothing defines; ReadiumLCP is commented out in the podspec and absent from Package.swift. It is also lazy, not eager.
  • Spotlight as! HighlightConfig: every Decoration.Style the plugin builds (init(withStyle:), .highlight, .underline) uses HighlightConfig.
  • ReadiumError as!: each cast follows an is check on the same value.
  • Android go !!: onMethodCall catches the NPE and returns InvalidArgument (the catch comment names this case).
  • Android EpubReaderFragment after process death: vm is a plain field, not a Jetpack ViewModel, so a restored fragment has vm == null; onViewCreated and onResume return early on it and never reach the !!. The widget also removes a leftover fragment by tag on init. The !! was not reachable; the change is for symmetry with PDF.
  • Other Android !!: ensureNavigator() (TTS, audiobook) follows an initNavigator() that assigns or throws; mediaServiceFacade!! sits inside a try/catch; binder!! follows its assignment inside a serial queue; the retriever/opener caches are assigned right before the read; url.filename!! follows an extension check; lastTocMatch!! and duration!! follow null checks on locals; context!! and classLoader!! are never null there; syncTextLocator!! follows takeIf { it.syncTextLocator != null }.

Review notes

  • Behaviour change: opening a DRM-protected publication now fails on Android too (it used to open an unreadable reader), for parity with iOS. Web has no DRM path.
  • Overlap: touches FlutterReadiumPlugin.swift (openPublication handler), PDFReaderView.swift and EPUBReaderView+MethodChannel.swift (onMethodCall only), EpubReaderFragment.kt (fragment's own attachNavigator) and PdfNavigator.kt (afterFragmentStarted, go, updatePreferences). It stays out of the regions changed by fix/ios-reader-view-without-publication (ReadiumReaderViewFactory.swift, the reader inits), fix/android-navigator-fragment-lifecycle (Utils.kt, navigators' dispose() / attachNavigator()) and fix(reader): let the newest reader widget own the visual navigator #332 (navigators' dispose()). Expect at most a textual rebase near PdfNavigator.dispose().

Verification

  • ./gradlew :flutter_readium:compileDebugKotlin and :flutter_readium:testDebugUnitTest --tests PublicationErrorTest pass (new test: Restricted maps to the forbidden wire code with the protection error as details.message).
  • ktlint clean on the edited Kotlin files.
  • flutter build ios --simulator --debug for the example app succeeds.
  • bin/changelog check passes.

Not covered

  • Not exercised at runtime: there is no DRM-protected fixture in readium-test-resources, and no repro for the PDF go or PdfNavigator race. Behaviour is verified by reading the code and the pinned toolkit sources (swift-toolkit 3.11.1 _FallbackContentProtection, kotlin-toolkit 3.4.0 FallbackContentProtection).
  • No Swift unit test: the changed paths need an open publication and a live navigator.

🤖 Generated with Claude Code

openPublication now rejects a publication that is DRM-protected and
not unlocked (isRestricted) with a forbidden error on iOS and Android.
Both toolkits mark LCP/Adobe files restricted through their fallback
content protection; on iOS the EPUB/PDF navigators then threw into a
try! and crashed, on Android the reader opened but could not decrypt.
The toolkit's protection error goes in details.message.

Also:
- iOS PDF go: parse the locator with try? and return false, like EPUB.
- iOS EPUB/PDF go: read args with first/dropFirst() so a short list
  can't trap on an out-of-range index.
- Android PdfNavigator: await the fragment the caller captured instead
  of re-reading pdfNavigator!!, which dispose() can null meanwhile.
- Android EpubReaderFragment: navigatorFactory!! becomes ?: return,
  matching PdfReaderFragment.
- Android plugin attach: AssetManager.list() is nullable; drop paths!!.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ddfreiling ddfreiling added the low-priority This issue or PR is of low priority, do not take it first. label Nov 18, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

low-priority This issue or PR is of low priority, do not take it first.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant