diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 00000000..2ff6eea2 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @motif-ui/core-team \ No newline at end of file diff --git a/.github/workflows/auto_assign.yml b/.github/workflows/auto-assign.yml similarity index 100% rename from .github/workflows/auto_assign.yml rename to .github/workflows/auto-assign.yml diff --git a/.github/workflows/auto_label.yml b/.github/workflows/auto-label.yml similarity index 100% rename from .github/workflows/auto_label.yml rename to .github/workflows/auto-label.yml diff --git a/.github/workflows/ui.yml b/.github/workflows/chromatic-ui.yml similarity index 100% rename from .github/workflows/ui.yml rename to .github/workflows/chromatic-ui.yml diff --git a/.github/workflows/pr-validator.yml b/.github/workflows/pr-validator.yml index 19cdeaf3..b5f45fc1 100644 --- a/.github/workflows/pr-validator.yml +++ b/.github/workflows/pr-validator.yml @@ -1,7 +1,7 @@ -name: "PR Validator" +name: PR Validator on: - pull_request_target: + pull_request: types: [opened, edited, reopened, synchronize] concurrency: @@ -31,20 +31,7 @@ jobs: with: pr-author: ${{ github.event.pull_request.user.login }} - # 2. CHECK FOR WORKFLOW CHANGES - - name: Get changed workflow files - id: changed-files - uses: tj-actions/changed-files@v44 - with: - files: .github/workflows/** - - - name: Security — Block unauthorized workflow changes - if: steps.changed-files.outputs.any_changed == 'true' && steps.team-check.outputs.is-internal != 'true' - run: | - echo "❌ Access Denied: Only core team members can modify workflow files." - exit 1 - - # 3. LINT PR TITLE + # 2. LINT PR TITLE - name: Validate PR title uses: amannn/action-semantic-pull-request@v5 env: @@ -64,7 +51,7 @@ jobs: revert requireScope: false - # 4. SYNC RELEASE LABELS + # 3. SYNC RELEASE LABELS - name: Sync release labels uses: actions/github-script@v7 with: @@ -103,7 +90,7 @@ jobs: }); } - # 5. CHECK INTERNAL ISSUE REFERENCE + # 4. CHECK INTERNAL ISSUE REFERENCE - name: Check internal issue reference if: steps.team-check.outputs.is-internal == 'true' uses: actions/github-script@v7 diff --git a/.github/workflows/workflow-protection.yml b/.github/workflows/workflow-protection.yml new file mode 100644 index 00000000..e22aa2fe --- /dev/null +++ b/.github/workflows/workflow-protection.yml @@ -0,0 +1,71 @@ +name: "Workflow Protection" + +on: + pull_request_target: + types: [opened, synchronize, reopened] + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + +permissions: + contents: read + pull-requests: read + +jobs: + protect: + name: Detect Workflow Tampering + runs-on: ubuntu-latest + steps: + + - name: Check for unauthorized .github/ changes + uses: actions/github-script@v7 + with: + script: | + let page = 1; + let allFiles = []; + while (true) { + const { data: files } = await github.rest.pulls.listFiles({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + per_page: 100, + page, + }); + allFiles = allFiles.concat(files); + if (files.length < 100) break; + page++; + } + + const sensitiveFiles = allFiles.filter(f => + f.filename.startsWith('.github/workflows/') || + f.filename.startsWith('.github/actions/') + ); + + if (sensitiveFiles.length === 0) { + core.info('No workflow or action files changed. ✅'); + return; + } + + // 2. Check if the PR author is an authorized collaborator + let authorized = false; + try { + const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ + owner: context.repo.owner, + repo: context.repo.repo, + username: context.payload.pull_request.user.login, + }); + authorized = ['admin', 'maintain', 'write'].includes(data.role_name); + } catch { + authorized = false; + } + + if (!authorized) { + const changed = sensitiveFiles.map(f => ` - ${f.filename}`).join('\n'); + core.setFailed( + `❌ This PR modifies protected files and the author is not an authorized collaborator.\n` + + `Changed files:\n${changed}` + ); + } else { + core.info(`Authorized collaborator modifying .github/ files. ✅`); + }