Skip to content

Commit ed8f034

Browse files
authored
add job to automatically fetch new CRS versions (#142)
1 parent 3c2f2d0 commit ed8f034

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

.github/crs-version.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v4.14.0

.github/workflows/crs-update.yaml

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
name: Sync CRS Rules
2+
on:
3+
schedule:
4+
- cron: "0 0 * * *" # every day at midnight
5+
workflow_dispatch:
6+
7+
# Needs to push a branch and open PRs
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
jobs:
13+
sync-crs:
14+
runs-on: ubuntu-latest
15+
env:
16+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
17+
CRS_REPO: coreruleset/coreruleset
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
- name: Check for new CRS release
24+
run: |
25+
set -euo pipefail
26+
27+
UPSTREAM_VERSION=$(gh api repos/$CRS_REPO/releases/latest -q .tag_name)
28+
if [ -z "$UPSTREAM_VERSION" ]; then
29+
echo "::error::Failed to fetch latest CRS release"
30+
exit 1
31+
fi
32+
echo "Latest upstream version: $UPSTREAM_VERSION"
33+
34+
CURRENT_VERSION=$(cat .github/crs-version.txt 2>/dev/null || echo "")
35+
echo "Current tracked version: $CURRENT_VERSION"
36+
37+
if [ "$UPSTREAM_VERSION" = "$CURRENT_VERSION" ]; then
38+
echo "Already up to date at $UPSTREAM_VERSION"
39+
exit 0
40+
fi
41+
42+
# Strip 'v' prefix for directory naming
43+
VERSION_NUMBER="${UPSTREAM_VERSION#v}"
44+
45+
# Check for existing open PR to avoid duplicates
46+
EXISTING_PR=$(gh pr list --search "Update CRS to ${UPSTREAM_VERSION}" --state open --json number -q '.[0].number' || echo "")
47+
if [ -n "$EXISTING_PR" ]; then
48+
echo "PR #$EXISTING_PR already exists for $UPSTREAM_VERSION, skipping"
49+
exit 0
50+
fi
51+
52+
echo "UPSTREAM_VERSION=$UPSTREAM_VERSION" >> $GITHUB_ENV
53+
echo "VERSION_NUMBER=$VERSION_NUMBER" >> $GITHUB_ENV
54+
- name: Update CRS rules
55+
if: env.UPSTREAM_VERSION != ''
56+
run: |
57+
set -euo pipefail
58+
59+
# Download source tarball
60+
gh release download "$UPSTREAM_VERSION" --repo "$CRS_REPO" -A tar.gz
61+
62+
# Create target directory
63+
TARGET_DIR="appsec/crs/$VERSION_NUMBER"
64+
mkdir -p "$TARGET_DIR"
65+
66+
# Extract only .conf and .data files from rules/ directory
67+
tar -xzf coreruleset-*.tar.gz --wildcards --strip-components=2 \
68+
-C "$TARGET_DIR" '*/rules/*.conf' '*/rules/*.data'
69+
70+
# Generate custom crs-setup.conf with version-specific values
71+
CRS_SETUP_VERSION=$(echo "$VERSION_NUMBER" | awk -F. '{printf "%d%02d%d", $1, $2, $3}')
72+
cat > "$TARGET_DIR/crs-setup.conf" << EOF
73+
SecDefaultAction "phase:1,log,auditlog,pass"
74+
SecDefaultAction "phase:2,log,auditlog,pass"
75+
SecCollectionTimeout 600
76+
77+
SecAction \\
78+
"id:900990,\\
79+
phase:1,\\
80+
pass,\\
81+
t:none,\\
82+
nolog,\\
83+
tag:'OWASP_CRS',\\
84+
ver:'OWASP_CRS/$VERSION_NUMBER',\\
85+
setvar:tx.crs_setup_version=$CRS_SETUP_VERSION"
86+
EOF
87+
88+
# Clean up tarball
89+
rm -f coreruleset-*.tar.gz
90+
91+
# Configure git identity
92+
git config user.name "github-actions[bot]"
93+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
94+
95+
# Create branch
96+
BRANCH_NAME="update-crs-$UPSTREAM_VERSION"
97+
git checkout -b "$BRANCH_NAME"
98+
99+
# Update version tracking file
100+
echo -n "$UPSTREAM_VERSION" > .github/crs-version.txt
101+
102+
# Stage and commit
103+
git add "$TARGET_DIR/" .github/crs-version.txt
104+
git commit -m "Add CRS $UPSTREAM_VERSION rules"
105+
git push --set-upstream origin "$BRANCH_NAME" --force-with-lease
106+
107+
# Create PR
108+
gh pr create \
109+
--title "Update CRS to $UPSTREAM_VERSION" \
110+
--body "This PR adds CRS version $VERSION_NUMBER rules from upstream release $UPSTREAM_VERSION.
111+
112+
**Changes:**
113+
- Added \`appsec/crs/$VERSION_NUMBER/\` with all rule files (.conf) and data files (.data) from upstream
114+
- Updated version tracker to $UPSTREAM_VERSION
115+
116+
**Source:** https://github.com/$CRS_REPO/releases/tag/$UPSTREAM_VERSION"

0 commit comments

Comments
 (0)