Skip to content

Commit 0c49019

Browse files
committed
fixes #5 - handle 403 forbidden and wait when api rate limit is hit
1 parent 382de23 commit 0c49019

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ GH_TOKEN=<github_token> python github-dork.py -u dev-nepal # search
3535

3636
#### Limitations
3737

38-
- Authenticated requests get a higher rate limit. But, you can still hit limit with user/org with too many repos or even with large repos or large number of dorks. This is a major limitation, imo, at the moment for this tool.
38+
- Authenticated requests get a higher rate limit. But, since this tool waits for the api rate limit to be reset (which is usually less than a minute), it can be slightly slow.
3939
- Output formatting is not great. PR welcome
40-
- Handle rate limit and retry. PR welcome
40+
- ~~Handle rate limit and retry. PR welcome~~
4141

4242
### Contribution
43-
Please consider contributing the dorks that can reveal potentially senstive information in github.
43+
Please consider contributing the dorks that can reveal potentially sensitive information in github.
4444

4545
### List of Dorks
4646
I am not categorizing at the moment. Instead I am going to just the list of dorks with a description. Many of the dorks can be modified to make the search more specific or generic. You can see more options [here](https://github.com/search#search_cheatsheet_pane).

github-dork.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import github3 as github
66
import os
77
import argparse
8+
import time
9+
from copy import copy
10+
from sys import stderr
811

912

1013
gh_user = os.getenv('GH_USER', None)
@@ -13,6 +16,24 @@
1316

1417
gh = github.GitHub(username=gh_user, password=gh_pass, token=gh_token)
1518

19+
def search_wrapper(gen):
20+
while True:
21+
gen_back = copy(gen)
22+
try:
23+
yield next(gen)
24+
except StopIteration:
25+
raise
26+
except github.exceptions.ForbiddenError as e:
27+
search_rate_limit = gh.rate_limit()['resources']['search']
28+
limit_remaining = search_rate_limit['remaining']
29+
reset_time = search_rate_limit['reset']
30+
current_time = int(time.time())
31+
sleep_time = reset_time - current_time + 1
32+
stderr.write('GitHub Search API rate limit reached. Sleeping for %d seconds.\n\n' %(sleep_time))
33+
time.sleep(sleep_time)
34+
yield next(gen_back)
35+
except Exception as e:
36+
raise e
1637

1738
def search(repo_to_search=None, user_to_search=None, gh_dorks_file=None):
1839
if gh_dorks_file is None:
@@ -27,13 +48,13 @@ def search(repo_to_search=None, user_to_search=None, gh_dorks_file=None):
2748
if not dork or dork[0] in '#;':
2849
continue
2950
addendum = ''
30-
if repo_to_search is not None:
51+
if repo_to_search:
3152
addendum = ' repo:' + repo_to_search
32-
elif user_to_search is not None:
53+
elif user_to_search:
3354
addendum = ' user:' + user_to_search
3455

3556
dork = dork + addendum
36-
search_results = gh.search_code(dork)
57+
search_results = search_wrapper(gh.search_code(dork))
3758
try:
3859
for search_result in search_results:
3960
found = True
@@ -53,16 +74,12 @@ def search(repo_to_search=None, user_to_search=None, gh_dorks_file=None):
5374
''
5475
]).format(**fmt_args)
5576
print(result)
56-
except github.exceptions.ForbiddenError as e:
57-
print(e)
58-
return
59-
# need to retry in case of API rate limit reached
60-
# not done yet
6177
except github.exceptions.GitHubError as e:
6278
print('GitHubError encountered on search of dork: ' + dork)
6379
print(e)
6480
return
6581
except Exception as e:
82+
print(e)
6683
print('Error encountered on search of dork: ' + dork)
6784

6885
if not found:

0 commit comments

Comments
 (0)