Skip to content

Conversation

@BrayanDSO
Copy link
Member

@BrayanDSO BrayanDSO commented Apr 9, 2024

Purpose / Description

Loading files with file:/ scheme in much faster in an Android Webview, and that's what 2.15.6 did by setting the Webview base url to the media directory.

But in 2.16/2.17, in order to enable POST requests to be done, the base url was set to localhost and the files were delivered by the server. After noticing that loading files was slower with the server, a WebViewAssetLoader was used, but it was still slower than file:/. The next alternative was parsing the card's HTML to use file:/ for media sources, but that leads to CORS issues and sometimes HTML parsing issues.

So, here's what I believe that should be the definitive solution, which is to remap POST requests base url, so the Webview base url can be set to the media directory so the 2.15.6 behavior is restored.

Fixes

Approach

In the commits

How Has This Been Tested?

Emulator 34:

Tested by adding console.log("foo"); into Custom scheduling deck option then seeing if it appeared in Logcat

Also checked if media, specially videos and audios, were properly loaded

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

@BrayanDSO BrayanDSO added Blocked by dependency Currently blocked by some other dependent / related change Needs a new dev For PR that were good start but the original dev' left. Any dev can take over and finish it. labels Apr 9, 2024
@BrayanDSO BrayanDSO removed the Needs a new dev For PR that were good start but the original dev' left. Any dev can take over and finish it. label Apr 9, 2024
@mikehardy mikehardy removed the Blocked by dependency Currently blocked by some other dependent / related change label Apr 20, 2024
@mikehardy
Copy link
Member

Should be good to go now

@BrayanDSO BrayanDSO changed the title fix: Use media directory as the CardViewers' base url fix: Use media directory as the base url of the CardViewers Apr 20, 2024
that makes the baseUrl always the same, so `localStorage` works again

also loads media with the `file` scheme by default, which is faster
} else {
newChunkedResponse(status, mimeType, ByteArrayInputStream(data))
}.apply {
addHeader("Access-Control-Allow-Origin", "*")
Copy link
Member

Choose a reason for hiding this comment

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

[security] Is it feasible to limit this so other apps on-device can't reasonably access the server?

Copy link
Member Author

@BrayanDSO BrayanDSO Apr 20, 2024

Choose a reason for hiding this comment

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

Tried it once but it didn't work immediately. Since my timeblock was gone and this was the same of what 2.16 did, I left it as is.

https://github.com/ankidroid/Anki-Android/blob/release-2.16/AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.kt#L586

Copy link
Member

Choose a reason for hiding this comment

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

Do you feel we should block this PR on this?

If we have MANAGE_EXTERNAL_STORAGE, we must not allow access to files outside collection.media, and I would block on this

Copy link
Member

Choose a reason for hiding this comment

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

I've extracted this to: #16230

Copy link
Member Author

@BrayanDSO BrayanDSO Apr 20, 2024

Choose a reason for hiding this comment

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

Took a look and since the Reviewer now uses file:/// as base url, origin is null so only * can be used as the value of Access-Control-Allow-Origin.

On a quick assessment, only POST requests are allowed. In the reviewer only getSchedulingStatesWithContext and setSchedulingStates are enabled. So, I don't see any danger right now. Card templates and deck options' custom scheduling already can use those methods but aren't considered a security vulnerability

@david-allison david-allison added Needs Second Approval Has one approval, one more approval to merge and removed Needs Review labels Apr 20, 2024
Copy link
Member

@mikehardy mikehardy left a comment

Choose a reason for hiding this comment

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

let's do this - thanks @BrayanDSO (and David)

@mikehardy mikehardy added this pull request to the merge queue Apr 20, 2024
Merged via the queue into ankidroid:main with commit e55cae2 Apr 20, 2024
@github-actions github-actions bot removed the Needs Second Approval Has one approval, one more approval to merge label Apr 20, 2024
@github-actions github-actions bot added this to the 2.18 release milestone Apr 20, 2024
@BrayanDSO BrayanDSO deleted the prep branch April 20, 2024 20:51
@BrayanDSO BrayanDSO restored the prep branch May 10, 2024 12:47
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.

[BUG]: Cloze cards showing 'data-ordinal="1"' when using latex and images [BUG]: localStorage does not seem to be persistent in version 2.17.5

3 participants