summaryrefslogtreecommitdiffstats
path: root/scripts/generate-mirror-commands.py
blob: c7987c01f52155253526c8c1278875a88a195e71 (plain)
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
#!/usr/bin/env python3
# Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

import re
import sys

# Etree isn't subject to any data leak vulnerabilities:
# +---------------------------+------------+-----------------------------------+
# | Issue                     | Etree      | Issue description                 |
# +---------------------------+------------+-----------------------------------+
# | billion laughs            | Vulnerable | DDOS by huge CPU and memory usage |
# +---------------------------+------------+-----------------------------------+
# | quadratic blowup          | Vulnerable | DDOS by huge CPU and memory usage |
# +---------------------------+------------+-----------------------------------+
# | external entity expansion | Safe (1)   | Data leak                         |
# +---------------------------+------------+-----------------------------------+
# | DTD retrieval             | Safe       | Data leak                         |
# +---------------------------+------------+-----------------------------------+
# | decompression bomb        | Safe       | DDOS by huge amount of CPU        |
# +---------------------------+------------+-----------------------------------+
# (1) xml.etree.ElementTree doesn’t expand external entities and raises a
#     ParserError when an entity occurs.
#
# Other XML parsers like sax, minidom, pulldom, xmlrpc have similar security
# properties: they are not vulnerable (anymore) to data leaks but they are still
# vulnerable to DDOS attacks.
# Reference: https://docs.python.org/3.8/library/xml.html#xml-vulnerabilities
import xml.etree.ElementTree

import jinja2

def usage(progname):
    print ('Usage:')
    print ('\t{} <path/to/default.xml>'.format(progname))
    sys.exit(1)

class Manifest(object):
    def __init__(self, manifest_xml_path):
        self.remotes = {}
        self.xml_file = open(manifest_xml_path, 'r')
        tree = xml.etree.ElementTree.parse(self.xml_file)
        self.root = tree.getroot()
        self.defaults = self._get_defaults(self.root)

    def _get_defaults(self, root):
        for child in root.iter('default'):
            return child

    def get_remote(self, name):
        if name in self.remotes.keys():
            return self.remotes.get(name)

        for child in self.root.iter('remote'):
            if child.get('name') == name:
                self.remotes[name] = child
                return child

    def get_project_property(self, project, prop):
        if prop in project.keys():
            return project.get(prop)
        else:
            return self.defaults.get(prop)

    def get_clone_url(self, elm):
        repo_path = re.sub('/*$', '', self.get_project_property(elm, 'name'))
        remote_name = self.get_project_property(elm, 'remote')
        remote_url = re.sub('/*$', '',
                            self.get_remote(remote_name).get('fetch'))

        return "{}/{}".format(remote_url, repo_path)

    def get_revision(self, elm):
        if 'revision' in elm.keys():
            return elm.get('revision')

        remote = self.get_remote(self.get_project_property(elm, 'remote'))
        if 'revision' in remote:
            return remote.get('revision')
        elif self.defaults.get('remote') == remote.get('name'):
            return self.defaults.get('revision')

        assert(False)

    def get_base_directory(self, given_remote):
        # Since the commands are generated it's a good idea to do our
        # best not to produce potentially dangerous commands. The
        # downside is that the list above will need to be updated
        # automatically
        #
        # TODO: android-x86  ccache  F-Droid LineageOS
        whitelist = {
            'aosp' : {
                'fetch'   : 'https://android.googlesource.com',
                'dirname' : 'mirrors/AOSP',
            },
            'freedesktop' : {
                'fetch' : 'https://gitlab.freedesktop.org',
                'dirname' : 'mirrors/freesmartphone.org',
            },
        }

        assert(given_remote.get('name') in whitelist)
        whiltelist_renote = whitelist.get(given_remote.get('name'))
        whitelist_fetch_url = re.sub('/*$', '', whiltelist_renote.get('fetch'))
        given_remote_fetch_url = re.sub('/*$', '', given_remote.get('fetch'))
        assert (whitelist_fetch_url == given_remote_fetch_url)

        return whitelist.get(given_remote.get('name')).get('dirname')

    def is_revision_whitelisted(self, elm):
        remote = self.get_project_property(elm, 'remote')
        revision = self.get_revision(elm)
        if remote == 'aosp' and revision == 'refs/tags/android-11.0.0_r17':
            return True
        if remote == 'freedesktop' and \
           revision == '32819fe45972e0c706423d71075788a5885f7b86':
            return True

        return False

    def is_revision_blacklisted(self, elm):
        revision = self.get_revision(elm)
        if revision == 'master':
            return True

        return False

    def get_clone_commands(self, elm):
        commands = []

        url = self.get_clone_url(elm)
        remote = self.get_remote(self.get_project_property(elm, 'remote'))
        base_directory = self.get_base_directory(remote)
        repo_directory = self.get_project_property(elm,
                                                   'name').replace('/', '_')
        revision = self.get_revision(elm)

        # Guard against updating the master branch of mirrors that could be used
        # by several Replicant versions at the same time by forcing users to
        # manually declare revisions here. The revisions have to be either
        # whitelisted or blacklisted else we abort.
        if not self.is_revision_whitelisted(elm) and \
           not self.is_revision_blacklisted(elm):
            print('/!\ The "{}" revision of {}/{} is not known by {}'.format(
                revision, base_directory, repo_directory, sys.argv[0]))
            print('    Please add it either to the whitelist in {}'.format(
                'is_revision_whitelisted'))
            print('    or to the blacklist in {}'.format(
                'is_revision_blacklisted'))
            assert(False)
        elif self.is_revision_blacklisted(elm):
            return []

        commands.append("if [ ! -d {}/{}.git ] ; then".format(
            base_directory, repo_directory))

        commands.append("    git clone --mirror {} {}/{}.git".format(
            url, base_directory, repo_directory))

        commands.append("else")

        commands.append("    git -C {}/{}.git fetch {} {}".format(
            base_directory, repo_directory, url, revision))

        commands.append("    touch {}/{}.git/git-daemon-export-ok".format(
            base_directory, repo_directory))

        commands.append("fi")

        return commands

    def parse(self):
        for child in self.root.iter('project'):
            print("# {}".format(self.get_project_property(child, 'name')))
            for command in self.get_clone_commands(child):
                print(command)
            print('')

    def close(self):
        self.xml_file.close()

if __name__ == '__main__':
    if len(sys.argv) != 2:
           usage(sys.argv[0])

    manifest_xml_path = sys.argv[1]

    manifest = Manifest(manifest_xml_path)
    manifest.parse()
    manifest.close()