CVE-2023-3085: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') (CWE-79)
value: A vulnerability, which was classified as problematic, has been found in X-WRT luci up to 22.10_b202303061504. This issue affects the function run_action of the file modules/luci-base/ucode/dispatcher.uc of the component 404 Error Template Handler. The manipulation of the argument request_path leads to cross site scripting. The attack may be initiated remotely. Upgrading to version 22.10_b202303121313 is able to address this issue. The patch is named 24d7da2416b9ab246825c33c213fe939a89b369c. It is recommended to upgrade the affected component. The associated identifier of this vulnerability is VDB-230663.
CWE description: The product does not neutralize or incorrectly neutralizes user-controllable input before it is placed in output that is used as a web page that is served to other users.
CVSS v3 base score: 6.1
Published: 2023-06-03T11:15Z
Repository: https://github.com/x-wrt/luci
Language: Other
Files changed: modules/luci-base/ucode/dispatcher.uc, modules/luci-base/ucode/template/header.ut
Commit date: 2023-03-10 15:36:09 +0100
Commit message: luci-base: dispatcher.uc: prevent XSS through 404 error template

Make sure to escape the user controlled URL passed as part of the error
message into the error404 template in order to avoid XSS.

Reported-by: 40826d <40826d@posteo.de>
Signed-off-by: Jo-Philipp Wich <jo@mein.io>

Diff: diff --git a/modules/luci-base/ucode/dispatcher.uc b/modules/luci-base/ucode/dispatcher.uc
index 24769f0037..39af8375ea 100644
--- a/modules/luci-base/ucode/dispatcher.uc
+++ b/modules/luci-base/ucode/dispatcher.uc
@@ -843,11 +843,11 @@ function run_action(request_path, lang, tree, resolved, action) {
 		if (!length(tree.children))
 			error404("No root node was registered, this usually happens if no module was installed.\n" +
 			         "Install luci-mod-admin-full and retry. " +
 			         "If the module is already installed, try removing the /tmp/luci-indexcache file.");
 		else
-			error404(`No page is registered at '/${join("/", resolved.ctx.request_path)}'.\n` +
+			error404(`No page is registered at '/${entityencode(join("/", resolved.ctx.request_path))}'.\n` +
 			         "If this url belongs to an extension, make sure it is properly installed.\n" +
 			         "If the extension was recently installed, try removing the /tmp/luci-indexcache file.");
 		break;
 
 	default:
diff --git a/modules/luci-base/ucode/template/header.ut b/modules/luci-base/ucode/template/header.ut
index e87560010f..7dc3742a9d 100644
--- a/modules/luci-base/ucode/template/header.ut
+++ b/modules/luci-base/ucode/template/header.ut
@@ -8,11 +8,11 @@
 -%}
 
 <script type="text/javascript" src="{{ resource }}/promis.min.js"></script>
 <script type="text/javascript" src="{{ resource }}/luci.js"></script>
 <script type="text/javascript">
-	L = new LuCI({{ {
+	L = new LuCI({{ replace(`${ {
 		media          : media,
 		resource       : resource,
 		scriptname     : http.getenv("SCRIPT_NAME"),
 		pathinfo       : http.getenv("PATH_INFO"),
 		documentroot   : http.getenv("DOCUMENT_ROOT"),
@@ -26,7 +26,7 @@
 		apply_rollback : max(+config.apply.rollback ||  90, 90),
 		apply_holdoff  : max(+config.apply.holdoff  ||   4,  1),
 		apply_timeout  : max(+config.apply.timeout  ||   5,  1),
 		apply_display  : max(+config.apply.display  || 1.5,  1),
 		rollback_token : rollback_token
-	} }});
+	} }`, '/', '\\/') }});
 </script>

Vulnerable code: 			error404(`No page is registered at '/${join("/", resolved.ctx.request_path)}'.\n` +
	L = new LuCI({{ {
	} }});
Fixed code: 			error404(`No page is registered at '/${entityencode(join("/", resolved.ctx.request_path))}'.\n` +
	L = new LuCI({{ replace(`${ {
	} }`, '/', '\\/') }});