aboutsummaryrefslogtreecommitdiff
blob: 968e2bf2bf1c672601f50a58389938deae9ac350 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Copyright 2013-2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2


import portage
from portage import os, _encodings
from portage.tests import TestCase
from portage.tests.resolver.ResolverPlayground import (
    ResolverPlayground,
    ResolverPlaygroundTestCase,
)

from portage.glsa import GlsaFormatException


class SecuritySetTestCase(TestCase):
    glsa_template = """\
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/xsl/glsa.xsl" type="text/xsl"?>
<?xml-stylesheet href="/xsl/guide.xsl" type="text/xsl"?>
<!DOCTYPE glsa SYSTEM "http://www.gentoo.org/dtd/glsa.dtd">
<glsa id="%(glsa_id)s">
  <title>%(pkgname)s: Multiple vulnerabilities</title>
  <synopsis>Multiple vulnerabilities have been found in %(pkgname)s.
  </synopsis>
  <product type="ebuild">%(pkgname)s</product>
  <announced>January 18, 2013</announced>
  <revised count="1">January 18, 2013</revised>
  <bug>55555</bug>
  <access>remote</access>
  <affected>
    <package name="%(cp)s" auto="yes" arch="%(arch)s">
      <unaffected range="ge">%(unaffected_version)s</unaffected>
      <vulnerable range="lt">%(unaffected_version)s</vulnerable>
    </package>
  </affected>
  <background>
    <p>%(pkgname)s is software package.</p>
  </background>
  <description>
    <p>Multiple vulnerabilities have been discovered in %(pkgname)s.
    </p>
  </description>
  <impact type="normal">
    <p>A remote attacker could exploit these vulnerabilities.</p>
  </impact>
  <workaround>
    <p>There is no known workaround at this time.</p>
  </workaround>
  <resolution>
    <p>All %(pkgname)s users should upgrade to the latest version:</p>
    <code>
      # emerge --sync
      # emerge --ask --oneshot --verbose "&gt;=%(cp)s-%(unaffected_version)s"
    </code>
  </resolution>
  <references>
  </references>
</glsa>
"""

    def _must_skip(self):
        try:
            __import__("xml.etree.ElementTree")
            __import__("xml.parsers.expat").parsers.expat.ExpatError
        except (AttributeError, ImportError):
            return "python is missing xml support"

    def write_glsa_test_case(self, glsa_dir, glsa):
        with open(
            os.path.join(glsa_dir, "glsa-" + glsa["glsa_id"] + ".xml"),
            encoding=_encodings["repo.content"],
            mode="w",
        ) as f:
            f.write(self.glsa_template % glsa)

    def testSecuritySet(self):
        skip_reason = self._must_skip()
        if skip_reason:
            self.portage_skip = skip_reason
            self.assertFalse(True, skip_reason)
            return

        ebuilds = {
            "cat/A-vulnerable-2.2": {"KEYWORDS": "x86"},
            "cat/B-not-vulnerable-4.5": {"KEYWORDS": "x86"},
        }

        installed = {
            "cat/A-vulnerable-2.1": {"KEYWORDS": "x86"},
            "cat/B-not-vulnerable-4.4": {"KEYWORDS": "x86"},
        }

        glsas = (
            {
                "glsa_id": "201301-01",
                "pkgname": "A-vulnerable",
                "cp": "cat/A-vulnerable",
                "unaffected_version": "2.2",
                "arch": "*",
            },
            {
                "glsa_id": "201301-02",
                "pkgname": "B-not-vulnerable",
                "cp": "cat/B-not-vulnerable",
                "unaffected_version": "4.4",
                "arch": "*",
            },
            {
                "glsa_id": "201301-03",
                "pkgname": "NotInstalled",
                "cp": "cat/NotInstalled",
                "unaffected_version": "3.5",
                "arch": "*",
            },
        )

        world = ["cat/A"]

        test_cases = (
            ResolverPlaygroundTestCase(
                ["@security"],
                options={},
                success=True,
                mergelist=["cat/A-vulnerable-2.2"],
            ),
        )

        playground = ResolverPlayground(
            ebuilds=ebuilds, installed=installed, world=world, debug=False
        )

        try:
            portdb = playground.trees[playground.eroot]["porttree"].dbapi
            glsa_dir = os.path.join(
                portdb.repositories["test_repo"].location, "metadata", "glsa"
            )
            portage.util.ensure_dirs(glsa_dir)
            for glsa in glsas:
                self.write_glsa_test_case(glsa_dir, glsa)

            for test_case in test_cases:
                playground.run_TestCase(test_case)
                self.assertEqual(test_case.test_success, True, test_case.fail_msg)
        finally:
            playground.cleanup()

    def testStatelessSecuritySet(self):
        # Tests which don't rely on the GLSA being fixed. This allows
        # testing the format parsing with a bit more flexibility (no
        # need to keep inventing packages).

        skip_reason = self._must_skip()
        if skip_reason:
            self.portage_skip = skip_reason
            self.assertFalse(True, skip_reason)
            return

        ebuilds = {
            "cat/A-vulnerable-2.2": {"KEYWORDS": "x86"},
            "cat/B-not-vulnerable-4.5": {"KEYWORDS": "x86"},
        }

        installed = {
            "cat/A-vulnerable-2.1": {"KEYWORDS": "x86"},
            "cat/B-not-vulnerable-4.4": {"KEYWORDS": "x86"},
        }

        glsas = (
            {
                "glsa_id": "201301-04",
                "pkgname": "A-vulnerable",
                "cp": "cat/A-vulnerable",
                "unaffected_version": "2.2",
                # Use an invalid delimiter (comma)
                "arch": "amd64,sparc",
            },
            {
                "glsa_id": "201301-05",
                "pkgname": "A-vulnerable",
                "cp": "cat/A-vulnerable",
                "unaffected_version": "2.2",
                # Use an invalid arch (~arch)
                "arch": "~amd64",
            },
            {
                "glsa_id": "201301-06",
                "pkgname": "A-vulnerable",
                "cp": "cat/A-vulnerable",
                "unaffected_version": "2.2",
                # Two valid arches followed by an invalid one
                "arch": "amd64 sparc $$$$",
            },
        )

        world = ["cat/A"]

        test_cases = (
            ResolverPlaygroundTestCase(
                ["@security"],
                success=True,
                mergelist=["cat/A-vulnerable-2.2"],
            ),
        )

        # Give each GLSA a clean slate
        for glsa in glsas:
            playground = ResolverPlayground(
                ebuilds=ebuilds, installed=installed, world=world, debug=True
            )

            try:
                portdb = playground.trees[playground.eroot]["porttree"].dbapi
                glsa_dir = os.path.join(
                    portdb.repositories["test_repo"].location, "metadata", "glsa"
                )
                portage.util.ensure_dirs(glsa_dir)

                self.write_glsa_test_case(glsa_dir, glsa)

                with self.assertRaises(GlsaFormatException):
                    for test_case in test_cases:
                        playground.run_TestCase(test_case)
            finally:
                playground.cleanup()