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
13 changes: 12 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ jobs:
steps:
- name: Check out
uses: actions/checkout@v6
with:
persist-credentials: false
- name: Verify duplicated file names
run: ./scripts/verify-duplicated-file-name.sh
- name: Install Python test dependencies
run: python3 -m pip install --disable-pip-version-check --no-deps PyYAML==6.0.3
- name: Verify safer defaults
run: python3 scripts/test-safer-defaults.py
- name: Verify media workflow
run: |
python3 -m pip install --disable-pip-version-check --no-deps PyYAML==6.0.3
python3 scripts/test-media-workflow.py
python3 scripts/test-privileged-workflow-actors.py

Expand All @@ -30,6 +33,8 @@ jobs:
steps:
- name: Check out
uses: actions/checkout@v6
with:
persist-credentials: false
- uses: actions/setup-node@v6
with:
node-version: "18"
Expand All @@ -45,6 +50,8 @@ jobs:
steps:
- name: Check out
uses: actions/checkout@v6
with:
persist-credentials: false
- uses: actions/setup-node@v6
with:
node-version: "18"
Expand All @@ -60,6 +67,8 @@ jobs:
steps:
- name: Check out
uses: actions/checkout@v6
with:
persist-credentials: false
- uses: actions/setup-node@v6
with:
node-version: "18"
Expand All @@ -75,6 +84,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v6
with:
persist-credentials: false
- name: Vale Linter
uses: errata-ai/vale-action@518a9136acc6e6668ce7c00d367051e0941e87ff # reviewdog
with:
Expand Down
102 changes: 102 additions & 0 deletions scripts/test-safer-defaults.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
"""Regression checks for repository and documentation defaults."""

import ast
import os
from pathlib import Path
import re
import shutil
import subprocess
import tempfile

import yaml


ROOT = Path(__file__).resolve().parents[1]


def check_ci_security(content):
workflow = yaml.safe_load(content)
# PyYAML treats the unquoted "on" key as True under YAML 1.1.
trigger = workflow.get("on", workflow.get(True))
assert trigger == ["pull_request"], trigger
assert workflow.get("permissions") == {"contents": "read"}
return workflow


ci_workflow = (ROOT / ".github/workflows/ci.yaml").read_text()
workflow = check_ci_security(ci_workflow)

# Comments must not hide changes to the active trigger or permissions.
for unsafe_workflow in (
ci_workflow.replace(
"on: [pull_request]",
"# on: [pull_request]\non: [pull_request_target]",
1,
),
ci_workflow.replace("contents: read", "contents: write", 1),
):
try:
check_ci_security(unsafe_workflow)
except AssertionError:
pass
else:
raise AssertionError("Unsafe CI configuration was accepted")

checkout_steps = [
step
for job in workflow["jobs"].values()
for step in job.get("steps", [])
if step.get("uses", "").startswith("actions/checkout@")
]
assert checkout_steps
assert all(
step.get("with", {}).get("persist-credentials") is False
for step in checkout_steps
)

install_commands = [
line.split("#", 1)[0]
for line in ci_workflow.splitlines()
Expand All @@ -21,6 +66,63 @@
)
assert all("--include=dev" in command for command in install_commands)


# Exercise tool selection in an isolated fixture, without running npm or network calls.
with tempfile.TemporaryDirectory(prefix="docs-tool-check-") as directory:
fixture = Path(directory) / "repo with spaces"
scripts = fixture / "scripts"
scripts.mkdir(parents=True)
local_bin = fixture / "node_modules/.bin"
local_bin.mkdir(parents=True)
(fixture / "test.md").write_text("# Test\n")
(scripts / "markdown-link-check.tpl").write_text("{}\n")

global_bin = Path(directory) / "bin"
global_bin.mkdir()
forbidden_marker = Path(directory) / "global-tool-ran"
local_marker = Path(directory) / "local-tool-ran"
env = dict(os.environ, PATH=f"{global_bin}{os.pathsep}{os.environ['PATH']}")
env["FORBIDDEN_TOOL_MARKER"] = str(forbidden_marker)
env["LOCAL_TOOL_MARKER"] = str(local_marker)
for binary in ("markdown-link-check", "remark", "npx"):
tool = global_bin / binary
tool.write_text('#!/bin/sh\nprintf ran > "$FORBIDDEN_TOOL_MARKER"\nexit 99\n')
tool.chmod(0o755)

for filename, binary in (
("verify-links.sh", "markdown-link-check"),
("verify-link-anchors.sh", "remark"),
):
script = scripts / filename
shutil.copyfile(ROOT / "scripts" / filename, script)
tool = local_bin / binary

def run_check():
return subprocess.run(
["bash", str(script)], cwd=directory, env=env,
capture_output=True, text=True, timeout=30,
)

# Neither a missing nor a non-executable local tool may trigger a fallback.
for state in ("missing", "not executable"):
if state == "not executable":
tool.write_text('#!/bin/sh\nexit 0\n')
tool.chmod(0o644)
result = run_check()
assert result.returncode != 0, (filename, state, result)
assert "npm ci --ignore-scripts --include=dev" in result.stderr
assert not forbidden_marker.exists(), (filename, state)

tool.write_text('#!/bin/sh\nprintf "%s\\n" "$@" > "$LOCAL_TOOL_MARKER"\n')
tool.chmod(0o755)
result = run_check()
assert result.returncode == 0, (filename, result)
assert local_marker.exists(), filename
arguments = local_marker.read_text().splitlines()
assert ("./test.md" if binary == "markdown-link-check" else "--frail") in arguments
assert not forbidden_marker.exists(), filename
local_marker.unlink()

deployment = (ROOT / "check-before-deployment.md").read_text()
permission_commands = [
line.strip()
Expand Down
20 changes: 8 additions & 12 deletions scripts/verify-link-anchors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,17 @@
#
# In addition to verify-links.sh, this script additionally check anchors.
#
# See https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally if you meet permission problems when executing npm install.
# Install the pinned local tools with `npm ci --ignore-scripts --include=dev`.

ROOT=$(unset CDPATH && cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)
cd $ROOT
ROOT=$(unset CDPATH && cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
cd "$ROOT" || exit 1

REMARK_CMD=()
if [ -x "$ROOT/node_modules/.bin/remark" ]; then
# Prefer the repo-local pinned version (installed by `npm ci`).
REMARK_CMD=("$ROOT/node_modules/.bin/remark")
elif command -v remark >/dev/null 2>&1; then
# Fall back to a globally-installed version (less reproducible).
REMARK_CMD=(remark)
else
REMARK_CMD=(npx --no-install remark)
# Require the repo-local version installed from package-lock.json.
if [ ! -x "$ROOT/node_modules/.bin/remark" ]; then
echo "error: local remark is missing or not executable. Run npm ci --ignore-scripts --include=dev." >&2
exit 1
fi
REMARK_CMD=("$ROOT/node_modules/.bin/remark")

echo "info: checking links anchors under $ROOT directory..."

Expand Down
21 changes: 8 additions & 13 deletions scripts/verify-links.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,17 @@
# - When a file was moved, all other references are required to be updated for now, even if alias are given
# - This is recommended because of less redirects and better anchors support.
#
# See https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally if you meet permission problems when executing npm install.
# Install the pinned local tools with `npm ci --ignore-scripts --include=dev`.

ROOT=$(unset CDPATH && cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)
cd $ROOT
ROOT=$(unset CDPATH && cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
cd "$ROOT" || exit 1

MLC_CMD=()
if [ -x "$ROOT/node_modules/.bin/markdown-link-check" ]; then
# Prefer the repo-local pinned version (installed by `npm ci`).
MLC_CMD=("$ROOT/node_modules/.bin/markdown-link-check")
elif command -v markdown-link-check >/dev/null 2>&1; then
# Fall back to a globally-installed version (less reproducible).
MLC_CMD=(markdown-link-check)
else
# As a last resort, rely on `npx` without installing.
MLC_CMD=(npx --no-install markdown-link-check)
# Require the repo-local version installed from package-lock.json.
if [ ! -x "$ROOT/node_modules/.bin/markdown-link-check" ]; then
echo "error: local markdown-link-check is missing or not executable. Run npm ci --ignore-scripts --include=dev." >&2
exit 1
fi
MLC_CMD=("$ROOT/node_modules/.bin/markdown-link-check")

VERBOSE=${VERBOSE:-}
CONFIG_TMP=$(mktemp)
Expand Down
Loading