Skip to content

Commit 54d9d30

Browse files
committed
isSecureContext added (issue #573)
1 parent 00aacba commit 54d9d30

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

src/main/java/org/htmlunit/javascript/host/Window.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4197,6 +4197,32 @@ public void put(final String name, final Scriptable start, final Object value) {
41974197
}
41984198
super.put(name, start, value);
41994199
}
4200+
4201+
/**
4202+
* @return a boolean indicating whether the current context is secure (true) or not (false).
4203+
*/
4204+
@JsxGetter({CHROME, EDGE, FF, FF_ESR})
4205+
public Object getIsSecureContext() {
4206+
final Page page = getWebWindow().getEnclosedPage();
4207+
if (page != null) {
4208+
final String protocol = page.getUrl().getProtocol();
4209+
if ("https".equals(protocol)
4210+
|| "wss".equals(protocol)
4211+
|| "file".equals(protocol)) {
4212+
return true;
4213+
}
4214+
4215+
final String host = page.getUrl().getHost();
4216+
if ("localhost".equals(host)
4217+
|| "localhost.".equals(host)
4218+
|| host.endsWith(".localhost")
4219+
|| host.endsWith(".localhost.")) {
4220+
return true;
4221+
}
4222+
}
4223+
4224+
return false;
4225+
}
42004226
}
42014227

42024228
class HTMLCollectionFrames extends HTMLCollection {

src/test/java/org/htmlunit/WebDriverTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1538,7 +1538,7 @@ private static Server buildServer(final int port) {
15381538
// https://github.com/HtmlUnit/htmlunit/issues/462
15391539
// https://github.com/eclipse/jetty.project/issues/2503
15401540
// the value for the QueuedThreadPool are validated,
1541-
// let's amke another try with the defaults
1541+
// let's make another try with the defaults
15421542
//
15431543
// final QueuedThreadPool threadPool = new QueuedThreadPool(5, 2);
15441544
//

src/test/java/org/htmlunit/javascript/host/Window2Test.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
import static org.htmlunit.junit.BrowserRunner.TestedBrowser.FF_ESR;
1919
import static org.htmlunit.junit.BrowserRunner.TestedBrowser.IE;
2020

21+
import java.io.ByteArrayOutputStream;
22+
import java.io.File;
23+
import java.io.OutputStreamWriter;
24+
import java.net.URL;
25+
import java.nio.charset.StandardCharsets;
26+
27+
import org.apache.commons.io.FileUtils;
28+
import org.htmlunit.CookieManager4Test;
2129
import org.htmlunit.WebDriverTestCase;
2230
import org.htmlunit.html.HtmlPageTest;
2331
import org.htmlunit.junit.BrowserRunner;
@@ -28,6 +36,7 @@
2836
import org.junit.Test;
2937
import org.junit.runner.RunWith;
3038
import org.openqa.selenium.By;
39+
import org.openqa.selenium.JavascriptExecutor;
3140
import org.openqa.selenium.WebDriver;
3241
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
3342

@@ -2663,4 +2672,97 @@ public void eventPropertyReplaced() throws Exception {
26632672

26642673
loadPageVerifyTitle2(html);
26652674
}
2675+
2676+
/**
2677+
* @throws Exception if an error occurs
2678+
*/
2679+
@Test
2680+
@Alerts(DEFAULT = "true",
2681+
IE = "not available")
2682+
public void isSecureContextLocalhost() throws Exception {
2683+
final String html
2684+
= "<html><body><script>\n"
2685+
+ LOG_TITLE_FUNCTION
2686+
+ " log(window.hasOwnProperty('isSecureContext') ? isSecureContext : 'not available');\n"
2687+
+ "</script></body></html>";
2688+
2689+
loadPageVerifyTitle2(html);
2690+
}
2691+
2692+
/**
2693+
* @throws Exception if an error occurs
2694+
*/
2695+
@Test
2696+
@Alerts(DEFAULT = "false",
2697+
IE = "not available")
2698+
public void isSecureContextHttp() throws Exception {
2699+
final String html
2700+
= "<html><body><script>\n"
2701+
+ LOG_TITLE_FUNCTION
2702+
+ " log(window.hasOwnProperty('isSecureContext') ? isSecureContext : 'not available');\n"
2703+
+ "</script></body></html>";
2704+
2705+
final WebDriver driver = loadPage2(html, new URL(CookieManager4Test.URL_HOST1));
2706+
verifyTitle2(driver, getExpectedAlerts());
2707+
}
2708+
2709+
/**
2710+
* @throws Exception if an error occurs
2711+
*/
2712+
@Test
2713+
@Alerts(DEFAULT = "true",
2714+
IE = "null")
2715+
public void isSecureContextHttpS() throws Exception {
2716+
final WebDriver driver = loadPage2(new URL("https://www.wetator.org/HtmlUnit"), StandardCharsets.UTF_8);
2717+
2718+
final String script = "return window.isSecureContext";
2719+
final Object result = ((JavascriptExecutor) driver).executeScript(script);
2720+
assertEquals(getExpectedAlerts()[0], "" + result);
2721+
}
2722+
2723+
/**
2724+
* @throws Exception if an error occurs
2725+
*/
2726+
@Test
2727+
@Alerts(DEFAULT = "true",
2728+
IE = "null")
2729+
public void isSecureContextFile() throws Exception {
2730+
final String html
2731+
= "<html><body><script>\n"
2732+
+ LOG_TITLE_FUNCTION
2733+
+ " log(window.hasOwnProperty('isSecureContext') ? isSecureContext : 'not available');\n"
2734+
+ "</script></body></html>";
2735+
2736+
final File currentDirectory = new File((new File("")).getAbsolutePath());
2737+
final File tmpFile = File.createTempFile("isSecureContext", ".html", currentDirectory);
2738+
tmpFile.deleteOnExit();
2739+
final String encoding = (new OutputStreamWriter(new ByteArrayOutputStream())).getEncoding();
2740+
FileUtils.writeStringToFile(tmpFile, html, encoding);
2741+
2742+
final WebDriver driver = getWebDriver();
2743+
driver.get("file://" + tmpFile.getCanonicalPath());
2744+
2745+
final String script = "return window.isSecureContext";
2746+
final Object result = ((JavascriptExecutor) driver).executeScript(script);
2747+
assertEquals(getExpectedAlerts()[0], "" + result);
2748+
2749+
shutDownAll();
2750+
}
2751+
2752+
/**
2753+
* @throws Exception if an error occurs
2754+
*/
2755+
@Test
2756+
@Alerts(DEFAULT = "false",
2757+
IE = "null")
2758+
public void isSecureContextAboutBlank() throws Exception {
2759+
final WebDriver driver = getWebDriver();
2760+
driver.get("about:blank");
2761+
2762+
final String script = "return window.isSecureContext";
2763+
final Object result = ((JavascriptExecutor) driver).executeScript(script);
2764+
assertEquals(getExpectedAlerts()[0], "" + result);
2765+
2766+
shutDownAll();
2767+
}
26662768
}

0 commit comments

Comments
 (0)