Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.git
.github
node_modules
**/node_modules
web/.next
web/next-env.d.ts
web/out/*
!web/out/.gitkeep
test/e2e/out
19 changes: 19 additions & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: e2e
on:
pull_request:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: read
jobs:
e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: bash test/e2e/run.sh
- if: always()
uses: actions/upload-artifact@v4
with:
name: e2e-out
path: test/e2e/out
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ web/.next/
web/next-env.d.ts
web/out/*
!web/out/.gitkeep
# e2e output (test/e2e/run.sh): reports, access log, compose logs
test/e2e/out/
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# One image with two commands: `touchstone serve` (the page and POST /v1/scans)
# and `touchstone scan <url>` (the CLI). See docs/architecture/touchstone.md,
# "Deployment shape".

# The page is embedded at build time (web/embed.go), so it is built first.
FROM node:20-alpine AS web
RUN apk add --no-cache make
WORKDIR /src
COPY Makefile ./
COPY web ./web
RUN make web-build

FROM golang:1.26 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
COPY --from=web /src/web/out/ ./web/out/
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/touchstone ./cmd/touchstone

FROM gcr.io/distroless/static-debian12:nonroot
COPY --from=build /out/touchstone /touchstone
# serve listens on all interfaces inside the container; the default is loopback.
ENV TOUCHSTONE_ADDR=0.0.0.0:8080
EXPOSE 8080
USER nonroot:nonroot
ENTRYPOINT ["/touchstone"]
CMD ["serve"]
39 changes: 39 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Local dev and the passive-only e2e (test/e2e/run.sh).
#
# The vendor network uses the documentation range 198.51.100.0/24, which the
# SSRF guard (internal/fetch/passive.go, IsPrivate) does not refuse. So both
# `serve` and the CLI scan fixture-vendor with the guard fully on; no
# --allow-private is used anywhere in this stack.
services:
touchstone:
build: .
command: ["serve"]
ports:
- "127.0.0.1:8080:8080"
networks:
vendor:
depends_on:
fixture-vendor:
condition: service_healthy

fixture-vendor:
image: nginx:1.27-alpine
volumes:
- ./test/fixtures/sites:/usr/share/nginx/html:ro
- ./test/fixtures/fixture-vendor.conf:/etc/nginx/conf.d/default.conf:ro
# The access log the e2e reads; see test/e2e/run.sh.
- ./test/e2e/out/access:/var/log/fixture
healthcheck:
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1/healthz"]
interval: 2s
timeout: 2s
retries: 15
networks:
vendor:
ipv4_address: 198.51.100.10

networks:
vendor:
ipam:
config:
- subnet: 198.51.100.0/24
71 changes: 71 additions & 0 deletions test/e2e/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env bash
# Passive-only e2e for docker-compose.yml. It scans fixture-vendor through the
# CLI and through POST /v1/scans, then checks that the two reports agree and
# that the vendor's nginx access log matches the report's checked list.
#
# Usage: bash test/e2e/run.sh (needs docker compose, curl and jq)
set -euo pipefail

cd "$(dirname "$0")/../.."

OUT=test/e2e/out
TARGET=http://fixture-vendor/
SERVE=http://127.0.0.1:8080
ALLOWED='["TARGET","SUBRESOURCE","DISCOVERY_PATH","DECLARED"]'
# The two scans start and finish at different times; nothing else may differ.
STRIP='del(.startedAt, .completedAt, .vulnDataAsOf)'

fail() {
echo "e2e: FAIL: $*" >&2
exit 1
}

teardown() {
docker compose logs --no-color > "$OUT/compose.log" 2>&1 || true
docker compose down -v
}

rm -rf "$OUT"
mkdir -p "$OUT/access"
trap teardown EXIT

docker compose up -d --build --wait

for _ in $(seq 60); do
curl -fsS -o /dev/null "$SERVE/" 2>/dev/null && break
sleep 1
done
curl -fsS -o /dev/null "$SERVE/" || fail "touchstone serve did not answer on $SERVE"

# Both scans go to fixture-vendor by its compose name; neither uses
# --allow-private, so the SSRF guard is on for both.
docker compose run --rm -T touchstone scan "$TARGET" > "$OUT/cli.json"
curl -fsS -X POST -H 'Content-Type: application/json' \
-d "{\"url\": \"$TARGET\"}" "$SERVE/v1/scans" > "$OUT/http.json"

jq -S "$STRIP" "$OUT/cli.json" > "$OUT/cli.norm.json"
jq -S "$STRIP" "$OUT/http.json" > "$OUT/http.norm.json"
diff -u "$OUT/cli.norm.json" "$OUT/http.norm.json" \
|| fail "CLI and POST /v1/scans reports differ (see diff above)"

jq -e --argjson allowed "$ALLOWED" \
'[.checked[] | select(.reason as $r | $allowed | contains([$r]) | not)] | length == 0' \
"$OUT/cli.json" > /dev/null \
|| fail "checked list has a location with a reason outside $ALLOWED"

# Passive-only proof. Refused requests never left touchstone, so they are
# left out of the checked side. The access log is combined format: field 2 is
# the request line.
LOG="$OUT/access/access.log"
[ -s "$LOG" ] || fail "no access log at $LOG"
awk -F'"' '{ split($2, r, " "); print r[1] }' "$LOG" | sort -u > "$OUT/methods.txt"
[ "$(cat "$OUT/methods.txt")" = "GET" ] \
|| fail "fixture-vendor saw methods other than GET: $(tr '\n' ' ' < "$OUT/methods.txt")"

awk -F'"' '{ split($2, r, " "); print r[2] }' "$LOG" | sort -u > "$OUT/logged.txt"
jq -r '.checked[] | select(.result != "RESULT_REFUSED") | .url | sub("^https?://[^/]+"; "")' \
"$OUT/cli.json" | sort -u > "$OUT/sent.txt"
diff -u "$OUT/sent.txt" "$OUT/logged.txt" \
|| fail "access log and checked list differ: '-' is checked but never requested, '+' is requested but not checked"

echo "e2e: OK: $(wc -l < "$OUT/logged.txt") requested paths, all in the checked list; CLI and POST /v1/scans reports identical"
20 changes: 20 additions & 0 deletions test/fixtures/fixture-vendor.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# fixture-vendor: serves test/fixtures/sites and logs every request to the
# access log that test/e2e/run.sh checks against the report's checked list.
server {
listen 80;
root /usr/share/nginx/html;

access_log /var/log/fixture/access.log combined;

# Compose's health check. Not logged, so the passive-only check never
# sees it.
location = /healthz {
access_log off;
default_type text/plain;
return 200 "ok\n";
}

location / {
try_files $uri $uri/ =404;
}
}
11 changes: 11 additions & 0 deletions test/fixtures/sites/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fixture vendor</title>
</head>
<body>
<h1>Fixture vendor</h1>
<script src="/js/jquery-1.12.4.min.js"></script>
</body>
</html>
2 changes: 2 additions & 0 deletions test/fixtures/sites/js/jquery-1.12.4.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*! jQuery v1.12.4 fixture stand-in | the scanner reads the version from the URL, not from this body */
window.jQuery = window.$ = function () { return {}; };
Loading