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

Security fix for self-comment-ci.yml #35548

Merged
merged 6 commits into from
Jan 20, 2025
Merged

Conversation

ydshieh
Copy link
Collaborator

@ydshieh ydshieh commented Jan 7, 2025

No description provided.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@ydshieh ydshieh marked this pull request as ready for review January 7, 2025 17:04
id: get_sha
env:
PR_NUMBER: ${{ needs.get-pr-number.outputs.PR_NUMBER }}
COMMENT_DATE: ${{ github.event.comment.created_at }}
Copy link
Collaborator Author

@ydshieh ydshieh Jan 7, 2025

Choose a reason for hiding this comment

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

COMMENT_DATE: ${{ github.event.comment.created_at }}

new for security

Copy link
Collaborator

Choose a reason for hiding this comment

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

ha got it !

Comment on lines +62 to +72
git fetch origin refs/pull/$PR_NUMBER/merge:refs/remotes/pull/$PR_NUMBER/merge
git checkout refs/remotes/pull/$PR_NUMBER/merge
PR_MERGE_COMMIT_TIMESTAMP=$(git log -1 --date=unix --format=%cd)
echo "PR_MERGE_COMMIT_TIMESTAMP: $PR_MERGE_COMMIT_TIMESTAMP"
COMMENT_TIMESTAMP=$(date -d "${COMMENT_DATE}" +"%s")
echo "PR_HEAD_SHA: $COMMENT_DATE"
echo "COMMENT_TIMESTAMP: $COMMENT_TIMESTAMP"
if [ $COMMENT_TIMESTAMP -le $PR_MERGE_COMMIT_TIMESTAMP ]; then
echo "Last commit on the pull request is newer than the issue comment triggering this run! Abort!";
exit -1;
fi
Copy link
Collaborator Author

@ydshieh ydshieh Jan 7, 2025

Choose a reason for hiding this comment

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

new for security

from the line git fetch origin refs/pull/$PR_NUMBER/merge:refs/remotes/pull/$PR_NUMBER/merge to here

Copy link
Collaborator Author

@ydshieh ydshieh Jan 7, 2025

Choose a reason for hiding this comment

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

Currently I am using the merge commit.

(merge commit = PR head commit merged with main )

Merge commit might change if there is a push or merge to the main branch. So it would be better to use the head commit of the PR instead.

Copy link
Member

@qubvel qubvel Jan 13, 2025

Choose a reason for hiding this comment

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

btw, I am not sure $COMMENT_TIMESTAMP -le $PR_MERGE_COMMIT_TIMESTAMP check 100% safe, the commit date might be manually specified to be in the past 🥲

Copy link
Member

Choose a reason for hiding this comment

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

however, this approach with comment message should be safer than triggering with contributor's commit message, because we control it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hi @qubvel This PR doesn't use users' input for date calculation. The merge commit refs/remotes/pull/$PR_NUMBER/merge is prepared by github itself.

But even if it's the date from the user (i.e. the actual commit from the contributor), would that date be manipulated?

Copy link
Member

@qubvel qubvel Jan 13, 2025

Choose a reason for hiding this comment

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

This PR doesn't use users' input for date calculation. The merge commit refs/remotes/pull/$PR_NUMBER/merge is prepared by github itself.

Looks good then, thanks for clarification!

But even if it's the date from the user (i.e. the actual commit from the contributor), would that date be manipulated?

I hope no! But security issue was an initial concern to disable the feature, wasn't it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I hope no! But security issue was an initial concern to disable the feature, wasn't it?

Yes. At that time, there is no check on date at all. But now the concern is about if the commit date could be manipulated.
Anyway, if we use merge commit as mentioned above, it's fine :-)

Copy link
Member

Choose a reason for hiding this comment

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

Hmm... if merge commit is created when CI is started wouldn't it be always later than the comment?

-> user commit
-> message to trigger CI (COMMENT_TIMESTAMP)
-> CI triggered
-> merge commit created (PR_MERGE_COMMIT_TIMESTAMP)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry, I should be more clear. The merge commit is prepared by github when a commit is pushed to the PR branch or the target branch of the PR (usually main) is updated.

So the flow is

-> user commit (pushed)
-> merge commit created (PR_MERGE_COMMIT_TIMESTAMP)
-> message to trigger CI (COMMENT_TIMESTAMP)
-> CI triggered

Copy link
Member

Choose a reason for hiding this comment

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

Ok, sounds good then!

@ydshieh ydshieh requested a review from ArthurZucker January 7, 2025 17:09
@ydshieh
Copy link
Collaborator Author

ydshieh commented Jan 7, 2025

a failing run looks like this

Copy link
Contributor

@glegendre01 glegendre01 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Sorry for the late review! Merging!

id: get_sha
env:
PR_NUMBER: ${{ needs.get-pr-number.outputs.PR_NUMBER }}
COMMENT_DATE: ${{ github.event.comment.created_at }}
Copy link
Collaborator

Choose a reason for hiding this comment

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

ha got it !

@ydshieh ydshieh merged commit 332fa02 into main Jan 20, 2025
11 checks passed
@ydshieh ydshieh deleted the revert-35366-disable_for_now branch January 20, 2025 10:16
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