Repository navigation
Expand file tree
/
Copy pathlocal.py
More file actions
253 lines (222 loc) · 8.06 KB
/
Copy pathlocal.py
File metadata and controls
253 lines (222 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
"""Offline scanning for cloned repositories and local source trees."""
import shlex
import subprocess
import time
from contextlib import nullcontext
from dataclasses import dataclass
from pathlib import Path
from github_dorks.dictionaries import iter_dorks, resolve_dictionaries
from github_dorks.output import create_writer
LANGUAGE_EXTENSIONS = {
'bash': {'.bash', '.sh'},
'json': {'.json'},
'php': {'.php'},
'python': {'.py'},
'ruby': {'.rb'},
'shell': {'.bash', '.fish', '.ksh', '.sh', '.zsh'},
'yaml': {'.yaml', '.yml'},
}
QUALIFIERS = {'extension', 'filename', 'language', 'path'}
@dataclass
class LocalScanStats:
queries: int = 0
matches: int = 0
files: int = 0
failures: int = 0
retries: int = 0
elapsed_seconds: float = 0.0
@property
def exit_code(self):
return 2 if self.failures else 0
@dataclass(frozen=True)
class Predicate:
kind: str
value: str
def matches(self, path, relative_path, normalized_text):
value = self.value.casefold()
if self.kind == 'filename':
return value in path.name.casefold()
if self.kind == 'extension':
return path.suffix.casefold().lstrip('.') == value.lstrip('.')
if self.kind == 'path':
return value in relative_path.casefold()
if self.kind == 'language':
return path.suffix.casefold() in LANGUAGE_EXTENSIONS.get(value, set())
return value in normalized_text
@dataclass(frozen=True)
class LocalQuery:
source: str
clauses: tuple
exclusions: tuple
def matches(self, path, relative_path, normalized_text):
if any(
predicate.matches(path, relative_path, normalized_text)
for predicate in self.exclusions
):
return False
return all(
any(
predicate.matches(path, relative_path, normalized_text)
for predicate in clause
)
for clause in self.clauses
)
def snippets(self, text, limit=5):
needles = {
predicate.value.casefold()
for clause in self.clauses
for predicate in clause
if predicate.kind == 'content'
}
if not needles:
return []
matches = []
for number, line in enumerate(text.splitlines(), 1):
if any(needle in line.casefold() for needle in needles):
matches.append(f'{number}: {line.strip()}'[:300])
if len(matches) == limit:
break
return matches
def parse_query(query):
"""Parse the useful local subset of GitHub code-search syntax."""
tokens = shlex.split(query)
clauses = []
exclusions = []
join_previous = False
negate_next = False
for token in tokens:
operator = token.upper()
if operator == 'OR':
join_previous = True
continue
if operator == 'NOT':
negate_next = True
continue
name, separator, value = token.partition(':')
predicate = Predicate(
name.casefold() if separator and name.casefold() in QUALIFIERS
else 'content',
value if separator and name.casefold() in QUALIFIERS else token,
)
if negate_next:
exclusions.append(predicate)
negate_next = False
join_previous = False
elif join_previous and clauses:
clauses[-1].append(predicate)
join_previous = False
else:
clauses.append([predicate])
if negate_next or join_previous:
raise ValueError(f'incomplete operator in dork: {query}')
return LocalQuery(
source=query,
clauses=tuple(tuple(clause) for clause in clauses),
exclusions=tuple(exclusions),
)
def _git_files(root):
try:
result = subprocess.run(
[
'git', '-C', str(root), 'ls-files', '-z', '--cached', '--others',
'--exclude-standard',
],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
check=False,
)
except OSError:
return None
if result.returncode:
return None
return [
root / name
for name in result.stdout.decode(errors='surrogateescape').split('\0')
if name
]
def iter_local_files(target):
"""Yield Git-tracked and non-ignored files, with a filesystem fallback."""
target = Path(target).resolve()
if target.is_file():
yield target, target.name
return
if not target.is_dir():
raise ValueError(f'local scan path is not a file or directory: {target}')
paths = _git_files(target)
if paths is None:
paths = (
path for path in target.rglob('*')
if '.git' not in path.parts
)
for path in paths:
if path.is_file() and not path.is_symlink():
yield path, path.relative_to(target).as_posix()
def _read_text(path, max_file_size):
if path.stat().st_size > max_file_size:
return None
contents = path.read_bytes()
if b'\0' in contents:
return None
return contents.decode('utf-8', errors='replace')
def scan_local(local_path, gh_dorks_file=None, categories=None,
output_filename=None, output_format=None, force=False,
quiet=False, verbose=False, max_file_size=1_000_000,
monotonic=time.monotonic, stdout=None, stderr=None):
"""Scan a local working tree with the bundled or custom dorks."""
import sys
if max_file_size <= 0:
raise ValueError('maximum file size must be greater than zero')
stdout = sys.stdout if stdout is None else stdout
stderr = sys.stderr if stderr is None else stderr
root = Path(local_path).resolve()
excluded_output = Path(output_filename).resolve() if output_filename else None
sources = resolve_dictionaries(gh_dorks_file, categories)
queries = [parse_query(dork) for dork in iter_dorks(sources)]
output_format = output_format or ('csv' if output_filename else 'text')
stats = LocalScanStats(queries=len(queries))
started_at = monotonic()
status_stream = stderr if not output_filename and output_format != 'text' else stdout
if not quiet:
status_stream.write(f'Scanning local path: {root}\n')
output_context = (
open(output_filename, 'w' if force else 'x', newline='', encoding='utf-8')
if output_filename else nullcontext(stdout)
)
with output_context as output_file:
writer = create_writer(output_format, output_file)
writer.start()
try:
for path, relative_path in iter_local_files(root):
if excluded_output and path.resolve() == excluded_output:
continue
try:
text = _read_text(path, max_file_size)
except OSError as error:
if verbose and not quiet:
status_stream.write(f'Skipping {relative_path}: {error}\n')
continue
if text is None:
continue
stats.files += 1
normalized_text = text.casefold()
for query in queries:
if query.matches(path, relative_path, normalized_text):
stats.matches += 1
writer.write({
'dork': query.source,
'text_matches': query.snippets(text),
'path': relative_path,
'score': len(query.clauses),
'url': path.as_uri(),
})
finally:
stats.elapsed_seconds = monotonic() - started_at
writer.finish(stats)
if not quiet:
if not stats.matches:
status_stream.write('No local matches found. Hurray!\n')
status_stream.write(
f'Summary: {stats.queries} queries, {stats.matches} matches, '
f'{stats.files} files, {stats.elapsed_seconds:.1f}s elapsed\n'
)
return stats