Skip to content

Commit ef64996

Browse files
authored
Merge pull request #321 from eduar-hte/windows-port
Add support to build ModSecurity-nginx on Windows
2 parents 0d8ee3b + 43b0531 commit ef64996

14 files changed

+611
-14
lines changed

.github/workflows/test.yml

+132-6
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ jobs:
2626
sudo dpkg --add-architecture i386
2727
sudo apt-get update -y -qq
2828
sudo apt-get install -y make autoconf automake make libyajl-dev libxml2-dev libmaxminddb-dev libcurl4-gnutls-dev $COMPDEPS
29-
- name: Install ModSecurity library
30-
env:
31-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29+
- name: Get libModSecurity source
30+
uses: actions/checkout@v4
31+
with:
32+
repository: owasp-modsecurity/ModSecurity
33+
path: ModSecurity
34+
submodules: true
35+
fetch-depth: 1
36+
- name: Build libModSecurity
37+
working-directory: ModSecurity
3238
run: |
33-
gh release download -p "*.tar.gz" -R owasp-modsecurity/ModSecurity -O - | tar -xzf -
34-
cd modsecurity-*
39+
./build.sh
3540
./configure --without-lmdb --prefix=/usr
3641
make -j $(nproc)
3742
sudo make install
@@ -45,13 +50,26 @@ jobs:
4550
repository: nginx/nginx
4651
path: nginx
4752
fetch-depth: 1
53+
- name: Get Nginx tests
54+
uses: actions/checkout@v4
55+
with:
56+
repository: nginx/nginx-tests
57+
path: nginx/test
58+
fetch-depth: 1
59+
- name: Copy ModSecurity-nginx tests to nginx/test
60+
run: |
61+
cp ModSecurity-nginx/tests/* nginx/test
4862
- name: Build nginx with ModSecurity-nginx module
4963
working-directory: nginx
5064
run: |
51-
./auto/configure --with-ld-opt="-Wl,-rpath,/usr/local/lib" --without-pcre2 --add-module=../ModSecurity-nginx
65+
./auto/configure --with-ld-opt="-Wl,-rpath,/usr/local/lib" --without-pcre2 --with-http_v2_module --with-http_auth_request_module --add-module=../ModSecurity-nginx
5266
make
5367
make modules
5468
sudo make install
69+
- name: Run ModSecurity-nginx tests
70+
working-directory: nginx/test
71+
run: |
72+
TEST_NGINX_BINARY=../objs/nginx prove modsecurity*.t
5573
- name: Start Nginx
5674
run: |
5775
sudo /usr/local/nginx/sbin/nginx -c /home/runner/work/ModSecurity-nginx/ModSecurity-nginx/ModSecurity-nginx/.github/nginx/nginx.conf
@@ -91,3 +109,111 @@ jobs:
91109
echo "FAIL"
92110
exit 1
93111
fi
112+
113+
build-windows:
114+
runs-on: windows-2022
115+
defaults:
116+
run:
117+
shell: msys2 {0}
118+
steps:
119+
- name: Set up MSVC
120+
uses: ilammy/msvc-dev-cmd@v1
121+
- name: Set up msys
122+
uses: msys2/setup-msys2@v2
123+
with:
124+
msystem: UCRT64
125+
path-type: inherit
126+
- name: Get Nginx source
127+
uses: actions/checkout@v4
128+
with:
129+
repository: nginx/nginx
130+
path: nginx
131+
fetch-depth: 1
132+
- name: Get Nginx tests
133+
uses: actions/checkout@v4
134+
with:
135+
repository: nginx/nginx-tests
136+
path: nginx/test
137+
fetch-depth: 1
138+
- name: Set up third-party libraries
139+
working-directory: nginx
140+
run: |
141+
mkdir objs
142+
mkdir objs/lib
143+
cd objs/lib
144+
wget -q -O - https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.39/pcre2-10.39.tar.gz | tar -xzf -
145+
wget -q -O - https://www.zlib.net/fossils/zlib-1.3.tar.gz | tar -xzf -
146+
wget -q -O - https://www.openssl.org/source/openssl-3.0.13.tar.gz | tar -xzf -
147+
- name: Get libModSecurity source
148+
uses: actions/checkout@v4
149+
with:
150+
repository: owasp-modsecurity/ModSecurity
151+
submodules: true
152+
path: nginx/objs/lib/ModSecurity
153+
fetch-depth: 1
154+
- name: Setup Conan
155+
shell: cmd
156+
run: |
157+
pip3 install conan --upgrade
158+
conan profile detect
159+
- name: Build libModSecurity
160+
working-directory: nginx/objs/lib/ModSecurity
161+
shell: cmd
162+
run: |
163+
vcbuild.bat
164+
- name: Get ModSecurity-nginx source code
165+
uses: actions/checkout@v4
166+
with:
167+
path: nginx/objs/lib/ModSecurity-nginx
168+
- name: Copy ModSecurity-nginx tests to nginx/test
169+
working-directory: nginx/test
170+
run: |
171+
cp ../objs/lib/ModSecurity-nginx/tests/* .
172+
- name: Remove /usr/bin/link conflicting with MSVC link.exe
173+
run: |
174+
set -ex
175+
which link
176+
rm /usr/bin/link
177+
- name: Build nginx w/ModSecurity-nginx module
178+
working-directory: nginx
179+
run: |
180+
: # Windows native version of Perl is required by nginx build
181+
export PATH=/c/Strawberry/perl/bin:$PATH
182+
: # Set env variables to point to libModSecurity v3 include & lib directories
183+
export MODSECURITY_INC=objs/lib/ModSecurity/headers
184+
export MODSECURITY_LIB=objs/lib/ModSecurity/build/win32/build/Release
185+
: # Copy libModSecurity.dll to objs dir (to be able to run nginx later)
186+
cp $MODSECURITY_LIB/libModSecurity.dll objs
187+
: # Configure nginx build w/ModSecurity-nginx module
188+
auto/configure \
189+
--with-cc=cl \
190+
--with-debug \
191+
--prefix= \
192+
--conf-path=conf/nginx.conf \
193+
--pid-path=logs/nginx.pid \
194+
--http-log-path=logs/access.log \
195+
--error-log-path=logs/error.log \
196+
--sbin-path=nginx.exe \
197+
--http-client-body-temp-path=temp/client_body_temp \
198+
--http-proxy-temp-path=temp/proxy_temp \
199+
--http-fastcgi-temp-path=temp/fastcgi_temp \
200+
--http-scgi-temp-path=temp/scgi_temp \
201+
--http-uwsgi-temp-path=temp/uwsgi_temp \
202+
--with-cc-opt=-DFD_SETSIZE=1024 \
203+
--with-pcre=objs/lib/pcre2-10.39 \
204+
--with-zlib=objs/lib/zlib-1.3 \
205+
--with-openssl=objs/lib/openssl-3.0.13 \
206+
--with-openssl-opt=no-asm \
207+
--with-http_ssl_module \
208+
--with-http_v2_module \
209+
--with-http_auth_request_module \
210+
--add-module=objs/lib/ModSecurity-nginx
211+
nmake
212+
- name: Run ModSecurity-nginx tests
213+
working-directory: nginx/test
214+
shell: cmd # tests need to run on a "windows" shell
215+
run: |
216+
md temp
217+
set TEMP=temp
218+
set TEST_NGINX_BINARY=..\objs\nginx.exe
219+
prove modsecurity*.t

config

+11-5
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ if [ -n "$MODSECURITY_INC" -o -n "$MODSECURITY_LIB" ]; then
3232
ngx_modsecurity_opt_I="-I$MODSECURITY_INC"
3333
ngx_modsecurity_opt_L="-L$MODSECURITY_LIB $YAJL_EXTRA"
3434

35-
if [ $NGX_RPATH = YES ]; then
36-
ngx_feature_libs="-R$MODSECURITY_LIB -L$MODSECURITY_LIB -lmodsecurity $YAJL_EXTRA"
37-
elif [ "$NGX_IGNORE_RPATH" != "YES" -a $NGX_SYSTEM = "Linux" ]; then
38-
ngx_feature_libs="-Wl,-rpath,$MODSECURITY_LIB -L$MODSECURITY_LIB -lmodsecurity $YAJL_EXTRA"
35+
if [ "$NGX_CC_NAME" != msvc ]; then
36+
if [ $NGX_RPATH = YES ]; then
37+
ngx_feature_libs="-R$MODSECURITY_LIB -L$MODSECURITY_LIB -lmodsecurity $YAJL_EXTRA"
38+
elif [ "$NGX_IGNORE_RPATH" != "YES" -a $NGX_SYSTEM = "Linux" ]; then
39+
ngx_feature_libs="-Wl,-rpath,$MODSECURITY_LIB -L$MODSECURITY_LIB -lmodsecurity $YAJL_EXTRA"
40+
else
41+
ngx_feature_libs="-L$MODSECURITY_LIB -lmodsecurity $YAJL_EXTRA"
42+
fi
3943
else
40-
ngx_feature_libs="-L$MODSECURITY_LIB -lmodsecurity $YAJL_EXTRA"
44+
# Adjust link library arguments to work with MSVC C++ compiler and
45+
# output of the Windows port of libModSecurity v3
46+
ngx_feature_libs="$MODSECURITY_LIB/libModSecurity.lib $YAJL_EXTRA"
4147
fi
4248

4349
. auto/feature

src/ngx_http_modsecurity_body_filter.c

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*
1414
*/
1515

16+
#include <ngx_config.h>
17+
1618
#ifndef MODSECURITY_DDEBUG
1719
#define MODSECURITY_DDEBUG 0
1820
#endif

src/ngx_http_modsecurity_header_filter.c

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*
1414
*/
1515

16+
#include <ngx_config.h>
17+
1618
#ifndef MODSECURITY_DDEBUG
1719
#define MODSECURITY_DDEBUG 0
1820
#endif

src/ngx_http_modsecurity_log.c

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*
1414
*/
1515

16+
#include <ngx_config.h>
17+
1618
#ifndef MODSECURITY_DDEBUG
1719
#define MODSECURITY_DDEBUG 0
1820
#endif

src/ngx_http_modsecurity_module.c

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,22 @@
1313
*
1414
*/
1515

16+
#include <ngx_config.h>
17+
1618
#ifndef MODSECURITY_DDEBUG
1719
#define MODSECURITY_DDEBUG 0
1820
#endif
1921
#include "ddebug.h"
2022

2123
#include "ngx_http_modsecurity_common.h"
2224
#include "stdio.h"
23-
#include <ngx_config.h>
2425
#include <ngx_core.h>
2526
#include <ngx_http.h>
2627

28+
#ifdef _MSC_VER
29+
#define strdup _strdup
30+
#endif
31+
2732
static ngx_int_t ngx_http_modsecurity_init(ngx_conf_t *cf);
2833
static void *ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf);
2934
static char *ngx_http_modsecurity_init_main_conf(ngx_conf_t *cf, void *conf);
@@ -131,7 +136,7 @@ ngx_inline char *ngx_str_to_char(ngx_str_t a, ngx_pool_t *p)
131136
}
132137

133138

134-
ngx_inline int
139+
int
135140
ngx_http_modsecurity_process_intervention (Transaction *transaction, ngx_http_request_t *r, ngx_int_t early_log)
136141
{
137142
char *log = NULL;
@@ -254,7 +259,7 @@ ngx_http_modsecurity_cleanup(void *data)
254259
}
255260

256261

257-
ngx_inline ngx_http_modsecurity_ctx_t *
262+
ngx_http_modsecurity_ctx_t *
258263
ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
259264
{
260265
ngx_str_t s;

src/ngx_http_modsecurity_pre_access.c

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*
1414
*/
1515

16+
#include <ngx_config.h>
17+
1618
#ifndef MODSECURITY_DDEBUG
1719
#define MODSECURITY_DDEBUG 0
1820
#endif

src/ngx_http_modsecurity_rewrite.c

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*
1414
*/
1515

16+
#include <ngx_config.h>
17+
1618
#ifndef MODSECURITY_DDEBUG
1719
#define MODSECURITY_DDEBUG 0
1820
#endif

0 commit comments

Comments
 (0)