Skip to content

Commit 652786b

Browse files
e3ndrWanneSimon
andcommitted
Core: Disable nested bridge access by default.
There is now an additional method. Security reasons :) Co-Authored-By: wanne seville <29028687+WanneSimon@users.noreply.github.com>
1 parent 74c9ac7 commit 652786b

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

core/src/main/java/dev/webview/webview_java/Webview.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ public class Webview implements Closeable, Runnable {
4646
@Deprecated
4747
public long $pointer;
4848

49-
// private String initScript = "";
50-
5149
/**
5250
* Creates a new Webview. <br/>
5351
* The default size will be set, and if the size is set again before loading the
@@ -186,27 +184,44 @@ public void setFixedSize(int width, int height) {
186184
}
187185

188186
/**
189-
* Sets the script to be run on page load.
187+
* Sets the script to be run on page load. Defaults to no nested access (false).
190188
*
191189
* @implNote This get's called AFTER window.load.
192190
*
193191
* @param script
192+
*
193+
* @see #setInitScript(String, boolean)
194194
*/
195195
public void setInitScript(@NonNull String script) {
196+
this.setInitScript(script, false);
197+
}
198+
199+
/**
200+
* Sets the script to be run on page load.
201+
*
202+
* @implNote This get's called AFTER window.load.
203+
*
204+
* @param script
205+
* @param allowNestedAccess whether or not to inject the script into nested
206+
* iframes.
207+
*/
208+
public void setInitScript(@NonNull String script, boolean allowNestedAccess) {
196209
script = String.format(
197-
"(async () => {"
210+
"(() => {"
198211
+ "try {"
212+
+ "if (window.top == window.self || %b) {"
199213
+ "%s"
214+
+ "}"
200215
+ "} catch (e) {"
201216
+ "console.error('[Webview]', 'An error occurred whilst evaluating init script:', %s, e);"
202217
+ "}"
203218
+ "})();",
219+
allowNestedAccess,
204220
script,
205221
'"' + _WebviewUtil.jsonEscape(script) + '"'
206222
);
207223

208224
N.webview_init($pointer, script);
209-
// this.initScript = script;
210225
}
211226

212227
/**
@@ -219,13 +234,11 @@ public void eval(@NonNull String script) {
219234
N.webview_eval(
220235
$pointer,
221236
String.format(
222-
"(async () => {"
223-
+ "try {"
237+
"try {"
224238
+ "%s"
225239
+ "} catch (e) {"
226240
+ "console.error('[Webview]', 'An error occurred whilst evaluating script:', %s, e);"
227-
+ "}"
228-
+ "})();",
241+
+ "}",
229242
script,
230243
'"' + _WebviewUtil.jsonEscape(script) + '"'
231244
)

0 commit comments

Comments
 (0)