Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force HTTP links to HTTPS #1214

Merged
merged 3 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1506,3 +1506,14 @@ fun Context.startActivitySafe(intent: Intent) {
Toast.makeText(this, this.getText(R.string.no_activity_found), Toast.LENGTH_SHORT).show()
}
}

/**
* This function rewrites HTTP URLs to HTTPS
*/
fun String.toHttps(): String {
return if (this.startsWith("http://", true)) {
this.replaceFirst("http", "https", true)
} else {
this
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import coil.imageLoader
import com.jerboa.JerboaAppState
import com.jerboa.convertSpToPx
import com.jerboa.util.markwon.BetterLinkMovementMethod
import com.jerboa.util.markwon.ForceHttpsPlugin
import com.jerboa.util.markwon.MarkwonLemmyLinkPlugin
import com.jerboa.util.markwon.MarkwonSpoilerPlugin
import io.noties.markwon.AbstractMarkwonPlugin
Expand Down Expand Up @@ -90,9 +91,11 @@ object MarkdownHelper {
val loader = context.imageLoader
// main markdown parser has coil + html on
markwon = Markwon.builder(context)
.usePlugin(ForceHttpsPlugin())
// email urls interfere with lemmy links
.usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS))
.usePlugin(MarkwonLemmyLinkPlugin())
.usePlugin(MarkwonSpoilerPlugin(true))
.usePlugin(StrikethroughPlugin.create())
.usePlugin(TablePlugin.create(context))
.usePlugin(ClickableCoilImagesPlugin.create(context, loader, appState))
Expand All @@ -116,7 +119,6 @@ object MarkdownHelper {
}
}
})
.usePlugin(MarkwonSpoilerPlugin(true))
.build()

// no image parser has html off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ import com.jerboa.nsfwCheck
import com.jerboa.rememberJerboaAppState
import com.jerboa.siFormat
import com.jerboa.toEnum
import com.jerboa.toHttps
import com.jerboa.ui.components.common.ActionBarButton
import com.jerboa.ui.components.common.ActionBarButtonAndBadge
import com.jerboa.ui.components.common.CircularIcon
Expand Down Expand Up @@ -349,7 +350,7 @@ fun PostTitleAndImageLink(
showIfRead: Boolean,
) {
// This was tested, we know it exists
val url = postView.post.url!!
val url = postView.post.url!!.toHttps()

Column(
modifier = Modifier.padding(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import coil.ImageLoader
import coil.request.Disposable
import coil.request.ImageRequest
import com.jerboa.JerboaAppState
import com.jerboa.toHttps
import io.noties.markwon.MarkwonConfiguration
import io.noties.markwon.MarkwonSpansFactory
import io.noties.markwon.RenderProps
import io.noties.markwon.SpanFactory
import io.noties.markwon.image.AsyncDrawable
import io.noties.markwon.image.AsyncDrawableSpan
import io.noties.markwon.image.ImageProps
import io.noties.markwon.image.ImageSpanFactory
import org.commonmark.node.Image

Expand Down Expand Up @@ -55,7 +58,7 @@ class ClickableCoilImagesPlugin(coil: CoilStore, imageLoader: ImageLoader, priva
}
}

class ClickableImageFactory(val appState: JerboaAppState) : ImageSpanFactory() {
internal class ClickableImageFactory(val appState: JerboaAppState) : HttpsImageSpanFactory() {

override fun getSpans(configuration: MarkwonConfiguration, props: RenderProps): Any {
val image = super.getSpans(configuration, props) as AsyncDrawableSpan
Expand All @@ -69,3 +72,22 @@ class ClickableImageFactory(val appState: JerboaAppState) : ImageSpanFactory() {
return arrayOf(image, clickSpan)
}
}

/**
* Custom implementation of [ImageSpanFactory] that rewrites all http:// links to https://
*/
internal open class HttpsImageSpanFactory : SpanFactory {
override fun getSpans(configuration: MarkwonConfiguration, props: RenderProps): Any? {
return AsyncDrawableSpan(
configuration.theme(),
AsyncDrawable(
ImageProps.DESTINATION.require(props).toHttps(),
configuration.asyncDrawableLoader(),
configuration.imageSizeResolver(),
ImageProps.IMAGE_SIZE[props],
),
AsyncDrawableSpan.ALIGN_BOTTOM,
ImageProps.REPLACEMENT_TEXT_IS_LINK[props, false],
)
}
}
26 changes: 26 additions & 0 deletions app/src/main/java/com/jerboa/util/markwon/ForceHttpsPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.jerboa.util.markwon

import com.jerboa.toHttps
import io.noties.markwon.AbstractMarkwonPlugin
import io.noties.markwon.MarkwonSpansFactory
import io.noties.markwon.core.CoreProps
import io.noties.markwon.core.spans.LinkSpan
import org.commonmark.node.Link

/**
* A markwon plugin that rewrites all http:// links to https://
*/

class ForceHttpsPlugin : AbstractMarkwonPlugin() {
override fun configureSpansFactory(builder: MarkwonSpansFactory.Builder) {
builder.setFactory(
Link::class.java,
) { configuration, props ->
LinkSpan(
configuration.theme(),
CoreProps.LINK_DESTINATION.require(props).toHttps(),
configuration.linkResolver(),
)
}
}
}
7 changes: 7 additions & 0 deletions app/src/test/java/com/jerboa/UtilsKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,11 @@ class UtilsKtTest {
assertEquals(-1, compareVersions("0.1.2-alpha1", "0.1.2-beta1"))
assertEquals(1, compareVersions("0.1.2-beta1", "0.1.2-alpha2"))
}

@Test
fun rewriteHttpToHttps() {
assertEquals("https://example.com", "http://example.com".toHttps())
assertEquals("https://example.com", "https://example.com".toHttps())
assertEquals("example.com", "example.com".toHttps())
}
}