Skip to content

Commit 7879db3

Browse files
Merge pull request #18 from signalsciences/allow-http-redirects
Allow http redirects
2 parents c0e10cb + 5b99fbb commit 7879db3

File tree

12 files changed

+161
-20
lines changed

12 files changed

+161
-20
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ _testmain.go
2626
*.exe
2727
*.test
2828
*.prof
29+
30+
scripts/*/goroot/

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
* Deprecated the `AltResponseCodes` concept in favor of using all codes 300-599 as "blocking"
6+
* Added HTTP redirect support
67

78
## 1.7.1 2020-04-06
89

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.10.6-alpine3.8
1+
FROM golang:1.14-alpine
22

33
COPY goroot/ /go/
44
# this is used to lint and build tarball

Dockerfile.git

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
FROM golang:1.10.6-alpine3.8
1+
FROM golang:1.14-alpine
22
RUN apk --update add git

make.sh

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,20 @@ set -ex
66
find . -name "goroot" -type d | xargs rm -rf
77
mkdir goroot
88

9-
10-
docker build -f Dockerfile.git -t golang-git:1.10.6-alpine3.8 .
11-
docker run --user $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}/goroot:/go/ --rm golang-git:1.10.6-alpine3.8 /bin/sh -c 'go get github.com/signalsciences/tlstext && go get github.com/tinylib/msgp && go get github.com/alecthomas/gometalinter'
9+
docker build -f Dockerfile.git -t golang-git:1.14-alpine .
10+
docker run --user $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}/goroot:/go/ --rm golang-git:1.14-alpine /bin/sh -c 'go get github.com/signalsciences/tlstext && go get github.com/tinylib/msgp && go get github.com/alecthomas/gometalinter'
1211
./scripts/build-docker.sh
1312

1413
# run module tests
1514
./scripts/test.sh
1615

17-
1816
BASE=$PWD
1917
## setup our package properties by distro
2018
PKG_NAME="sigsci-module-golang"
2119
DEST_BUCKET="package-build-artifacts"
2220
DEST_KEY="${PKG_NAME}/${GITHUB_RUN_NUMBER}"
2321
VERSION=$(cat ./VERSION)
2422

25-
2623
cd ${BASE}
2724
echo "DONE"
2825

@@ -51,9 +48,3 @@ aws s3api put-object \
5148
--body "CHANGELOG.md" \
5249
--key "${DEST_KEY}/CHANGELOG.md" \
5350
--grant-full-control id="${PROD_ID}"
54-
55-
56-
57-
58-
59-

module.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,25 @@ func (m *Module) ServeHTTP(w http.ResponseWriter, req *http.Request) {
122122
wafresponse := out.WAFResponse
123123
switch {
124124
case m.config.IsAllowCode(int(wafresponse)):
125-
// continue with normal request
125+
// Continue with normal request
126126
m.handler.ServeHTTP(rw, req)
127127
case m.config.IsBlockCode(int(wafresponse)):
128128
status := int(wafresponse)
129+
130+
// Only redirect if it is a redirect status (3xx) AND there is a redirect URL
131+
if status >= 300 && status <= 399 {
132+
redirect := req.Header.Get("X-Sigsci-Redirect")
133+
if len(redirect) > 0 {
134+
http.Redirect(rw, req, redirect, status)
135+
break
136+
}
137+
}
138+
139+
// Block
129140
http.Error(rw, fmt.Sprintf("%d %s\n", status, http.StatusText(status)), status)
130141
default:
131142
log.Printf("ERROR: Received invalid response code from inspector (failing open): %d", wafresponse)
132-
// continue with normal request
143+
// Continue with normal request
133144
m.handler.ServeHTTP(rw, req)
134145
}
135146

@@ -229,15 +240,25 @@ func (m *Module) inspectorPreRequest(req *http.Request) (inspin2 RPCMsgIn2, out
229240
return
230241
}
231242

232-
// set any request headers
233243
if out.RequestID != "" {
234-
req.Header.Add("X-Sigsci-Requestid", out.RequestID)
244+
req.Header.Set("X-Sigsci-Requestid", out.RequestID)
245+
} else {
246+
req.Header.Del("X-Sigsci-Requestid")
235247
}
236248

237249
wafresponse := out.WAFResponse
238-
req.Header.Add("X-Sigsci-Agentresponse", strconv.Itoa(int(wafresponse)))
250+
req.Header.Set("X-Sigsci-Agentresponse", strconv.Itoa(int(wafresponse)))
251+
252+
// Add request headers from the WAF response to the request
253+
req.Header.Del("X-Sigsci-Tags")
254+
req.Header.Del("X-Sigsci-Redirect")
239255
for _, kv := range out.RequestHeaders {
240-
req.Header.Add(kv[0], kv[1])
256+
// For X-Sigsci-* headers, use Set to override, but for custom headers, use Add to append
257+
if strings.HasPrefix(http.CanonicalHeaderKey(kv[0]), "X-Sigsci-") {
258+
req.Header.Set(kv[0], kv[1])
259+
} else {
260+
req.Header.Add(kv[0], kv[1])
261+
}
241262
}
242263

243264
inspin2 = RPCMsgIn2{

scripts/test-golang114/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM golang:1.14-alpine
2+
3+
COPY goroot/ /go/
4+
5+
# we will mount the current directory here
6+
VOLUME [ "/go/src/github.com/signalsciences/sigsci-module-golang" ]
7+
WORKDIR /go/src/github.com/signalsciences/sigsci-module-golang
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: "3"
2+
3+
services:
4+
# this defines our webserver uses our sigsci-module
5+
# we only define it so it is attached to our fake network
6+
# it will be run a few times with different options manually
7+
#
8+
# The volumes spec is a bit weird.. this script is run in scripts/test but
9+
# needs stuff in ../../examples. Consider moving.
10+
web:
11+
volumes:
12+
- ../..:/go/src/github.com/signalsciences/sigsci-module-golang
13+
command: [ "go", "run", "/go/src/github.com/signalsciences/sigsci-module-golang/examples/mtest/main.go" ]
14+
environment:
15+
- DEBUG=0
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
version: "3"
2+
networks:
3+
mtest:
4+
5+
services:
6+
# this defines our webserver uses our sigsci-module
7+
# we only define it so it is attached to our fake network
8+
# it will be run a few times with different options manually
9+
#
10+
#
11+
web:
12+
build:
13+
context: .
14+
dockerfile: Dockerfile
15+
expose:
16+
- "8085"
17+
networks:
18+
- mtest
19+
depends_on:
20+
- agent
21+
22+
# agent
23+
agent:
24+
image: 803688608479.dkr.ecr.us-west-2.amazonaws.com/local-dev/sigsci-agent:latest
25+
command: [ "-debug-log-web-inputs", "2", "-rpc-address", "9090", "-debug-rpc-test-harness", "-debug-standalone", "3" ]
26+
expose:
27+
- "9090"
28+
- "12345"
29+
networks:
30+
- mtest
31+
32+
# punching bag
33+
punchingbag:
34+
image: 803688608479.dkr.ecr.us-west-2.amazonaws.com/local-dev/module-testing:latest
35+
networks:
36+
- mtest
37+
expose:
38+
- "8086"
39+
command: [ "/bin/punchingbag", "-addr", ":8086" ]
40+
41+
# mtest
42+
#
43+
mtest:
44+
image: 803688608479.dkr.ecr.us-west-2.amazonaws.com/local-dev/module-testing:latest
45+
networks:
46+
- mtest
47+
depends_on:
48+
- web
49+
- agent
50+
- punchingbag
51+
environment:
52+
- DISABLE_HTTP_OPTIONS=1
53+
- DISABLE_NOCOOKIE=1
54+
- MTEST_BASEURL=web:8085
55+
- MTEST_AGENT=agent:12345
56+
- "MTEST_RUN_TEST_BLOCK_VIA_REDIRECT=true"
57+
command: [ "/bin/wait-for", "web:8085", "--", "/bin/mtest", "-test.v" ]
58+

scripts/test-golang114/test.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
set -e
3+
4+
DOCKERCOMPOSE="docker-compose"
5+
6+
# run at end no matter what
7+
cleanup() {
8+
echo "shutting down"
9+
# capture log output
10+
$DOCKERCOMPOSE logs --no-color agent >& agent.log
11+
$DOCKERCOMPOSE logs --no-color web >& web.log
12+
$DOCKERCOMPOSE logs --no-color mtest >& mtest.log
13+
$DOCKERCOMPOSE logs --no-color punchingbag >& punchingbag.log
14+
15+
# delete everything
16+
$DOCKERCOMPOSE down
17+
18+
# show output of module testing
19+
cat mtest.log
20+
}
21+
trap cleanup 0 1 2 3 6
22+
23+
set -x
24+
25+
# attempt to clean up any leftover junk
26+
$DOCKERCOMPOSE down
27+
28+
$DOCKERCOMPOSE pull --ignore-pull-failures
29+
30+
# start everything, run tests
31+
#
32+
# --no-color --> safe for ci
33+
# --build --> alway build test server/module container
34+
# --abort-on-container-exit --> without this, the other servers keep the process running
35+
# --exit-code-from mtest --> make exit code be the result of module test
36+
#
37+
# > /dev/null --> output of all servers is mixed together and ugly
38+
# we get the individual logs at end
39+
#
40+
if [ -d "goroot" ]; then
41+
rm -rf goroot
42+
fi
43+
docker run -v ${PWD}/goroot:/go/ --rm golang:1.14-alpine /bin/sh -c 'apk --update add git && go get github.com/signalsciences/tlstext && go get github.com/tinylib/msgp && go get github.com/alecthomas/gometalinter'
44+
$DOCKERCOMPOSE up --no-color --build --abort-on-container-exit --exit-code-from mtest > /dev/null
45+

0 commit comments

Comments
 (0)