Skip to content

Commit d86dfac

Browse files
Making the bot more secure by using the pull_request_target event. (#2133)
* Making the bot more secure by using the pull_request_target event. Co-authored-by: Sean Morgan <seanmorgan@outlook.com>
1 parent d19c3ef commit d86dfac

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

.github/workflows/ci_test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ jobs:
9494
python-version: 3.7
9595
- run: pip install pygithub click
9696
- name: Check that the CODEOWNERS is valid
97-
run: python .github/workflows/notify_codeowners.py .github/CODEOWNERS
97+
env:
98+
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
99+
run: python .github/workflows/notify_codeowners.py
98100
nbfmt:
99101
name: Notebook format
100102
runs-on: ubuntu-latest

.github/workflows/notify_codeowners.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import github
22
import click
3-
import urllib.request
3+
44
from pathlib import Path
55
from typing import List, Tuple
66
import re
@@ -23,22 +23,18 @@
2323
]
2424

2525

26-
def xor_strings(a, b):
27-
result = int(a, 16) ^ int(b, 16)
28-
return "{:x}".format(result)
29-
30-
3126
def get_github_client():
32-
bot_token = "1353d990cdb8b8ceb1b73d301dce83cc0da3db29"
33-
bot_token_key = "a1b2c3d47311f8e29e204f85a81b4df4a44e252c"
34-
35-
return github.Github(xor_strings(bot_token, bot_token_key))
27+
return github.Github(os.environ["BOT_TOKEN"])
3628

3729

3830
CLIENT = get_github_client()
3931

32+
# faster checks
33+
valid_users_cache = set()
34+
4035

4136
def check_user(user: str, line_idx: int):
37+
print(f"Checking that {user} actually exists")
4238
if user[0] != "@":
4339
raise ValueError(
4440
f"User '{user}' at line {line_idx} of CODEOWNERS "
@@ -49,7 +45,10 @@ def check_user(user: str, line_idx: int):
4945
if user in WRITE_ACCESS_LIST:
5046
return None
5147
try:
48+
if user in valid_users_cache:
49+
return user
5250
CLIENT.get_user(user)
51+
valid_users_cache.add(user)
5352
except github.UnknownObjectException:
5453
raise KeyError(
5554
f"User '{user}' line {line_idx} does not exist. Did you make a typo?"
@@ -158,12 +157,8 @@ def get_pull_request_id_from_gh_actions():
158157
@click.command()
159158
@click.option("--pull-request-id")
160159
@click.option("--no-dry-run", is_flag=True)
161-
@click.argument("file")
162-
def notify_codeowners(pull_request_id, no_dry_run, file):
163-
if file.startswith("http"):
164-
text = urllib.request.urlopen(file).read().decode("utf-8")
165-
else:
166-
text = Path(file).read_text()
160+
def notify_codeowners(pull_request_id, no_dry_run):
161+
text = (Path(__file__).parents[1] / "CODEOWNERS").read_text()
167162
codeowners = parse_codeowners(text)
168163

169164
if pull_request_id is not None:

.github/workflows/notify_codeowners.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: Notify codeowners
22

33
on:
4-
pull_request:
4+
pull_request_target:
55
types: [opened]
66

77

@@ -18,8 +18,8 @@ jobs:
1818
- name: Drop a message for codeowners
1919
env:
2020
PR: ${{ steps.findPr.outputs.pr }}
21+
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
2122
run: |
2223
python .github/workflows/notify_codeowners.py \
2324
--pull-request-id=auto \
24-
--no-dry-run \
25-
https://raw.githubusercontent.com/tensorflow/addons/master/.github/CODEOWNERS
25+
--no-dry-run

0 commit comments

Comments
 (0)