From 1d38e7ae7377b8d2c5e438bc38abf2a045c3a8fd Mon Sep 17 00:00:00 2001 From: renaud gaudin Date: Thu, 26 Mar 2020 12:06:36 +0000 Subject: [PATCH 1/2] Add external links blocking in serve In many use cases, it is not wanted to have user accidentaly click on external links and leave the served ZIM content. This could be because the result is unpredictible (reader not implementing this properly) or because the serve user knows there's no backup internet connexion or because there is an induced cost behind external links that doesn't affect served content. using a new flag (`blockExternalLinks`) on `Response`/`setTaskBar`, a piece of JS code is injected into the taskbar code. This code adds a JS handler on all link click events and verifies the destination. If the destination appears to be an external link (1), the link target is changed to a specific URL: ``` /external?source= ``` (1) external is a link that's not on the same origin and starts with either `http:` `https:` or `//`. Server implements a new handler on `/external` that displays a new page (`captured_external.html`) which returns a generic message explaining the situation and offering to click on the link again should the user really want to. This is done by specifically asking `set_taskbar` to not block external requests on that page. This approach allows integrators using a reverse proxy to handle that endpoint differently (rebrand it) 1. `Server` now has an `m_blockExternalLinks` defaulting to `false` 1. `Server.setTaskbar` is extended to support an additional bool to set the variable. 1. `Response` now has an `m_blockExternalLinks` 1. `Response` constr expects an additional bool for `blockExternalLinks`. 1. `Response.set_taskbar` is extended to support an additional bool to set the variable. 1. JNI/Java Wrapper reflects the extensions. 1. New resource file `templates/block_external.js` (included in head_part). Should it be in skin? 1. New resource file `templates/captured_external.html` for `handle_captured_external()` 1. Added a comment on `head_part.html` to help with JS insertion at the right place 1. `introduce_taskbar()` conditionnaly inserts the JS inside the taskbar --- src/wrapper/java/kiwixserver.cpp | 6 ++++++ src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixServer.java | 1 + 2 files changed, 7 insertions(+) diff --git a/src/wrapper/java/kiwixserver.cpp b/src/wrapper/java/kiwixserver.cpp index 7242c4c..1fb2f36 100644 --- a/src/wrapper/java/kiwixserver.cpp +++ b/src/wrapper/java/kiwixserver.cpp @@ -85,6 +85,12 @@ Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject obj, jboo SERVER->setTaskbar(withTaskbar, withLibraryButton); } +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject obj, jboolean withTaskbar, jboolean withLibraryButton, jboolean blockExternalLinks) +{ + SERVER->setTaskbar(withTaskbar, withLibraryButton, blockExternalLinks); +} + JNIEXPORT jboolean JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_start(JNIEnv* env, jobject obj) { diff --git a/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixServer.java b/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixServer.java index 578e2f4..2fc49e4 100644 --- a/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixServer.java +++ b/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixServer.java @@ -33,6 +33,7 @@ public class JNIKiwixServer public native void setNbThreads(int nbTreads); public native void setTaskbar(boolean withTaskBar, boolean witLibraryButton); + public native void setTaskbar(boolean withTaskBar, boolean witLibraryButton, boolean blockExternalLinks); public native boolean start(); From 10523168bcd90541875ffc0169d35893b45334b5 Mon Sep 17 00:00:00 2001 From: renaud gaudin Date: Fri, 27 Mar 2020 11:25:39 +0000 Subject: [PATCH 2/2] moved blockExternalLink outside of taskbar - `setBlockExternalLinks()` on server - zero-dependency JS code - JS script added in `inject_externallinks_blocker()` - changed URL to `/catch/external?source=` --- src/wrapper/java/kiwixserver.cpp | 4 ++-- src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixServer.java | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/wrapper/java/kiwixserver.cpp b/src/wrapper/java/kiwixserver.cpp index 1fb2f36..b64f618 100644 --- a/src/wrapper/java/kiwixserver.cpp +++ b/src/wrapper/java/kiwixserver.cpp @@ -86,9 +86,9 @@ Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject obj, jboo } JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject obj, jboolean withTaskbar, jboolean withLibraryButton, jboolean blockExternalLinks) +Java_org_kiwix_kiwixlib_JNIKiwixServer_setBlockExternalLinks(JNIEnv* env, jobject obj, jboolean blockExternalLinks) { - SERVER->setTaskbar(withTaskbar, withLibraryButton, blockExternalLinks); + SERVER->setBlockExternalLinks(blockExternalLinks); } JNIEXPORT jboolean JNICALL diff --git a/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixServer.java b/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixServer.java index 2fc49e4..11c5f0d 100644 --- a/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixServer.java +++ b/src/wrapper/java/org/kiwix/kiwixlib/JNIKiwixServer.java @@ -33,7 +33,8 @@ public class JNIKiwixServer public native void setNbThreads(int nbTreads); public native void setTaskbar(boolean withTaskBar, boolean witLibraryButton); - public native void setTaskbar(boolean withTaskBar, boolean witLibraryButton, boolean blockExternalLinks); + + public native void setBlockExternalLinks(boolean blockExternalLinks); public native boolean start();