Repository navigation
fix(native): fail safely on restricted pubs and bad locators - #339
Open
ddfreiling wants to merge 1 commit into
Open
ddfreiling wants to merge 1 commit into
ddfreiling wants to merge 1 commit into
Conversation
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
openPublicationnow rejects a publication that is DRM-protected and not unlocked (publication.isRestricted), and throwsReadiumExceptionwithReadiumErrorCode.forbidden; the toolkit's protection error is indetails.messageon 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/PDFNavigatorViewControllerthen threw into atry!and crashed. On Android the reader opened but could not decrypt.loadPublication(manifest only) is unchanged.go: parses the locator withguard … try?and returnsfalse, as the EPUB handler already does.go: read arguments withfirst/dropFirst().firstinstead of[0]/[1], so a short argument list can't trap on an out-of-range index.PdfNavigator:afterFragmentStartedwaits on the fragment the caller already captured instead of re-readingpdfNavigator!!, whichdispose()can null between the caller's check and the main-thread hop.EpubReaderFragment:navigatorFactory!!becomes?: return, matchingPdfReaderFragment.AssetManager.list()is@Nullable;paths!!becomesisNullOrEmpty(). This runs on every plugin attach.Findings
PDFReaderViewgo:try! Locator(…)!go:args[1]indextry! EPUB/PDFNavigatorViewControlleras!method-channel arg caststry! Decoration.Style(fromMap:)Readium.swiftLCPtry!/as! DRMContextas! HighlightConfigReadiumErroras!go:Locator.fromJSON(…)!!EpubReaderFragmentnavigatorFactory!!PdfNavigatorpdfNavigator!!.startedlistAssetFilespaths!!!!Reasons for the sites left as-is:
as!casts andtry! Decoration.Style(fromMap:): the only caller is this package's Dart side, released in lockstep, which always sends these shapes (ReaderDecorationStyle.toJson()always includesstyle, the only keyfromMaprequires). Init-timecreationParamscasts are in theinitregions changed by the overlapping PR.Readium.swift: it sits under#if LCP, which nothing defines; ReadiumLCP is commented out in the podspec and absent fromPackage.swift. It is alsolazy, not eager.as! HighlightConfig: everyDecoration.Stylethe plugin builds (init(withStyle:),.highlight,.underline) usesHighlightConfig.ReadiumErroras!: each cast follows anischeck on the same value.go!!:onMethodCallcatches the NPE and returnsInvalidArgument(the catch comment names this case).EpubReaderFragmentafter process death:vmis a plain field, not a JetpackViewModel, so a restored fragment hasvm == null;onViewCreatedandonResumereturn 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.!!:ensureNavigator()(TTS, audiobook) follows aninitNavigator()that assigns or throws;mediaServiceFacade!!sits inside atry/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!!andduration!!follow null checks on locals;context!!andclassLoader!!are never null there;syncTextLocator!!followstakeIf { it.syncTextLocator != null }.Review notes
FlutterReadiumPlugin.swift(openPublicationhandler),PDFReaderView.swiftandEPUBReaderView+MethodChannel.swift(onMethodCallonly),EpubReaderFragment.kt(fragment's ownattachNavigator) andPdfNavigator.kt(afterFragmentStarted,go,updatePreferences). It stays out of the regions changed byfix/ios-reader-view-without-publication(ReadiumReaderViewFactory.swift, the readerinits),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 nearPdfNavigator.dispose().Verification
./gradlew :flutter_readium:compileDebugKotlinand:flutter_readium:testDebugUnitTest --tests PublicationErrorTestpass (new test:Restrictedmaps to theforbiddenwire code with the protection error asdetails.message).ktlintclean on the edited Kotlin files.flutter build ios --simulator --debugfor the example app succeeds.bin/changelog checkpasses.Not covered
readium-test-resources, and no repro for the PDFgoorPdfNavigatorrace. Behaviour is verified by reading the code and the pinned toolkit sources (swift-toolkit 3.11.1_FallbackContentProtection, kotlin-toolkit 3.4.0FallbackContentProtection).🤖 Generated with Claude Code