vestedsolutionsgroup / libinjection-php
Pure-PHP translation of libinjection: SQL injection and XSS detection.
Package info
github.com/VestedSolutionsGroup/libinjection-php
pkg:composer/vestedsolutionsgroup/libinjection-php
Requires
- php: >=8.1
Requires (Dev)
- phpunit/phpunit: ^9.6
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-03 19:48:50 UTC
README
A pure-PHP translation of libinjection,
the C library that detects SQL injection (SQLi) and cross-site scripting (XSS) in
input strings. It is the library behind ModSecurity's @detectSQLi and @detectXSS
operators, which the OWASP Core Rule Set uses in rules 942100, 942101, 941100 and 941101.
The official libinjection repository only offers PHP through a binding to the compiled C library, which most shared hosts cannot load. This package needs nothing but PHP itself.
This is an independent translation. It is not affiliated with, or endorsed by, the libinjection project or its contributors. All credit for the detection logic and data goes to them.
Status
- Translates libinjection 4.0.3, upstream commit
40946c5c6a0800a6d6c77d829635e66a6353a934(2026-11-23). - Gives the same answers as the C library on every input tested (see Testing).
- Keeps libinjection 4's behaviour of returning an error result instead of aborting.
Requirements
- PHP 8.1 or later.
- No extensions beyond a default PHP build (no
mbstring, noctype), no runtime dependencies.
Input is treated as raw bytes, exactly as the C library does. Strings may contain any byte, including NUL; no character encoding is assumed.
Installation
composer require vestedsolutionsgroup/libinjection-php
Without Composer, copy the src/ folder and register a PSR-4 autoloader for the
VestedSolutionsGroup\LibInjection\ namespace. The code has no other dependencies,
so the namespace can be prefixed when bundling it into another project.
Usage
use VestedSolutionsGroup\LibInjection\LibInjection; use VestedSolutionsGroup\LibInjection\Result; $result = LibInjection::sqli("1' OR '1'='1", $fingerprint); // $result === Result::Injection, $fingerprint === 's&sos' $result = LibInjection::xss('<img src=x onerror=alert(1)>'); // $result === Result::Injection if (LibInjection::sqli($value)->isSuspicious()) { // block: injection found, or the parser reported an error }
API
| Method | C equivalent | Returns |
|---|---|---|
LibInjection::sqli(string $input, ?string &$fingerprint = null): Result |
libinjection_sqli() |
$fingerprint is set to the SQLi fingerprint (up to five token-type characters) when injection is found, otherwise to ''. |
LibInjection::xss(string $input): Result |
libinjection_xss() |
|
LibInjection::version(): string |
libinjection_version() |
The upstream version translated (4.0.3). |
Result is an enum whose integer values match C's injection_result_t:
| Case | ->value |
C constant | Meaning |
|---|---|---|---|
Result::Injection |
1 |
LIBINJECTION_RESULT_TRUE |
Injection detected. |
Result::Benign |
0 |
LIBINJECTION_RESULT_FALSE |
Nothing detected. |
Result::Error |
-1 |
LIBINJECTION_RESULT_ERROR |
The parser reached an invalid state. |
Result::isSuspicious() is true for both Injection and Error. Upstream's
migration guide
recommends treating errors as detections (fail safe). As in C, the SQLi check never
returns Error, and the XSS check returns it only from an internal parser state
that no input reached in any of the tests (the check is kept, as in C, as a safeguard).
The classes under Sqli\ and Xss\ (SqliState, Html5State, XssDetector)
expose the tokenizers and per-context checks, mirroring upstream's lower-level C
functions. They are marked @internal: they are used by the tests and may change.
Differences from the C library
None in results. Points of detail:
- Where the C code reads the byte just past the end of the input (one check on short "number + comment" fingerprints does), this translation reads a NUL byte. That is what C reads for NUL-terminated input, which is how ModSecurity and upstream's own tools call it. The differential test feeds the C library NUL-terminated input.
- Not translated: the hook for replacing the word-lookup function
(
libinjection_sqli_callback), which exists for debugging and SWIG bindings.
Regenerating the fingerprint table
src/Sqli/SqliData.php (the 9,359 SQL keywords, operators and SQLi fingerprints,
plus the byte-to-parser map) is generated from upstream's
src/libinjection_sqli_data.h, which upstream itself generates from
src/fingerprints.txt. To update it:
git clone https://github.com/libinjection/libinjection /path/to/libinjection git -C /path/to/libinjection checkout <commit> php tools/generate-sqli-data.php /path/to/libinjection/src/libinjection_sqli_data.h <full-commit-hash>
The script records the commit in the generated file (SqliData::UPSTREAM_COMMIT,
also exposed as LibInjection::UPSTREAM_COMMIT), and refuses to write the file if
the table is not sorted or contains lower-case letters, the two conditions under
which its hash-map lookup equals C's binary search.
When moving to a newer upstream commit, also compare the C sources for logic changes,
copy upstream's tests/ and data/ into tests/fixtures/upstream/, update the exact
counts in tests/UpstreamCorpusTest.php, and run the differential test.
Testing
composer install vendor/bin/phpunit
The test suite runs:
- Upstream's golden test files: all 588 files from upstream's
tests/folder (tokenizer, folding, SQLi and HTML5 tokenizer output), compared text for text, as upstream'stestdriver.cdoes. - Upstream's corpora from
data/, read and decoded as upstream'sreader.cdoes: 17 of the SQLi samples are missed (upstream allows at most 18), 21 benign samples are flagged (at most 21) and 20 XSS samples are missed (at most 20), exactly the C library's counts. - Byte-level edge cases where a translation most easily drifts (NUL and high-bit bytes, 31-byte token truncation, HTML entities), with expected values taken from the C library.
Results at the tracked commit: 625 tests, all passing on PHP 8.3.
Differential test against the C library
tools/differential/run.php compiles upstream's C sources with a small oracle program
(tools/differential/oracle.c) outside this repository, then compares, for every
input, 12 outputs: the libinjection_sqli() result and fingerprint, the fingerprint
and verdict in each of the five SQL quoting contexts, libinjection_is_xss() in each
of the five HTML contexts, and the libinjection_xss() result.
php tools/differential/run.php /path/to/libinjection [--random=300000] [--seed=N]
Inputs: upstream's corpora (167,657 lines), the 588 test-file inputs, 277 hand-written edge cases, and random inputs in three equal parts: random SQL/HTML tokens, random bytes, and mutated corpus lines. Results at the tracked commit:
| Run | Inputs | Mismatches |
|---|---|---|
| seed 20261203 (default) | 468,522 | 0 |
| seed 7 | 468,522 | 0 |
Benchmark
php bench/benchmark.php
Measured on PHP 8.3 (CLI, OPcache on, no JIT) in a cloud container; expect different absolute numbers elsewhere:
- Short values (a word, an e-mail address, a URL, a typical attack string):
3-20 µs per
sqli()call, 1-8 µs perxss()call. - 64 KB values: well under 1 ms for most text, 5-30 ms for HTML-heavy text, up to
about 135 ms for the slowest shape found (64 KB of
<b><b><b>…, which keeps the SQL folder busy to the end of the input). - Time grows linearly with input size (checked up to 1 MB on the slowest shapes) and peak memory stayed at 6-8 MB: no input was found that causes runaway time or memory.
Licence
BSD 3-Clause, like the original. See LICENSE. The upstream test files and
corpora in tests/fixtures/upstream/ are distributed under upstream's licence, reproduced
in tests/fixtures/upstream/COPYING.
Original work: Copyright (c) 2012-2016, Nick Galbreath; Copyright (c) 2017-2024, libinjection Contributors. PHP translation: Copyright (c) 2026, Vested Solutions Group.