Skip to content

Commit e619253

Browse files
jcchavezsanuraaga
andauthored
Syncing anuraaga's main before donation (envoyproxy#8)
* Allow building filter in mode that prints timing of each lifecycle me… (envoyproxy#33) …thod * Add variables for running FTW in cloud mode with envoy wasm disabled (envoyproxy#34) * Switch FTW backend to faster server (envoyproxy#35) * Try adding -crt-static to rust build (envoyproxy#36) * Build wasi-libc for TinyGo instead of using wasi SDK (envoyproxy#37) * Fix aho-corasick wasm lib and use it for pm (envoyproxy#38) * Update to latest coraza (envoyproxy#39) * Move cgo declaration to tinygo build tag to prevent warning when runn… (envoyproxy#40) …ing tests * Remove year from copyright header Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com>
1 parent 71aac7a commit e619253

34 files changed

+391
-123
lines changed

buildtools/aho-corasick/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ RUN mkdir -p /aho-corasick && curl -L https://github.com/BurntSushi/aho-corasick
99
WORKDIR /aho-corasick
1010
ADD aho-corasick.patch aho-corasick.patch
1111
RUN patch -p1 < aho-corasick.patch
12+
ENV RUSTFLAGS "-C target-feature=-crt-static"
1213
RUN cargo build --release --target wasm32-wasi
1314

1415
CMD ["cp", "target/wasm32-wasi/release/libaho_corasick.a", "/out/libaho_corasick.a"]

buildtools/aho-corasick/aho-corasick.patch

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
diff --git a/.gitignore b/.gitignore
2+
index f1a4d65..d6ff1a3 100644
3+
--- a/.gitignore
4+
+++ b/.gitignore
5+
@@ -1,3 +1,4 @@
6+
+.idea
7+
.*.swp
8+
doc
9+
tags
110
diff --git a/Cargo.toml b/Cargo.toml
211
index 610bd4d..55e2f37 100644
312
--- a/Cargo.toml
@@ -12,22 +21,35 @@ index 610bd4d..55e2f37 100644
1221
[features]
1322
diff --git a/src/exports.rs b/src/exports.rs
1423
new file mode 100644
15-
index 0000000..f97de6d
24+
index 0000000..29c203d
1625
--- /dev/null
1726
+++ b/src/exports.rs
18-
@@ -0,0 +1,93 @@
27+
@@ -0,0 +1,107 @@
1928
+use std::mem::MaybeUninit;
2029
+use std::slice;
30+
+use std::str;
2131
+use crate::{AhoCorasick, AhoCorasickBuilder, MatchKind};
2232
+
2333
+static mut MATCHERS: Vec<AhoCorasick> = Vec::new();
2434
+
2535
+#[no_mangle]
26-
+pub extern "C" fn new_matcher(patterns_ptr: usize, patterns_len: usize) -> usize {
27-
+ let patterns_str = ptr_to_string(patterns_ptr, patterns_len);
28-
+ std::mem::forget(&patterns_str);
36+
+pub extern "C" fn new_matcher(patterns_ptr: *mut u8, patterns_len: usize) -> usize {
37+
+ let all_patterns = unsafe {
38+
+ slice::from_raw_parts(patterns_ptr, patterns_len)
39+
+ };
2940
+
30-
+ let patterns = patterns_str.split(' ');
41+
+ let mut patterns = Vec::new();
42+
+
43+
+ let mut off = 0;
44+
+ while off < patterns_len {
45+
+ let pattern_len = u32::from_le_bytes([all_patterns[off], all_patterns[off+1], all_patterns[off+2], all_patterns[off+3]]) as usize;
46+
+ off += 4;
47+
+ let pattern = unsafe {
48+
+ str::from_utf8_unchecked(&all_patterns[off..off+pattern_len])
49+
+ };
50+
+ patterns.push(pattern);
51+
+ off += pattern_len;
52+
+ }
3153
+
3254
+ let ac = AhoCorasickBuilder::new()
3355
+ .ascii_case_insensitive(true)
@@ -39,6 +61,7 @@ index 0000000..f97de6d
3961
+ MATCHERS.push(ac);
4062
+ MATCHERS.len() - 1
4163
+ }
64+
+
4265
+}
4366
+
4467
+#[no_mangle]

buildtools/tinygo/Dockerfile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ RUN curl -L https://go.dev/dl/go1.19.1.linux-${TARGETARCH:-amd64}.tar.gz | tar -
1010
ENV PATH /go/bin:/root/go/bin:$PATH
1111
ENV GOROOT /go
1212

13-
RUN apt-get install -y libclang-14-dev wabt binaryen
13+
RUN apt-get install -y libclang-14-dev wabt binaryen git
1414

1515
# https://github.com/tinygo-org/tinygo/commit/9e4e182615cd80303c564f95020e0c3bd10af64a
16-
RUN go install github.com/tinygo-org/tinygo@9e4e182615cd80303c564f95020e0c3bd10af64a
17-
18-
RUN mkdir -p $(tinygo env TINYGOROOT)/lib/wasi-libc/ && \
19-
ln -s /wasi-sysroot $(tinygo env TINYGOROOT)/lib/wasi-libc/sysroot
16+
RUN git clone --shallow-submodules --recursive https://github.com/tinygo-org/tinygo --branch dev
17+
WORKDIR /tinygo
18+
RUN git reset --hard 9e4e182615cd80303c564f95020e0c3bd10af64a
19+
RUN go install
20+
RUN make wasi-libc

config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 The OWASP Coraza contributors
1+
// Copyright The OWASP Coraza contributors
22
// SPDX-License-Identifier: Apache-2.0
33

44
package main

config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2022 The OWASP Coraza contributors
1+
// Copyright The OWASP Coraza contributors
22
// SPDX-License-Identifier: Apache-2.0
33

44
package main

ftw/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ WORKDIR /workspace
77

88
RUN apk update && apk add curl
99

10-
RUN go install github.com/anuraaga/go-ftw@dev
10+
RUN go install github.com/fzipi/go-ftw@fd953f4f9ddd0f21595be4f48f0b468dda32e801
1111

1212
ADD https://github.com/coreruleset/coreruleset/archive/refs/tags/v4.0.0-rc1.tar.gz /workspace/coreruleset/
1313
RUN cd coreruleset && tar -xf v4.0.0-rc1.tar.gz --strip-components 1

ftw/docker-compose.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
httpbin:
3-
image: kennethreitz/httpbin:latest
3+
image: ealen/echo-server:latest
44
chown:
55
image: alpine:3.16
66
command:
@@ -16,7 +16,7 @@ services:
1616
image: envoyproxy/envoy:v1.23-latest
1717
command:
1818
- -c
19-
- /conf/envoy-config.yaml
19+
- ${ENVOY_CONFIG:-/conf/envoy-config.yaml}
2020
- --log-level
2121
- info
2222
- --component-log-level
@@ -54,6 +54,8 @@ services:
5454
depends_on:
5555
- wasm-logs
5656
build: .
57+
environment:
58+
- FTW_CLOUDMODE
5759
volumes:
5860
- logs:/home/envoy/logs:ro
5961
volumes:

ftw/envoy-config-nowasm.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
static_resources:
2+
listeners:
3+
- address:
4+
socket_address:
5+
address: 0.0.0.0
6+
port_value: 80
7+
filter_chains:
8+
- filters:
9+
- name: envoy.filters.network.http_connection_manager
10+
typed_config:
11+
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
12+
stat_prefix: ingress_http
13+
codec_type: auto
14+
http_protocol_options:
15+
accept_http_10: true
16+
route_config:
17+
virtual_hosts:
18+
- name: local_route
19+
domains:
20+
- "*"
21+
routes:
22+
- match:
23+
prefix: "/"
24+
route:
25+
cluster: local_server
26+
http_filters:
27+
- name: envoy.filters.http.router
28+
typed_config:
29+
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
30+
31+
clusters:
32+
- name: local_server
33+
connect_timeout: 6000s
34+
type: strict_dns
35+
lb_policy: round_robin
36+
load_assignment:
37+
cluster_name: local_server
38+
endpoints:
39+
- lb_endpoints:
40+
- endpoint:
41+
address:
42+
socket_address:
43+
address: httpbin
44+
port_value: 80

ftw/tests.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ while [[ "$status_code" -eq "000" ]]; do
2929
done
3030
echo -e "\n[Ok] Got status code $status_code, expected 200. Ready to start."
3131

32-
go-ftw run -d coreruleset/tests/regression/tests --config ftw.yml --read-timeout=10s || (echo "Envoy Logs:" && cat /home/envoy/logs/envoy.log)
32+
FTW_CLOUDMODE=${FTW_CLOUDMODE:-false}
33+
34+
go-ftw run -d coreruleset/tests/regression/tests --config ftw.yml --read-timeout=10s --cloud=$FTW_CLOUDMODE || (echo "Envoy Logs:" && cat /home/envoy/logs/envoy.log)

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ module github.com/jcchavezs/coraza-wasm-filter
33
go 1.17
44

55
require (
6-
github.com/corazawaf/coraza/v3 v3.0.0-20220913021343-a3bd8c85ebf5
6+
github.com/corazawaf/coraza/v3 v3.0.0-20220928011626-fce26f25ab3e
77
github.com/magefile/mage v1.13.0
88
github.com/stretchr/testify v1.8.0
9-
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220831045923-bd6f69563ef4
9+
github.com/tetratelabs/proxy-wasm-go-sdk v0.19.1-0.20220922045757-132ee0a06ac2
1010
github.com/tidwall/gjson v1.14.3
1111
)
1212

1313
require (
14-
github.com/corazawaf/libinjection-go v0.0.0-20220909190158-227e7e772cef // indirect
14+
github.com/corazawaf/libinjection-go v0.1.1 // indirect
1515
github.com/davecgh/go-spew v1.1.1 // indirect
1616
github.com/kr/pretty v0.1.0 // indirect
1717
github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9 // indirect
1818
github.com/pmezard/go-difflib v1.0.0 // indirect
1919
github.com/tetratelabs/wazero v1.0.0-beta.2 // indirect
2020
github.com/tidwall/match v1.1.1 // indirect
2121
github.com/tidwall/pretty v1.2.0 // indirect
22-
golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced // indirect
22+
golang.org/x/net v0.0.0-20220909164309-bea034e7d591 // indirect
2323
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
2424
gopkg.in/yaml.v3 v3.0.1 // indirect
2525
)

0 commit comments

Comments
 (0)