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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe "pip" do
context "when a project depends on the latest version of pip" do
let(:requirements) { "sentry-sdk>=0.7.7" }
it 'produces a valid report' do
runner.add_file('requirements.txt', requirements)
report = runner.scan
expect(report).to match_schema
expect(report[:version]).to start_with('2')
expect(report.dependency_names).to include("sentry-sdk")
expect(report.licenses_for('sentry-sdk')).to match_array(["BSD-4-Clause"])
end
end
context "when the project has a dependency that depends on a minimum of python 3.6" do
let(:requirements) do
[
'boto3',
'aws-lambda-context>=1.0.0',
'jsonschema>=3.0.0',
'python-json-logger>=0.1.10',
'sentry-sdk>=0.7.7',
'ptvsd',
'pylint',
'flake8',
'bandit',
'pydocstyle'
].join("\n")
end
it 'produces a valid report' do
runner.add_file('requirements.txt', requirements)
report = runner.scan
expect(report).to match_schema
expect(report[:version]).to start_with('2')
expect(report[:licenses]).not_to be_empty
expect(report[:dependencies]).not_to be_empty
end
end
context 'with BSD licenses dependencies' do
let(:requirements) do
[
'webencodings',
'idna',
'click',
'EasyProcess'
].join("\n")
end
it 'finds proper versions of BSD license' do
runner.add_file('requirements.txt', requirements)
report = runner.scan
expect(report.licenses_for('webencodings')).to eq(['BSD'])
expect(report.licenses_for('idna')).to eq(['BSD-like'])
expect(report.licenses_for('click')).to eq(['BSD-3-Clause'])
expect(report.licenses_for('EasyProcess')).to eq(['BSD'])
end
end
[{ version: '2', commit: '04dce91b' }, { version: '3', commit: '48e250a1' }].each do |python|
['1.0', '1.1', '2.0', '2.1'].each do |report_version|
context "when generating a `#{report_version}` report using Python `#{python[:version]}`" do
let(:url) { "https://gitlab.com/gitlab-org/security-products/tests/#{language}-#{package_manager}.git" }
let(:language) { 'python' }
let(:package_manager) { 'pip' }
let(:environment) { { 'LM_REPORT_VERSION' => report_version, 'LM_PYTHON_VERSION' => python[:version] } }
let(:expected_content) { fixture_file_content("expected/#{language}/#{python[:version]}/#{package_manager}/v#{report_version}.json").chomp }
it 'matches the expected report' do
runner.clone(url, branch: python[:commit])
report = runner.scan(env: environment)
expect(JSON.pretty_generate(report.to_h)).to eq(expected_content)
expect(report).to match_schema(version: report_version)
end
end
end
end
context "when scanning projects with a `setup.py` and does not have a `requirements.txt` file" do
it 'detects licenses in a simple `setup.py`' do
runner.add_file('setup.py', fixture_file_content('python/simple-setup.py'))
report = runner.scan
expect(report).to match_schema(version: '2.0')
expect(report[:dependencies]).not_to be_empty
expect(report.licenses_for('boto3')).to match_array(['Apache-2.0'])
end
it 'detects licenses in a more complicated `setup.py`' do
runner.clone('https://github.com/pypa/sampleproject.git', branch: 'd09af3dbd851d385e56f0aed29875bfa3d3df230')
report = runner.scan
expect(report).to match_schema(version: '2.0')
expect(report[:dependencies]).not_to be_empty
expect(report.licenses_for('peppercorn')).to match_array(['BSD-2-Clause'])
end
end
context "when scanning projects that have a custom index-url" do
before do
runner.add_file('requirements.txt', 'six')
end
it 'detects the licenses from the custom index' do
report = runner.scan(env: { 'PIP_INDEX_URL' => 'https://test.pypi.org/simple/' })
expect(report).to match_schema(version: '2.0')
expect(report.licenses_for('six')).to match_array(["MIT"])
end
end
context "when a project uses a custom `SETUP_CMD`" do
before do
runner.add_file('requirements.txt', 'six==1.14.0')
runner.add_file('.python-version', '3.8.4')
runner.add_file('custom.sh') do
<<~SCRIPT
#!/bin/bash -lx
asdf install python 3.8.4
asdf reshim
pip install -r requirements.txt
SCRIPT
end
end
it 'detects the software licenses' do
report = runner.scan(env: { 'SETUP_CMD' => 'bash custom.sh' })
expect(report).to match_schema(version: '2.0')
expect(report.licenses_for('six')).to match_array(["MIT"])
expect(report.dependency_names).to contain_exactly('six')
end
end
context "when a projects is running in airgap mode" do
before do
runner.add_file('requirements.txt', '')
end
it 'is able to scan the project' do
report = runner.scan(env: {
'PIP_INDEX_URL' => 'https://localhost/simple/'
})
expect(report).to match_schema(version: '2.0')
expect(report[:licenses]).to be_empty
expect(report[:dependencies]).to be_empty
end
end
context "when connecting to a private package repository with self signed certificate" do
let(:index_url) { "https://pypi.test/simple" }
before do
add_host('pypi.test', '127.0.0.1')
start_proxy_server
runner.add_file('setup.py') do
<<~RAW
from setuptools import setup, find_packages
setup(
name='gitlab-sp-test-python-pip',
version='1.2.0',
packages=find_packages(),
install_requires=['requests'],
)
RAW
end
end
it 'downloads the packages and trusts the certificate' do
report = runner.scan(env: {
'ADDITIONAL_CA_CERT_BUNDLE' => x509_certificate('wildcard.test').read,
'PIP_INDEX_URL' => index_url
})
expect(report).to match_schema(version: '2.0')
expect(report.dependency_names).to include('requests')
expect(report.licenses_for('requests')).to match_array(['Apache-2.0'])
end
end
context "when specifying the version of Python using environment variables" do
let(:output_file) { Pathname.new(runner.project_path.join('output.txt')) }
before do
runner.add_file('custom.sh') do
<<~SCRIPT
#!/bin/bash -l
python --version &> '#{output_file}'
SCRIPT
end
end
it 'prioritizes `LM_PYTHON_VERSION` over the `ASDF_PYTHON_VERSION`' do
runner.scan(env: {
'ASDF_PYTHON_VERSION' => '3.8.2',
'LM_PYTHON_VERSION' => '2.7.18',
'SETUP_CMD' => 'bash custom.sh'
})
expect(output_file).to exist
expect(output_file.read).to include('2.7.18')
end
it 'reads the ASDF_PYTHON_VERSION' do
runner.scan(env: {
'ASDF_PYTHON_VERSION' => '3.8.2',
'SETUP_CMD' => 'bash custom.sh'
})
expect(output_file).to exist
expect(output_file.read).to include('3.8.2')
end
it 'defaults to Python 3' do
runner.scan(env: { 'SETUP_CMD' => 'bash custom.sh' })
expect(output_file).to exist
expect(output_file.read).to include('3.8.2')
end
end
end
|