Skip to content
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

Improve security of our GitHub Actions #18413

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Improve security of our GitHub Actions #18413

wants to merge 13 commits into from

Conversation

sobolevn
Copy link
Member

@sobolevn sobolevn commented Jan 2, 2025

Recently CPython introduced this new tool: https://github.com/python/cpython/blob/8eebe4e6d02bb4ad3f1ca6c52624186903dce893/.pre-commit-config.yaml#L64-L67

Which finds different security related problems with GitHub Actions.

I added this tool to our .pre-commit-config.yaml and followed all its recommendations.

Changes:

@sobolevn
Copy link
Member Author

sobolevn commented Jan 2, 2025

CC @hugovk

.github/workflows/sync_typeshed.yml Outdated Show resolved Hide resolved
@sobolevn sobolevn requested a review from AlexWaygood January 3, 2025 08:36

This comment has been minimized.

1 similar comment

This comment has been minimized.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this overall looks great! I'm not familiar with the workflow_call/workflow_run distinction, though, so I haven't looked closely at that. (It looks reasonable, though.)

Another change you might want to make is to list shellcheck as an additional_dependency of actionlint -- I made this change to Ruff's pre-commit config: https://github.com/astral-sh/ruff/blob/0837cdd9314cb9ee1df087142af975d492e3e7ba/.pre-commit-config.yaml#L103-L121. actionlint's shellcheck integration is very useful (it grabs the shell-script strings in GitHub Actions run: steps and passes them to shellcheck), but it's not enabled by default when actionlint is run as part of pre-commit, as actionlint's shellcheck integration only works if shellcheck is already installed.

.github/workflows/build_wheels.yml Outdated Show resolved Hide resolved
name: Call mypy_primer_comment workflow
needs: [join_artifacts]
permissions:
contents: read
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this the default?

Suggested change
contents: read

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, TIL, sorry!!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexWaygood whenever permissions: is declared, it does not augment the global definition but replaces it entirely, FYI.

.github/workflows/sync_typeshed.yml Outdated Show resolved Hide resolved

This comment has been minimized.

1 similar comment

This comment has been minimized.

@@ -1,11 +1,11 @@
name: Comment with mypy_primer diff
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, perhaps make it more obvious that it's not going to be triggered anymore:

Suggested change
name: Comment with mypy_primer diff
name: >-
[Reusable, do not click]
Comment with mypy_primer diff

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexWaygood after merging this, go to https://github.com/python/mypy/actions and disable this workflow. The inclusion from the other workflow will keep working, but it'll show up lower than the others in the sidebar, which is useful. I'm doing this in many places and got CPython to adopt the same technique which decluttered the sidebar a bit (also with pinning the most important workflows to force them to the top of that list).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a maintainer here, just a triager, so it's out of my power to do this :-) @sobolevn is a mypy maintainer, though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea, however, we only have 7 workflows at the moment, so I don't think that we need to do that now.

This comment has been minimized.

1 similar comment
Copy link
Contributor

github-actions bot commented Jan 3, 2025

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Comment on lines 3 to +8
on:
workflow_run:
workflows:
- Run mypy_primer
types:
- completed
workflow_call:
inputs:
workflow_id:
required: true
type: string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this workflow continue to work for pull requests from forks?

As I understand it, workflow_call is in the forked repo scope, thus I think pull-requests: write won't have access to the PR to leave a comment. Believe that's the reason workflow_run was used here in the first place as that's executed in the upstream repo scope.

Note: This PR is from an upstream branch, so it doesn't test what happens to PRs from forks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created #18414 to test how forks work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like that this is true:

Diff from mypy_primer:

RequestError [HttpError]: Resource not accessible by integration
    at /home/runner/work/_actions/actions/github-script/v7/dist/index.js:9537:21
Error: Unhandled error: HttpError: Resource not accessible by integration
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async eval (eval at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v7/dist/index.js:35424:16), <anonymous>:36:1)
    at async main (/home/runner/work/_actions/actions/github-script/v7/dist/index.js:35522:20) {
  status: 403,

@sobolevn sobolevn mentioned this pull request Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants