chiark / gitweb /
fix pylint unused-argument
[fdroidserver.git] / fdroidserver / scanner.py
1 #!/usr/bin/env python3
2 #
3 # scanner.py - part of the FDroid server tools
4 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU Affero General Public License for more details.
15 #
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 import os
20 import re
21 import traceback
22 from argparse import ArgumentParser
23 import logging
24
25 from . import common
26 from . import metadata
27 from .exception import BuildException, VCSException
28
29 config = None
30 options = None
31
32
33 def get_gradle_compile_commands(build):
34     compileCommands = ['compile', 'releaseCompile']
35     if build.gradle and build.gradle != ['yes']:
36         compileCommands += [flavor + 'Compile' for flavor in build.gradle]
37         compileCommands += [flavor + 'ReleaseCompile' for flavor in build.gradle]
38
39     return [re.compile(r'\s*' + c, re.IGNORECASE) for c in compileCommands]
40
41
42 def scan_source(build_dir, build):
43     """Scan the source code in the given directory (and all subdirectories)
44     and return the number of fatal problems encountered
45     """
46
47     count = 0
48
49     # Common known non-free blobs (always lower case):
50     usual_suspects = {
51         exp: re.compile(r'.*' + exp, re.IGNORECASE) for exp in [
52             r'flurryagent',
53             r'paypal.*mpl',
54             r'google.*analytics',
55             r'admob.*sdk.*android',
56             r'google.*ad.*view',
57             r'google.*admob',
58             r'google.*play.*services',
59             r'crittercism',
60             r'heyzap',
61             r'jpct.*ae',
62             r'youtube.*android.*player.*api',
63             r'bugsense',
64             r'crashlytics',
65             r'ouya.*sdk',
66             r'libspen23',
67             r'firebase',
68         ]
69     }
70
71     def suspects_found(s):
72         for n, r in usual_suspects.items():
73             if r.match(s):
74                 yield n
75
76     gradle_mavenrepo = re.compile(r'maven *{ *(url)? *[\'"]?([^ \'"]*)[\'"]?')
77
78     allowed_repos = [re.compile(r'^https?://' + re.escape(repo) + r'/*') for repo in [
79         'repo1.maven.org/maven2',  # mavenCentral()
80         'jcenter.bintray.com',     # jcenter()
81         'jitpack.io',
82         'repo.maven.apache.org/maven2',
83         'oss.jfrog.org/artifactory/oss-snapshot-local',
84         'oss.sonatype.org/content/repositories/snapshots',
85         'oss.sonatype.org/content/repositories/releases',
86         'oss.sonatype.org/content/groups/public',
87         'clojars.org/repo',  # Clojure free software libs
88         's3.amazonaws.com/repo.commonsware.com',  # CommonsWare
89         'plugins.gradle.org/m2',  # Gradle plugin repo
90         ]
91     ]
92
93     scanignore = common.getpaths_map(build_dir, build.scanignore)
94     scandelete = common.getpaths_map(build_dir, build.scandelete)
95
96     scanignore_worked = set()
97     scandelete_worked = set()
98
99     def toignore(fd):
100         for k, paths in scanignore.items():
101             for p in paths:
102                 if fd.startswith(p):
103                     scanignore_worked.add(k)
104                     return True
105         return False
106
107     def todelete(fd):
108         for k, paths in scandelete.items():
109             for p in paths:
110                 if fd.startswith(p):
111                     scandelete_worked.add(k)
112                     return True
113         return False
114
115     def ignoreproblem(what, fd):
116         logging.info('Ignoring %s at %s' % (what, fd))
117         return 0
118
119     def removeproblem(what, fd, fp):
120         logging.info('Removing %s at %s' % (what, fd))
121         os.remove(fp)
122         return 0
123
124     def warnproblem(what, fd):
125         if toignore(fd):
126             return
127         logging.warn('Found %s at %s' % (what, fd))
128
129     def handleproblem(what, fd, fp):
130         if toignore(fd):
131             return ignoreproblem(what, fd)
132         if todelete(fd):
133             return removeproblem(what, fd, fp)
134         logging.error('Found %s at %s' % (what, fd))
135         return 1
136
137     def is_executable(path):
138         return os.path.exists(path) and os.access(path, os.X_OK)
139
140     textchars = bytearray({7, 8, 9, 10, 12, 13, 27} | set(range(0x20, 0x100)) - {0x7f})
141
142     def is_binary(path):
143         d = None
144         with open(path, 'rb') as f:
145             d = f.read(1024)
146         return bool(d.translate(None, textchars))
147
148     # False positives patterns for files that are binary and executable.
149     safe_paths = [re.compile(r) for r in [
150         r".*/drawable[^/]*/.*\.png$",  # png drawables
151         r".*/mipmap[^/]*/.*\.png$",    # png mipmaps
152         ]
153     ]
154
155     def safe_path(path):
156         for sp in safe_paths:
157             if sp.match(path):
158                 return True
159         return False
160
161     gradle_compile_commands = get_gradle_compile_commands(build)
162
163     def is_used_by_gradle(line):
164         return any(command.match(line) for command in gradle_compile_commands)
165
166     # Iterate through all files in the source code
167     for r, d, f in os.walk(build_dir, topdown=True):
168
169         # It's topdown, so checking the basename is enough
170         for ignoredir in ('.hg', '.git', '.svn', '.bzr'):
171             if ignoredir in d:
172                 d.remove(ignoredir)
173
174         for curfile in f:
175
176             if curfile in ['.DS_Store']:
177                 continue
178
179             # Path (relative) to the file
180             fp = os.path.join(r, curfile)
181
182             if os.path.islink(fp):
183                 continue
184
185             fd = fp[len(build_dir) + 1:]
186             _, ext = common.get_extension(fd)
187
188             if ext == 'so':
189                 count += handleproblem('shared library', fd, fp)
190             elif ext == 'a':
191                 count += handleproblem('static library', fd, fp)
192             elif ext == 'class':
193                 count += handleproblem('Java compiled class', fd, fp)
194             elif ext == 'apk':
195                 removeproblem('APK file', fd, fp)
196
197             elif ext == 'jar':
198                 for name in suspects_found(curfile):
199                     count += handleproblem('usual supect \'%s\'' % name, fd, fp)
200                 warnproblem('JAR file', fd)
201
202             elif ext == 'java':
203                 if not os.path.isfile(fp):
204                     continue
205                 with open(fp, 'r', encoding='utf8', errors='replace') as f:
206                     for line in f:
207                         if 'DexClassLoader' in line:
208                             count += handleproblem('DexClassLoader', fd, fp)
209                             break
210
211             elif ext == 'gradle':
212                 if not os.path.isfile(fp):
213                     continue
214                 with open(fp, 'r', encoding='utf8', errors='replace') as f:
215                     lines = f.readlines()
216                 for i, line in enumerate(lines):
217                     if is_used_by_gradle(line):
218                         for name in suspects_found(line):
219                             count += handleproblem('usual supect \'%s\' at line %d' % (name, i + 1), fd, fp)
220                 noncomment_lines = [l for l in lines if not common.gradle_comment.match(l)]
221                 joined = re.sub(r'[\n\r\s]+', ' ', ' '.join(noncomment_lines))
222                 for m in gradle_mavenrepo.finditer(joined):
223                     url = m.group(2)
224                     if not any(r.match(url) for r in allowed_repos):
225                         count += handleproblem('unknown maven repo \'%s\'' % url, fd, fp)
226
227             elif ext in ['', 'bin', 'out', 'exe']:
228                 if is_binary(fp):
229                     count += handleproblem('binary', fd, fp)
230
231             elif is_executable(fp):
232                 if is_binary(fp) and not safe_path(fd):
233                     warnproblem('possible binary', fd)
234
235     for p in scanignore:
236         if p not in scanignore_worked:
237             logging.error('Unused scanignore path: %s' % p)
238             count += 1
239
240     for p in scandelete:
241         if p not in scandelete_worked:
242             logging.error('Unused scandelete path: %s' % p)
243             count += 1
244
245     return count
246
247
248 def main():
249
250     global config, options
251
252     # Parse command line...
253     parser = ArgumentParser(usage="%(prog)s [options] [APPID[:VERCODE] [APPID[:VERCODE] ...]]")
254     common.setup_global_opts(parser)
255     parser.add_argument("appid", nargs='*', help="app-id with optional versionCode in the form APPID[:VERCODE]")
256     metadata.add_metadata_arguments(parser)
257     options = parser.parse_args()
258     metadata.warnings_action = options.W
259
260     config = common.read_config(options)
261
262     # Read all app and srclib metadata
263     allapps = metadata.read_metadata()
264     apps = common.read_app_args(options.appid, allapps, True)
265
266     probcount = 0
267
268     build_dir = 'build'
269     if not os.path.isdir(build_dir):
270         logging.info("Creating build directory")
271         os.makedirs(build_dir)
272     srclib_dir = os.path.join(build_dir, 'srclib')
273     extlib_dir = os.path.join(build_dir, 'extlib')
274
275     for appid, app in apps.items():
276
277         if app.Disabled:
278             logging.info("Skipping %s: disabled" % appid)
279             continue
280         if not app.builds:
281             logging.info("Skipping %s: no builds specified" % appid)
282             continue
283
284         logging.info("Processing " + appid)
285
286         try:
287
288             if app.RepoType == 'srclib':
289                 build_dir = os.path.join('build', 'srclib', app.Repo)
290             else:
291                 build_dir = os.path.join('build', appid)
292
293             # Set up vcs interface and make sure we have the latest code...
294             vcs = common.getvcs(app.RepoType, app.Repo, build_dir)
295
296             for build in app.builds:
297
298                 if build.disable:
299                     logging.info("...skipping version %s - %s" % (
300                         build.versionName, build.get('disable', build.commit[1:])))
301                 else:
302                     logging.info("...scanning version " + build.versionName)
303
304                     # Prepare the source code...
305                     common.prepare_source(vcs, app, build,
306                                           build_dir, srclib_dir,
307                                           extlib_dir, False)
308
309                     # Do the scan...
310                     count = scan_source(build_dir, build)
311                     if count > 0:
312                         logging.warn('Scanner found %d problems in %s (%s)' % (
313                             count, appid, build.versionCode))
314                         probcount += count
315
316         except BuildException as be:
317             logging.warn("Could not scan app %s due to BuildException: %s" % (
318                 appid, be))
319             probcount += 1
320         except VCSException as vcse:
321             logging.warn("VCS error while scanning app %s: %s" % (appid, vcse))
322             probcount += 1
323         except Exception:
324             logging.warn("Could not scan app %s due to unknown error: %s" % (
325                 appid, traceback.format_exc()))
326             probcount += 1
327
328     logging.info("Finished:")
329     print("%d problems found" % probcount)
330
331
332 if __name__ == "__main__":
333     main()