- 
                Notifications
    You must be signed in to change notification settings 
- Fork 837
A better security-wise style bot GH Action #2914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
5828734
              1831f66
              61c5cbb
              4d42d1b
              d81b168
              3fc12e3
              cfb6aae
              09d0b68
              b1568e7
              5dd2c6e
              af3bb09
              3b03d35
              1063565
              264bc0c
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -3,15 +3,15 @@ name: Style Bot Action | |
| on: | ||
| workflow_call: | ||
| inputs: | ||
| pre_commit_script: | ||
| style_command_type: | ||
| required: false | ||
| type: string | ||
| description: "Optional script to run before committing changes" | ||
| pre_commit_script_name: | ||
| description: "Which style command to run (options: 'default' (make style && make quality), 'quality_only', 'style_only')" | ||
| default: "default" | ||
| pr_number: | ||
| required: false | ||
| type: string | ||
| description: "Custom name for the pre-commit script step" | ||
| default: "Custom pre-commit script" | ||
| type: number | ||
| description: "Pull Request number to process (only used when running manually)" | ||
| python_quality_dependencies: | ||
| required: true | ||
| type: string | ||
|  | @@ -21,11 +21,6 @@ on: | |
| type: string | ||
| description: "Python version to run code formatter" | ||
| default: "3.10" | ||
| style_command: | ||
| required: false | ||
| type: string | ||
| description: "Command to run for style checks or/and style fixes" | ||
| default: "make style && make quality" | ||
| secrets: | ||
| bot_token: | ||
| required: true | ||
|  | @@ -34,8 +29,10 @@ on: | |
| jobs: | ||
| check-permissions: | ||
| if: > | ||
| contains(github.event.comment.body, '@bot /style') && | ||
| github.event.issue.pull_request != null | ||
| (github.event_name == 'issue_comment' && | ||
| contains(github.event.comment.body, '@bot /style') && | ||
| github.event.issue.pull_request != null) || | ||
| github.event_name == 'workflow_dispatch' | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| is_authorized: ${{ steps.check_user_permission.outputs.has_permission }} | ||
|  | @@ -45,7 +42,14 @@ jobs: | |
| uses: actions/github-script@v6 | ||
| with: | ||
| script: | | ||
| const comment_user = context.payload.comment.user.login; | ||
| let comment_user; | ||
| if (context.eventName === 'workflow_dispatch') { | ||
| comment_user = context.actor; | ||
| console.log('Workflow triggered manually by:', comment_user); | ||
| } else { | ||
| comment_user = context.payload.comment.user.login; | ||
| console.log('Workflow triggered by comment from:', comment_user); | ||
| } | ||
| const { data: permission } = await github.rest.repos.getCollaboratorPermissionLevel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
|  | @@ -65,18 +69,29 @@ jobs: | |
| uses: actions/github-script@v6 | ||
| with: | ||
| script: | | ||
| const prNumber = context.payload.issue.number; | ||
| const prNumber = context.eventName === 'workflow_dispatch' | ||
| ? context.payload.inputs.pr_number | ||
| : context.payload.issue.number; | ||
|  | ||
| // Get PR details | ||
| const { data: pr } = await github.rest.pulls.get({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: prNumber | ||
| }); | ||
|  | ||
| // We capture both the branch ref and the "full_name" of the head repo | ||
| // so that we can check out the correct repository & branch (including forks). | ||
| core.setOutput("prNumber", prNumber); | ||
| core.setOutput("headRef", pr.head.ref); | ||
| core.setOutput("headRepoFullName", pr.head.repo.full_name); | ||
| // Set outputs for use in subsequent steps | ||
| core.setOutput('headRepoFullName', pr.head.repo.full_name); | ||
| core.setOutput('headRef', pr.head.ref); | ||
| core.setOutput('baseRef', pr.base.ref); | ||
| core.setOutput('prNumber', prNumber); | ||
|  | ||
| console.log('PR Details:', { | ||
| number: prNumber, | ||
| headRepo: pr.head.repo.full_name, | ||
| headRef: pr.head.ref, | ||
| baseRef: pr.base.ref | ||
| }); | ||
|  | ||
| - name: Check out PR branch | ||
| uses: actions/checkout@v3 | ||
|  | @@ -101,6 +116,33 @@ jobs: | |
| echo "Head Ref: $HEADREF" | ||
| echo "Head Repo Full Name: $HEADREPOFULLNAME" | ||
|  | ||
| - name: Verify critical files haven't been modified | ||
| uses: actions/github-script@v6 | ||
| with: | ||
| script: | | ||
| const prNumber = context.eventName === 'workflow_dispatch' | ||
| ? context.payload.inputs.pr_number | ||
| : context.payload.issue.number; | ||
| const { data: pr } = await github.rest.pulls.listFiles({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| pull_number: prNumber | ||
| }); | ||
|  | ||
| const modifiedFiles = pr.map(file => file.filename); | ||
| console.log("Modified files:", modifiedFiles); | ||
|  | ||
| const protectedFiles = ["setup.py", "Makefile"]; | ||
|          | ||
| console.log("Protected files:", protectedFiles); | ||
|  | ||
| for (const file of protectedFiles) { | ||
| if (modifiedFiles.includes(file)) { | ||
| core.setFailed(`❌ Error: Protected file '${file}' has been modified in this PR. This is not allowed for security reasons.`); | ||
| return; | ||
| } | ||
| } | ||
|  | ||
| console.log("✅ All protected files check passed!"); | ||
| - name: Set up Python | ||
| uses: actions/setup-python@v4 | ||
| with: | ||
|  | @@ -113,18 +155,28 @@ jobs: | |
| python -m pip install --upgrade pip | ||
| pip install .$python_quality_dependencies | ||
|  | ||
| - name: ${{ inputs.pre_commit_script_name }} | ||
| env: | ||
| pre_commit_script: ${{ inputs.pre_commit_script }} | ||
| if: inputs.pre_commit_script != '' | ||
| run: | | ||
| bash -c "${pre_commit_script}" | ||
|  | ||
| - name: Run style command | ||
| env: | ||
| style_command: ${{ inputs.style_command }} | ||
| id: run_style | ||
| run: | | ||
| bash -c "$style_command" | ||
| case "${{ inputs.style_command_type }}" in | ||
| "default") | ||
| echo "Running default style and quality checks" | ||
| make style && make quality | ||
| ;; | ||
| "quality_only") | ||
| echo "Running quality checks only" | ||
| make quality | ||
| ;; | ||
| "style_only") | ||
| echo "Running style checks only" | ||
| make style | ||
| ;; | ||
| *) | ||
| echo "Invalid style_command_type: ${{ inputs.style_command_type }}" | ||
| echo "Valid options are: 'default', 'quality_only', 'style_only'" | ||
| exit 1 | ||
| ;; | ||
| esac | ||
|  | ||
| - name: Commit and push changes | ||
| id: commit_and_push | ||
|  | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this only be reserved for users having admin privileges?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes it's already checked here :
huggingface_hub/.github/workflows/style-bot-action.yml
Line 65 in 3fc12e3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(both for workflow_dispatch and issue comment)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, I am not sure about the details, but I kind feel
adminis not guaranteed to every maintainer if we are talking about HF repositories.For example, for
transformers, I thinkadminis not guaranteed to say @molbap @qubvel etc. I amadminbecause I need to accesstransformers repository setting page.It might be a good idea to double check here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If my statement above is correct, what I did so far is a lazy approach by a hard coded list of user name .... (but that is not good neither for security as people may leave the team someday)
https://github.com/huggingface/transformers/blob/2c2495cc7b0e3e2942a9310f61548f40a2bc8425/.github/workflows/self-comment-ci.yml#L32