repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
sync-mirrors.ymlYAML130 lines4.7 KiBraw download
name: Sync upstream mirrors

# Mirrors swisskyrepo's PayloadsAllTheThings and InternalAllTheThings into the
# site and pushes straight to main. No review gate: the build and the test suite
# are what stand between upstream and the live site, so a sync that breaks
# either one fails here and never lands. The build itself refuses imported
# content that carries a script (scripts/csp-hashes.mjs) and the importers
# refuse symbolic links (scripts/safe_source.py).

on:
  schedule:
    - cron: '17 6 * * *'   # daily 06:17 UTC
  workflow_dispatch:

# Nothing is granted at the top level; each job asks for what it needs.
permissions: {}

concurrency:
  group: sync-mirrors
  cancel-in-progress: false

# Two jobs, split on purpose.
#
# `convert` is the one that touches third-party input: it clones both
# upstreams, runs the importers over them and builds the site from the result.
# It gets a read-only token that is not left on disk, so nothing it reads or
# runs can push, and a file copied out of the runner by a hostile upstream
# (the importers refuse symbolic links for exactly that reason) would find no
# credential to copy.
#
# `publish` holds the write token and runs none of that. It receives the sync
# commits as a git bundle, checks that they change the mirror trees and
# nothing else, and pushes.
jobs:
  convert:
    runs-on: ubuntu-latest
    permissions:
      contents: read
    outputs:
      changed: ${{ steps.sync.outputs.changed }}
    steps:
      - name: Checkout site
        uses: actions/checkout@v4
        with:
          persist-credentials: false

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: npm

      # Everything the sync does lives in the script, so running it locally
      # and running it here are the same thing. It clones both upstreams,
      # regenerates the mirrors, refuses to go on if anything fails
      # `npm run build` or `node --test`, and commits one commit per upstream
      # for whichever trees actually moved. The commits stay local to this job.
      - name: Sync, validate and commit
        id: sync
        run: scripts/sync-mirrors.sh --commit

      - name: Bundle the sync commits
        if: steps.sync.outputs.changed == 'true'
        run: git bundle create "$RUNNER_TEMP/sync.bundle" "${GITHUB_SHA}..HEAD"

      - name: Hand the bundle to the publish job
        if: steps.sync.outputs.changed == 'true'
        uses: actions/upload-artifact@v4
        with:
          name: sync-bundle
          path: ${{ runner.temp }}/sync.bundle
          if-no-files-found: error
          retention-days: 1

  publish:
    needs: convert
    if: needs.convert.outputs.changed == 'true'
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout site
        uses: actions/checkout@v4
        with:
          # The commit the bundle was built on, with full history so the push
          # can rebase if main moved while the build was running.
          ref: ${{ github.sha }}
          fetch-depth: 0

      - name: Fetch the bundle
        uses: actions/download-artifact@v4
        with:
          name: sync-bundle
          path: ${{ runner.temp }}

      - name: Check and push
        run: |
          set -euo pipefail
          base=$(git rev-parse HEAD)
          git bundle verify "$RUNNER_TEMP/sync.bundle"
          git fetch --no-tags "$RUNNER_TEMP/sync.bundle" HEAD
          git merge-base --is-ancestor "$base" FETCH_HEAD

          # The sync may change the mirrors and nothing else. Anything outside
          # them (a workflow, a script, a page template) is not a sync.
          outside=$(git diff --name-only "$base" FETCH_HEAD -- . \
            ':(exclude)src/content/payloads' \
            ':(exclude)src/content/internal' \
            ':(exclude)vendor/PayloadsAllTheThings' \
            ':(exclude)payloads-manifest.json' \
            ':(exclude)internal-manifest.json')
          if [ -n "$outside" ]; then
            echo 'The sync commits touch files outside the mirrors; refusing to push:' >&2
            echo "$outside" >&2
            exit 1
          fi

          git config user.name  'github-actions[bot]'
          git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
          git merge --ff-only FETCH_HEAD
          # Retry once through a rebase in case main moved while the build was running.
          git push origin HEAD:main || {
            git fetch origin main
            git rebase origin/main
            git push origin HEAD:main
          }

      # No deploy step: Cloudflare Workers is the only host and it deploys
      # manually via `npm run deploy:workers`. A sync lands commits on main and
      # nothing more, so synced content goes live at the next manual deploy.