Skip to content

Commit c85abfa

Browse files
authored
Added support for mysqli (#241)
1 parent 3a2b11c commit c85abfa

File tree

61 files changed

+1197
-46
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1197
-46
lines changed

.devcontainer/centos/devcontainer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Centos Dev Container",
3-
"runArgs": [],
3+
"runArgs": ["--privileged"],
44
"mounts": [
55
"source=${localWorkspaceFolder}/.devcontainer/shared,target=/shared,type=bind"
66
],
@@ -20,7 +20,8 @@
2020
"ms-vscode.cpptools",
2121
"ms-vscode.cpptools-themes",
2222
"austin.code-gnu-global",
23-
"ms-vscode.makefile-tools"
23+
"ms-vscode.makefile-tools",
24+
"ms-python.vscode-pylance"
2425
]
2526
}
2627
}

.devcontainer/ubuntu/Dockerfile

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
# Docker container used for building Zen for PHP from source on Ubuntu
2-
3-
FROM --platform=linux/amd64 ubuntu:20.04
4-
5-
ARG PHP_VERSION=8.1
6-
7-
8-
RUN apt-get update
9-
RUN apt install software-properties-common -y
10-
RUN add-apt-repository ppa:ondrej/php -y
11-
RUN apt update
12-
RUN apt install php${PHP_VERSION} php${PHP_VERSION}-cli php${PHP_VERSION}-cgi php${PHP_VERSION}-fpm php${PHP_VERSION}-dev php${PHP_VERSION}-curl php${PHP_VERSION}-sqlite3 -y
13-
RUN apt-get install -y wget autoconf bison re2c libxml2-dev libssl-dev libcurl4-gnutls-dev protobuf-compiler protobuf-compiler-grpc git alien
14-
RUN wget https://go.dev/dl/go1.23.4.linux-amd64.tar.gz
15-
RUN tar -C /usr/local -xzf go1.23.4.linux-amd64.tar.gz
16-
ENV PATH="/usr/local/go/bin:${PATH}"
17-
ENV GOPATH="${HOME}/go"
18-
ENV PATH="${PATH}:${GOPATH}/bin"
19-
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
20-
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
1+
# Docker container used for building Zen for PHP from source on Ubuntu
2+
3+
FROM --platform=linux/amd64 ubuntu:22.04
4+
5+
ARG PHP_VERSION=8.1
6+
7+
RUN ln -fs /usr/share/zoneinfo/Etc/UTC /etc/localtime
8+
RUN echo "Etc/UTC" > /etc/timezone
9+
RUN apt-get update
10+
RUN apt install software-properties-common -y
11+
RUN add-apt-repository ppa:ondrej/php -y
12+
RUN apt update
13+
RUN apt install php${PHP_VERSION} php${PHP_VERSION}-cli php${PHP_VERSION}-cgi php${PHP_VERSION}-fpm php${PHP_VERSION}-dev php${PHP_VERSION}-curl php${PHP_VERSION}-sqlite3 -y
14+
RUN apt-get install -y wget autoconf bison re2c libxml2-dev libssl-dev libcurl4-gnutls-dev protobuf-compiler protobuf-compiler-grpc git alien
15+
RUN wget https://go.dev/dl/go1.23.4.linux-amd64.tar.gz
16+
RUN tar -C /usr/local -xzf go1.23.4.linux-amd64.tar.gz
17+
ENV PATH="/usr/local/go/bin:${PATH}"
18+
ENV GOPATH="${HOME}/go"
19+
ENV PATH="${PATH}:${GOPATH}/bin"
20+
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
21+
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

.devcontainer/ubuntu/devcontainer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"platform": "linux/amd64",
99
"dockerfile": "Dockerfile",
1010
"args": {
11-
"PHP_VERSION": "8.2"
11+
"PHP_VERSION": "8.1"
1212
}
1313
}
14-
}
14+
}

.github/workflows/build.yml

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ jobs:
126126
uses: shivammathur/setup-php@27853eb8b46dc01c33bf9fef67d98df2683c3be2 # v2
127127
with:
128128
php-version: ${{ matrix.php_version }}
129-
extensions: curl
129+
extensions: curl, mysqli
130130
coverage: none
131131

132132
- name: Check PHP setup
@@ -326,6 +326,7 @@ jobs:
326326
runs-on: ${{ matrix.os }}
327327
container:
328328
image: quay.io/centos/centos:stream9
329+
options: --privileged
329330
needs: [ build_rpm ]
330331
strategy:
331332
matrix:
@@ -352,11 +353,34 @@ jobs:
352353
dnf --assumeyes module reset php
353354
dnf --assumeyes --nogpgcheck module install php:remi-${{ matrix.php_version }}
354355
dnf --assumeyes install php-pdo
356+
dnf --assumeyes install php-mysqlnd
355357
yum install -y mod_php
356358
yum install -y nginx
357359
yum install -y php-fpm
358360
dnf install -y procps-ng
359361
362+
- name: Install and start MySQL
363+
run: |
364+
yum install -y mysql-server
365+
mkdir -p /var/lib/mysql
366+
mysqld --initialize-insecure --datadir=/var/lib/mysql
367+
mysqld -u root --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock &
368+
sleep 10
369+
mysql -u root -e "CREATE DATABASE IF NOT EXISTS db;"
370+
371+
- name: Test MySQL connection with mysqli
372+
run: |
373+
php -r '
374+
$mysqli = new mysqli("localhost", "root", "", "db");
375+
if ($mysqli->connect_error) {
376+
echo "MySQL connection failed: " . $mysqli->connect_error . "\n";
377+
exit(1);
378+
} else {
379+
echo "MySQL connection successful\n";
380+
$mysqli->close();
381+
}
382+
'
383+
360384
- name: Get Arch
361385
run: echo "ARCH=$(uname -m)" >> $GITHUB_ENV
362386

@@ -397,6 +421,7 @@ jobs:
397421
runs-on: ${{ matrix.os }}
398422
container:
399423
image: ${{ matrix.container }}
424+
options: --privileged
400425
needs: [ build_deb ]
401426
strategy:
402427
matrix:
@@ -442,13 +467,35 @@ jobs:
442467
a2enmod mpm_prefork
443468
a2enmod rewrite
444469
470+
- name: Install MariaDB server
471+
run: |
472+
apt-get install -y mariadb-server
473+
mkdir -p /var/lib/mysql
474+
mkdir -p /run/mysqld
475+
mysqld --user=root --datadir=/var/lib/mysql &
476+
sleep 10
477+
mysql -u root -e "CREATE DATABASE IF NOT EXISTS db;"
478+
445479
- name: Setup PHP
446480
uses: shivammathur/setup-php@27853eb8b46dc01c33bf9fef67d98df2683c3be2
447481
with:
448482
php-version: ${{ matrix.php_version }}
449-
extensions: curl, sqlite3
483+
extensions: curl, sqlite3, mysqli
450484
coverage: none
451485

486+
- name: Test MySQL connection with mysqli
487+
run: |
488+
php -r '
489+
$mysqli = new mysqli("localhost", "root", "", "db");
490+
if ($mysqli->connect_error) {
491+
echo "MySQL connection failed: " . $mysqli->connect_error . "\n";
492+
exit(1);
493+
} else {
494+
echo "MySQL connection successful\n";
495+
$mysqli->close();
496+
}
497+
'
498+
452499
- name: Check PHP setup
453500
run: |
454501
php_versions="php7.3 php7.4 php8.0 php8.1 php8.2 php8.3"
@@ -494,4 +541,5 @@ jobs:
494541
name: test-results-aikido-${{ env.AIKIDO_VERSION }}-${{ matrix.os }}-php-${{ matrix.php_version }}
495542
if-no-files-found: ignore
496543
path: |
497-
${{ github.workspace }}/tests/cli/**/*.diff
544+
${{ github.workspace }}/tests/cli/**/*.diff
545+
/var/log/mysql/error.log

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,25 @@ Prerequisites:
3838

3939
##### x86_64
4040
```
41-
rpm -Uvh --oldpackage https://github.com/AikidoSec/firewall-php/releases/download/v1.1.0/aikido-php-firewall.x86_64.rpm
41+
rpm -Uvh --oldpackage https://github.com/AikidoSec/firewall-php/releases/download/v1.2.0/aikido-php-firewall.x86_64.rpm
4242
```
4343

4444
##### arm64 / aarch64
4545
```
46-
rpm -Uvh --oldpackage https://github.com/AikidoSec/firewall-php/releases/download/v1.1.0/aikido-php-firewall.aarch64.rpm
46+
rpm -Uvh --oldpackage https://github.com/AikidoSec/firewall-php/releases/download/v1.2.0/aikido-php-firewall.aarch64.rpm
4747
```
4848

4949
#### For Debian-based Systems (Debian, Ubuntu)
5050

5151
##### x86_64
5252
```
53-
curl -L -O https://github.com/AikidoSec/firewall-php/releases/download/v1.1.0/aikido-php-firewall.x86_64.deb
53+
curl -L -O https://github.com/AikidoSec/firewall-php/releases/download/v1.2.0/aikido-php-firewall.x86_64.deb
5454
dpkg -i -E ./aikido-php-firewall.x86_64.deb
5555
```
5656

5757
##### arm64 / aarch64
5858
```
59-
curl -L -O https://github.com/AikidoSec/firewall-php/releases/download/v1.1.0/aikido-php-firewall.aarch64.deb
59+
curl -L -O https://github.com/AikidoSec/firewall-php/releases/download/v1.2.0/aikido-php-firewall.aarch64.deb
6060
dpkg -i -E ./aikido-php-firewall.aarch64.deb
6161
```
6262

docs/aws-elastic-beanstalk.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
```
55
commands:
66
aikido-php-firewall:
7-
command: "rpm -Uvh --oldpackage https://github.com/AikidoSec/firewall-php/releases/download/v1.1.0/aikido-php-firewall.x86_64.rpm"
7+
command: "rpm -Uvh --oldpackage https://github.com/AikidoSec/firewall-php/releases/download/v1.2.0/aikido-php-firewall.x86_64.rpm"
88
ignoreErrors: true
99
1010
files:

docs/fly-io.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Create a script to install the Aikido PHP Firewall during deployment:
3232
#!/usr/bin/env bash
3333
cd /tmp
3434

35-
curl -L -O https://github.com/AikidoSec/firewall-php/releases/download/v1.1.0/aikido-php-firewall.x86_64.deb
35+
curl -L -O https://github.com/AikidoSec/firewall-php/releases/download/v1.2.0/aikido-php-firewall.x86_64.deb
3636
dpkg -i -E ./aikido-php-firewall.x86_64.deb
3737
```
3838

docs/laravel-forge.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ cd /tmp
2121
2222
# Install commands from the "Manual install" section below, based on your OS
2323
24-
curl -L -O https://github.com/AikidoSec/firewall-php/releases/download/v1.1.0/aikido-php-firewall.x86_64.deb
24+
curl -L -O https://github.com/AikidoSec/firewall-php/releases/download/v1.2.0/aikido-php-firewall.x86_64.deb
2525
dpkg -i -E ./aikido-php-firewall.x86_64.deb
2626
2727
# Restarting the php services in order to load the Aikido PHP Firewall

lib/agent/globals/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package globals
22

33
const (
4-
Version = "1.1.0"
4+
Version = "1.2.0"
55
ConfigUpdatedAtMethod = "GET"
66
ConfigUpdatedAtAPI = "/config"
77
ConfigAPIMethod = "GET"
Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ AIKIDO_HANDLER_FUNCTION(handle_pre_pdo_query) {
66
zend_string *query = NULL;
77

88
ZEND_PARSE_PARAMETERS_START(0, -1)
9-
Z_PARAM_OPTIONAL
10-
Z_PARAM_STR(query)
9+
Z_PARAM_OPTIONAL
10+
Z_PARAM_STR(query)
1111
ZEND_PARSE_PARAMETERS_END();
1212

1313
if (!query) {
@@ -34,7 +34,7 @@ AIKIDO_HANDLER_FUNCTION(handle_pre_pdo_exec) {
3434
zend_string *query = NULL;
3535

3636
ZEND_PARSE_PARAMETERS_START(1, 1)
37-
Z_PARAM_STR(query)
37+
Z_PARAM_STR(query)
3838
ZEND_PARSE_PARAMETERS_END();
3939

4040
if (!query) {
@@ -71,4 +71,45 @@ AIKIDO_HANDLER_FUNCTION(handle_pre_pdostatement_execute) {
7171

7272
zval *pdo_object = &stmt->database_object_handle;
7373
eventCache.sqlDialect = GetSqlDialectFromPdo(pdo_object);
74-
}
74+
}
75+
76+
zend_class_entry* helper_load_mysqli_link_class_entry() {
77+
/* Static variable initialization ensures that the class entry is loaded only once and is thread-safe */
78+
static zend_class_entry* mysqliLinkClassEntry = (zend_class_entry*)zend_hash_str_find_ptr(EG(class_table), "mysqli", sizeof("mysqli") - 1);
79+
return mysqliLinkClassEntry;
80+
}
81+
82+
AIKIDO_HANDLER_FUNCTION(handle_pre_mysqli_query){
83+
zval* mysqliLinkObject = nullptr;
84+
char* query = nullptr;
85+
size_t queryLength;
86+
zend_long resultMode;
87+
88+
zend_class_entry* mysqliLinkClassEntry = helper_load_mysqli_link_class_entry();
89+
if (!mysqliLinkClassEntry) {
90+
AIKIDO_LOG_WARN("handle_pre_mysqli_query: did not find mysqli link class!\n");
91+
return;
92+
}
93+
94+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|l", &mysqliLinkObject, mysqliLinkClassEntry, &query, &queryLength, &resultMode) == FAILURE) {
95+
AIKIDO_LOG_WARN("handle_pre_mysqli_query: failed to parse parameters!\n");
96+
return;
97+
}
98+
99+
if (!queryLength) {
100+
AIKIDO_LOG_WARN("handle_pre_mysqli_query: query length is 0!\n");
101+
return;
102+
}
103+
104+
if (!mysqliLinkObject) {
105+
AIKIDO_LOG_WARN("handle_pre_mysqli_query: mysqli link object is null!\n");
106+
return;
107+
}
108+
109+
scopedTimer.SetSink(sink, "sql_op");
110+
111+
eventId = EVENT_PRE_SQL_QUERY_EXECUTED;
112+
eventCache.moduleName = "mysqli";
113+
eventCache.sqlQuery = query;
114+
eventCache.sqlDialect = "mysql";
115+
}

0 commit comments

Comments
 (0)