CVE-2022-3845: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') (CWE-79)
value: A vulnerability has been found in phpipam and classified as problematic. Affected by this vulnerability is an unknown functionality of the file app/admin/import-export/import-load-data.php of the component Import Preview Handler. The manipulation leads to cross site scripting. The attack can be launched remotely. Upgrading to version 1.5.0 is able to address this issue. The name of the patch is 22c797c3583001211fe7d31bccd3f1d4aeeb3bbc. It is recommended to upgrade the affected component. The associated identifier of this vulnerability is VDB-212863.
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: 2022-11-02T20:15Z
Repository: https://github.com/phpipam/phpipam
Language: PHP
Files changed: app/admin/import-export/import-load-data.php, misc/CHANGELOG
Commit date: 2022-04-24 12:37:17 +0000
Commit message: Bugfix: XSS (reflected) in import previews

Reported by Faisal Fs <faisalfs10x@gmail.com>

Diff: diff --git a/app/admin/import-export/import-load-data.php b/app/admin/import-export/import-load-data.php
index ab2303eed..202544296 100644
--- a/app/admin/import-export/import-load-data.php
+++ b/app/admin/import-export/import-load-data.php
@@ -63,11 +63,11 @@ if (strtolower($filetype) == "csv") {
 	$line = str_replace( array("\r\n","\r","\n") , "" , $line);	//remove line break
 	$cols = str_getcsv ($line, $Tools->csv_delimiter);
 	foreach ($cols as $val) {
 		$col++;
 		# map import columns to expected fields as per previous window
-		$fieldmap[$col] = $impfields[$val];
+		$fieldmap[$col] = escape_input(trim($impfields[$val]));
 		$hcol = $col;
 	}
 
 	# read each remaining row into a dictionary with expected fields as keys
 	while (($line = fgets($filehdl)) !== false) {
@@ -79,11 +79,11 @@ if (strtolower($filetype) == "csv") {
 			$col++;
 			if ($col > $hcol) {
 				$Result->show('danger', _("Extra column found on line ").$row._(" in CSV file. CSV delimiter used in value field?"), true);
 			} else {
 				# read each row into a dictionary with expected fields as keys
-				$record[$fieldmap[$col]] = trim($val);
+				$record[$fieldmap[$col]] = escape_input(trim($val));
 			}
 		}
 		$data[] = $record;
 	}
 	fclose($filehdl);
@@ -95,25 +95,22 @@ elseif(strtolower($filetype) == "xls") {
 	$xls = new Spreadsheet_Excel_Reader('upload/data_import.xls', false);
 	$sheet = 0; $row = 1;
 
 	# map import columns to expected fields as per previous window
 	for($col=1;$col<=$xls->colcount($sheet);$col++) {
-		$fieldmap[$col] = $impfields[$Tools->convert_encoding_to_UTF8($xls->val($row,$col,$sheet))];
+		$fieldmap[$col] = $impfields[escape_input($Tools->convert_encoding_to_UTF8($xls->val($row,$col,$sheet)))];
 		$hcol = $col;
 	}
 
 	# read each remaining row into a dictionary with expected fields as keys
 	for($row=2;$row<=$xls->rowcount($sheet);$row++) {
 		$record = array();
 		for($col=1;$col<=$xls->colcount($sheet);$col++) {
-			$record++;
 			if ($col > $hcol) {
 					$Result->show('danger', _("Extra column found on line ").$row._(" in XLS file. Please check input file."), true);
 			} else {
-				$record[$fieldmap[$col]] = trim($Tools->convert_encoding_to_UTF8($xls->val($row,$col,$sheet)));
+				$record[$fieldmap[$col]] = escape_input(trim($Tools->convert_encoding_to_UTF8($xls->val($row,$col,$sheet))));
 			}
 		}
 		$data[] = $record;
 	}
 }
-
-?>
diff --git a/misc/CHANGELOG b/misc/CHANGELOG
index 8018b01e1..4cfa87620 100755
--- a/misc/CHANGELOG
+++ b/misc/CHANGELOG
@@ -90,10 +90,11 @@
     + Stored XSS in the "Site title" parameter;
     + XSS while uploading CVS files;
     + XSS (reflected) in 'find subnets';
     + Incorrect privilege assignments (#3506);
     + XXS (reflected) in ripe-arin-query;
+    + XSS (reflected) in import previews;
 
     Translations:
     ----------------------------
     + Update Traditional Chinese support to version 1.5 (#2658);
     + Update Simplified Chinese Translation (#2725);

Vulnerable code: 		$fieldmap[$col] = $impfields[$val];
				$record[$fieldmap[$col]] = trim($val);
		$fieldmap[$col] = $impfields[$Tools->convert_encoding_to_UTF8($xls->val($row,$col,$sheet))];
			$record++;
				$record[$fieldmap[$col]] = trim($Tools->convert_encoding_to_UTF8($xls->val($row,$col,$sheet)));

?>
Fixed code: 		$fieldmap[$col] = escape_input(trim($impfields[$val]));
				$record[$fieldmap[$col]] = escape_input(trim($val));
		$fieldmap[$col] = $impfields[escape_input($Tools->convert_encoding_to_UTF8($xls->val($row,$col,$sheet)))];
				$record[$fieldmap[$col]] = escape_input(trim($Tools->convert_encoding_to_UTF8($xls->val($row,$col,$sheet))));
    + XSS (reflected) in import previews;