Skip to content

Commit b5b54bf

Browse files
committed
atob() must always fail on invalid input (issue #940)
1 parent 1ed9cb8 commit b5b54bf

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
<body>
1010
<release version="4.11.0" date="March xx, 2025" description="Bugfixes">
11+
<action type="fix" dev="rbri" due-to="Christoph Burgmer" issue="#940">
12+
atob() must always fail on invalid input.
13+
</action>
1114
<action type="fix" dev="rbri">
1215
The script async attribute is ignored if the src attribute is absent.
1316
</action>

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ public static String atob(final String encodedData, final HtmlUnitScriptable scr
6767
}
6868
}
6969
final byte[] bytes = encodedData.getBytes(StandardCharsets.ISO_8859_1);
70-
return new String(Base64.getDecoder().decode(bytes), StandardCharsets.ISO_8859_1);
70+
try {
71+
return new String(Base64.getDecoder().decode(bytes), StandardCharsets.ISO_8859_1);
72+
}
73+
catch (final IllegalArgumentException e) {
74+
throw JavaScriptEngine.asJavaScriptException(
75+
scriptable,
76+
"Failed to execute atob(): " + e.getMessage(),
77+
org.htmlunit.javascript.host.dom.DOMException.INVALID_CHARACTER_ERR);
78+
}
7179
}
7280

7381
/**

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Frank Danek
5050
* @author Carsten Steul
5151
* @author Colin Alworth
52+
* @author Christoph Burgmer
5253
*/
5354
@RunWith(BrowserRunner.class)
5455
public class Window2Test extends WebDriverTestCase {
@@ -66,7 +67,7 @@ public void thisIsWindow() throws Exception {
6667
+ " log(this);\n"
6768
+ " try {\n"
6869
+ " log(abc);\n"
69-
+ " } catch(e) {logEx(e)}\n"
70+
+ " } catch(e) { logEx(e) }\n"
7071
+ " log(this.abc);\n"
7172
+ " log(this.def);\n"
7273
+ " this.abc = 'hello';\n"
@@ -185,15 +186,15 @@ public void atob() throws Exception {
185186
* @throws Exception if the test fails
186187
*/
187188
@Test
188-
@Alerts({"InvalidCharacterError/DOMException"})
189+
@Alerts("InvalidCharacterError/DOMException")
189190
public void atobMalformedInput() throws Exception {
190191
final String html
191192
= "<html><head></head><body>\n"
192193
+ "<script>\n"
193194
+ LOG_TITLE_FUNCTION
194195
+ " try {\n"
195196
+ " window.atob('b');\n"
196-
+ " } catch(e) {logEx(e)}\n"
197+
+ " } catch(e) { logEx(e) }\n"
197198
+ "</script>\n"
198199
+ "</body></html>";
199200
loadPageVerifyTitle2(html);
@@ -211,10 +212,10 @@ public void atobUnicode() throws Exception {
211212
+ LOG_TITLE_FUNCTION
212213
+ " try {\n"
213214
+ " window.btoa('I \\u2661 Unicode!');\n"
214-
+ " } catch(e) {logEx(e)}\n"
215+
+ " } catch(e) { logEx(e) }\n"
215216
+ " try {\n"
216217
+ " window.atob('I \\u2661 Unicode!');\n"
217-
+ " } catch(e) {logEx(e)}\n"
218+
+ " } catch(e) { logEx(e) }\n"
218219
+ "</script>\n"
219220
+ "</body></html>";
220221
loadPageVerifyTitle2(html);
@@ -1150,13 +1151,13 @@ public void eval() throws Exception {
11501151
+ " x.a = 'Success';\n"
11511152
+ " try {\n"
11521153
+ " log(window['eval']('x.a'));\n"
1153-
+ " } catch(e) {logEx(e)}\n"
1154+
+ " } catch(e) { logEx(e) }\n"
11541155
+ " try {\n"
11551156
+ " log(window.eval('x.a'));\n"
1156-
+ " } catch(e) {logEx(e)}\n"
1157+
+ " } catch(e) { logEx(e) }\n"
11571158
+ " try {\n"
11581159
+ " log(eval('x.a'));\n"
1159-
+ " } catch(e) {logEx(e)}\n"
1160+
+ " } catch(e) { logEx(e) }\n"
11601161
+ "}\n"
11611162
+ "</script>\n"
11621163
+ "</body></html>";
@@ -2369,7 +2370,7 @@ public void constructorError() throws Exception {
23692370
+ " var divs = document.querySelectorAll('div');\n"
23702371
+ " var a = Array.from.call(window, divs);\n"
23712372
+ " log(a.length);\n"
2372-
+ " } catch(e) {logEx(e)}\n"
2373+
+ " } catch(e) { logEx(e) }\n"
23732374
+ " }\n"
23742375
+ "</script>\n"
23752376
+ "</head>\n"
@@ -2433,7 +2434,7 @@ public void test__proto__() throws Exception {
24332434
+ " for (var p = this.__proto__; p != null; p = p.__proto__) {\n"
24342435
+ " log(p);\n"
24352436
+ " }\n"
2436-
+ " } catch(e) {logEx(e)}\n"
2437+
+ " } catch(e) { logEx(e) }\n"
24372438
+ " }\n"
24382439
+ "</script>\n"
24392440
+ "</head>\n"

0 commit comments

Comments
 (0)