Skip to content

fix/ escape SQL values#627

Merged
stonebuzz merged 15 commits into
mainfrom
fix-datainjection-escape-sql-value
May 29, 2026
Merged

fix/ escape SQL values#627
stonebuzz merged 15 commits into
mainfrom
fix-datainjection-escape-sql-value

Conversation

@Herafia
Copy link
Copy Markdown
Contributor

@Herafia Herafia commented May 20, 2026

Checklist before requesting a review

Please delete options that are not relevant.

  • I have performed a self-review of my code.
  • I have added tests (when available) that prove my fix is effective or that my feature works.
  • I have updated the CHANGELOG with a short functional description of the fix or new feature.
  • This change requires a documentation update.

Description

  • It fixes !44078
  • SQL queries in dataAlreadyInDB concatenated field values directly without escaping, causing a syntax error when names contain apostrophes . All affected concatenations now use escape().

@Herafia Herafia requested review from Rom1-B, Copilot and stonebuzz and removed request for Copilot May 20, 2026 07:32
@Herafia Herafia self-assigned this May 20, 2026
Comment thread inc/commoninjectionlib.class.php Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be cleaner to refactor the code to replace doQuery() with the query builder.

$result = $DB->request($criteria);

Comment thread inc/model.class.php
),
];
}
unset($_FILES['filename']);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ?
I'm not sure if this is directly related to the topic of this PR?!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After move_uploaded_file(), the temporary file is deleted, but $_FILES['filename'] still contains its path.

Later, when rendering the page, Html::footer() calls createFromGlobals(). An UploadedFile instance is created for each entry, and it checks whether the temporary file still exists. Since the file has already been moved and no longer exists, an exception is thrown.

unset() clears the corresponding entry from $_FILES after the file has been moved.

Comment thread inc/commoninjectionlib.class.php Outdated
//Add additional parameters specific to this itemtype (or function checkPresent exists)
if (method_exists($injectionClass, 'checkPresent')) {
$where .= $injectionClass->checkPresent($this->values, $options);
$extra = trim((string) $injectionClass->checkPresent($this->values, $options));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The three implementations of checkPresent() (softwareversion, softwarelicense, networkport) still return raw SQL with unescaped name values.
QueryExpression continues to receive potentially dangerous values.
These methods should either be updated to return an array of criteria, or at the very least, $DB->escape() should be added to the string fields.

@Herafia Herafia requested a review from Rom1-B May 21, 2026 12:53
Comment on lines +1987 to +1990
$extra = $injectionClass->checkPresent($this->values, $options);
if (is_array($extra) && count($extra) > 0) {
$where = array_merge($where, $extra);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An exception should be thrown or a warning logged if $extra is not an array and is not empty

@Herafia Herafia requested a review from Rom1-B May 27, 2026 13:25
Copy link
Copy Markdown
Contributor

@Rom1-B Rom1-B left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, can you add tests?

@Herafia Herafia requested a review from Rom1-B May 28, 2026 12:48
Comment thread tests/unit/CommonInjectionLibDataAlreadyInDbTest.php Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests are not being run by the CI because the configuration is missing:

diff --git a/.gitignore b/.gitignore
index f033123..508e8ab 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,5 @@ vendor/
 .gh_token
 *.min.*
 
+
+var/phpunit
diff --git a/phpunit.xml b/phpunit.xml
new file mode 100644
index 0000000..b827cdf
--- /dev/null
+++ b/phpunit.xml
@@ -0,0 +1,18 @@
+<phpunit
+    bootstrap="tests/bootstrap.php"
+    colors="true"
+    testdox="true"
+    cacheDirectory="var/phpunit"
+>
+    <source>
+        <include>
+            <directory>src</directory>
+        </include>
+    </source>
+
+    <testsuites>
+        <testsuite name="Tests">
+            <directory suffix="Test.php">tests</directory>
+        </testsuite>
+    </testsuites>
+</phpunit>
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
new file mode 100644
index 0000000..7438cd6
--- /dev/null
+++ b/tests/bootstrap.php
@@ -0,0 +1,10 @@
+<?php
+
+$current_plugin_folder = basename(realpath(__DIR__ . '/../'));
+
+require __DIR__ . '/../../../tests/bootstrap.php';
+require dirname(__DIR__) . '/vendor/autoload.php';
+
+if (!Plugin::isPluginActive($current_plugin_folder)) {
+    throw new RuntimeException("Plugin $current_plugin_folder is not active in the test database");
+}

Herafia and others added 3 commits May 28, 2026 16:44
Co-authored-by: Romain B. <8530352+Rom1-B@users.noreply.github.com>
@Herafia Herafia requested a review from Rom1-B May 29, 2026 07:19
Comment thread .gitignore Outdated
@stonebuzz stonebuzz merged commit 9c84e1a into main May 29, 2026
3 checks passed
@stonebuzz stonebuzz deleted the fix-datainjection-escape-sql-value branch May 29, 2026 08:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants