• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Ensembl / ensembl-variation / #641672209

20 Nov 2026 08:47AM UTC coverage: 83.146%. First build
#641672209

Pull #1222

travis-ci

Pull Request #1222: Fix SQL injection vulnerability

6037 of 10206 branches covered (59.15%)

50 of 51 new or added lines in 2 files covered. (98.04%)

22753 of 27365 relevant lines covered (83.15%)

1421.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.25
/modules/t/structuralVariationAdaptor.t
1
# Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
2
# Copyright [2016-2026] EMBL-European Bioinformatics Institute
3
# 
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
# 
8
#      http://www.apache.org/licenses/LICENSE-2.0
9
# 
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15

16

17
use strict;
1 ✔
18
use warnings;
1 ✔
19
use Test::More;
1 ✔
20
use Test::Exception;
1 ✔
21

22

23
use Bio::EnsEMBL::Variation::DBSQL::SupportingStructuralVariationAdaptor;
1 ✔
24
use Bio::EnsEMBL::Variation::DBSQL::VariationSetAdaptor;
1 ✔
25
use Bio::EnsEMBL::Test::TestUtils;
1 ✔
26
use Bio::EnsEMBL::Test::MultiTestDB;
1 ✔
27

28
our $verbose = 0;
1 ✔
29

30
my $multi = Bio::EnsEMBL::Test::MultiTestDB->new('homo_sapiens');
1 ✔
31

32
my $vdb = $multi->get_DBAdaptor('variation');
1 ✔
33
   $vdb->include_failed_variations(1);
1 ✔
34

35
my $sva = $vdb->get_StructuralVariationAdaptor();
1 ✔
36

37
ok($sva && $sva->isa('Bio::EnsEMBL::Variation::DBSQL::StructuralVariationAdaptor'), "isa sv adaptor");
1 ✔
38

39
# test fetch by dbID
40
print "\n# Test - fetch_by_dbID\n";
1 ✔
41
my $sv = $sva->fetch_by_dbID(3506221);
1 ✔
42

43
ok($sv->variation_name() eq 'esv93078',  'variation name by sv id');
1 ✔
44
ok($sv->source->name() eq 'DGVa',        'source name by sv id');
1 ✔
45
ok($sv->study->name() eq 'estd59',       'study name by sv id' );
1 ✔
46
ok($sv->var_class() eq 'CNV',            'sv class display by sv id' );
1 ✔
47
ok($sv->class_SO_term() eq 'copy_number_variation', 'sv class SO term by sv id' );
1 ✔
48
ok($sv->validation_status() eq 'high quality', 'validation status by sv id');
1 ✔
49

50
my $clin_sign = $sv->get_all_clinical_significance_states();
1 ✔
51
ok($clin_sign->[0] eq 'benign' &&
1 ✔
52
   $clin_sign->[1] eq 'likely benign', 'clinsig by sv id');        
53

54
my $study_test = $sv->study;
1 ✔
55

56
# test fetch by name
57
print "\n# Test - fetch_by_name\n";
1 ✔
58
$sv = $sva->fetch_by_name('esv2421345');
1 ✔
59

60
ok($sv->variation_name() eq 'esv2421345', "name by name");
1 ✔
61
ok($sv->dbID() == 16158654,      "id by name" );
1 ✔
62
ok($sv->source->name() eq 'DGVa',"source by name");
1 ✔
63
ok($sv->alias eq 'HM3_CNP_741', "alias by name");
1 ✔
64

65
# test store
66
print "\n# Test - store\n";
1 ✔
67
delete $sv->{$_} for qw(dbID variation_name);
1 ✔
68
$sv->variation_name('test');
1 ✔
69

70
ok($sva->store($sv), "store");
1 ✔
71

72
$sv = $sva->fetch_by_name('test');
1 ✔
73
ok($sv && $sv->variation_name eq 'test', "fetch stored");
1 ✔
74

75

76
# test fetch by supporting evidence
77
print "\n# Test - fetch_all_by_supporting_evidence\n";
1 ✔
78
my $ssva = $vdb->get_SupportingStructuralVariationAdaptor();
1 ✔
79
my $ssv = $ssva->fetch_by_name('essv194301');
1 ✔
80
my $sv2 = $sva->fetch_all_by_supporting_evidence($ssv);
1 ✔
81
ok($sv2->[0]->variation_name() eq 'esv93078', "name by name");
1 ✔
82

83
my @sv_dbIDs = (3506221,3506222);
1 ✔
84
my @sv_names = ('esv93078','esv89107');
1 ✔
85

86
# test fetch all by name list
87
print "\n# Test - fetch_all_by_name_list\n";
1 ✔
88
my $svs = $sva->fetch_all_by_name_list(\@sv_names);
1 ✔
89
ok($svs->[0]->variation_name eq $sv_names[0], "fetch by name list - 1");
1 ✔
90
ok($svs->[1]->variation_name eq $sv_names[1], "fetch by name list - 2");
1 ✔
91

92
# test fetch Iterator by dbID list
93
print "\n# Test - fetch_Iterator_by_dbID_list\n";
1 ✔
94
my $sv3 = $sva->fetch_Iterator_by_dbID_list(\@sv_dbIDs);
1 ✔
95
ok($sv3->next()->variation_name eq $sv_names[0], "iterator by id - 1");
1 ✔
96
ok($sv3->next()->variation_name eq $sv_names[1], "iterator by id - 2");
1 ✔
97

98

99
## Variation Set ##
100
my $vsa = $vdb->get_VariationSetAdaptor();
1 ✔
101
my $vs  = $vsa->fetch_by_name('1000 Genomes - High coverage - Trios');
1 ✔
102
my @vssv_dbIDs = (3506221,3506222);
1 ✔
103

104
# test fetch all dbIDs by VariationSet
105
print "\n# Test - fetch_all_dbIDs_by_VariationSet\n";
1 ✔
106
my $sv4 = $sva->fetch_all_dbIDs_by_VariationSet($vs);
1 ✔
107
ok($sv4->[0] == $vssv_dbIDs[0], "id by VariationSet - 1");
1 ✔
108
ok($sv4->[1] == $vssv_dbIDs[1], "id by VariationSet - 2");
1 ✔
109

110
# test fetch all by VariationSet
111
print "\n# Test - fetch_all_by_VariationSet\n";
1 ✔
112
my $sv5 = $sva->fetch_all_by_VariationSet($vs);
1 ✔
113
ok($sv5->[0]->variation_name eq $sv_names[0], "name by VariationSet - 1");
1 ✔
114
ok($sv5->[1]->variation_name eq $sv_names[1], "name by VariationSet - 2");
1 ✔
115

116
# test fetch Iterator by VariationSet
117
print "\n# Test - fetch_Iterator_by_VariationSet\n";
1 ✔
118
my $sv6 = $sva->fetch_Iterator_by_VariationSet($vs);
1 ✔
119
ok($sv6->next()->variation_name eq $sv_names[0], "iterator by VariationSet - 1");
1 ✔
120
ok($sv6->next()->variation_name eq $sv_names[1], "iterator by VariationSet - 2");
1 ✔
121

122

123
## Other - BaseStructuralVariationAdaptor ##
124
my $sv_name = 'CN_674347';
1 ✔
125

126
# test fetch all
127
print "\n# Test - fetch_all\n";
1 ✔
128
my $sv7= $sva->fetch_all();
1 ✔
129
ok($sv7->[0]->variation_name eq $sv_name, "sv by all");
1 ✔
130

131
# test fetch all somatic
132
print "\n# Test - fetch_all_somatic\n";
1 ✔
133
my $sv8= $sva->fetch_all_somatic();
1 ✔
134
ok($sv8->[0]->variation_name eq 'esv2221103', "sv by all somatic");
1 ✔
135

136
# test list dbIDs
137
print "\n# Test - list_dbIDs\n";
1 ✔
138
my $sv9= $sva->list_dbIDs();
1 ✔
139
ok($sv9->[0] == 770260, "sv id by list of dbIDs");
1 ✔
140

141
# test fetch by stable_id
142
print "\n# Test - fetch_by_stable_id\n";
1 ✔
143
my $sv10= $sva->fetch_by_stable_id($sv_name);
1 ✔
144
ok($sv10->variation_name eq $sv_name, "sv by stable_id");
1 ✔
145

146
# test fetch all by Study
147
print "\n# Test - fetch_all_by_Study\n";
1 ✔
148
my $sv11= $sva->fetch_all_by_Study($study_test);
1 ✔
149
ok($sv11->[0]->variation_name eq $sv_names[0], "sv by study");
1 ✔
150

151
# test fetch all by Source
152
print "\n# Test - fetch_all_by_Source\n";
1 ✔
153
my $sv12= $sva->fetch_all_by_Source($sv->source);
1 ✔
154
ok($sv12->[0]->variation_name eq $sv_names[0], "sv by source");
1 ✔
155

156
# test get_all_failed_descriptions
157
print "\n# Test - get_all_failed_descriptions\n";
1 ✔
158
my $failed_sv = $sva->fetch_by_name('esv902225');
1 ✔
159
my $sv13= $sva->get_all_failed_descriptions($failed_sv);
1 ✔
160
ok($sv13->[0] eq 'Variant can not be re-mapped to the current assembly', "failed descriptions");
1 ✔
161

162
subtest 'structural variation names are treated as data' => sub {
163
  $multi->save('variation', 'structural_variation', 'failed_structural_variation');
1 ✔
164

165
  my $original_species = $vdb->species;
1 ✔
166
  my $include_failed = $vdb->include_failed_variations;
1 ✔
167
  my $original_alias = Bio::EnsEMBL::Registry->get_alias('bos_taurus', 1);
1 ✔
168
  # Reuse the fixture as a non-human species, without human name normalisation.
169
  Bio::EnsEMBL::Registry->add_alias($original_species, 'bos_taurus');
1 ✔
170
  $vdb->species('bos_taurus');
1 ✔
171
  $vdb->include_failed_variations(0);
1 ✔
172

173
  my @names = ("test'quoted", 'test\\backslash', "test' OR '1'='1", 'test+phencode');
1 ✔
174
  my $insert = $vdb->dbc->prepare(
1 ✔
175
    'INSERT INTO structural_variation (variation_name, source_id, is_evidence) VALUES (?, ?, ?)'
176
  );
177
  $insert->execute($_, 11, 0) for @names;
1 ✔
178
  $insert->execute("supporting'quoted", 11, 1);
1 ✔
179
  $insert->finish;
1 ✔
180

181
  foreach my $name (@names) {
1 ✔
182
    my $result;
4 ✔
183
    lives_ok { $result = $sva->fetch_by_name($name) } 'literal name can be queried';
4 ✔
184
    is($result && $result->variation_name, $name, 'only the exact name is returned');
4 ✔
185
  }
186

187
  foreach my $name (
1 ✔
188
    "esv2421345' AND 1=1 AND 'x'='x",
189
    "esv2421345' AND 1=0 AND 'x'='x",
190
    "' OR 1=1 #",
191
    "'",
192
    '',
193
    undef,
194
  ) {
195
    my $result;
6 ✔
196
    lives_ok { $result = $sva->fetch_by_name($name) } 'unmatched input does not cause a SQL error';
6 ✔
197
    ok(!defined $result, 'SQL syntax cannot change the lookup');
6 ✔
198
  }
199

200
  is($sva->fetch_by_name('esv2421345')->dbID, 16158654, 'bindings do not leak into the next query');
1 ✔
201
  is($sva->fetch_by_stable_id($names[0])->variation_name, $names[0], 'stable ID alias handles quotes');
1 ✔
202
  is($ssva->fetch_by_name("supporting'quoted")->variation_name, "supporting'quoted", 'supporting variants use the same safe lookup');
1 ✔
203
  ok(!defined $sva->fetch_by_name("supporting'quoted"), 'evidence filter is preserved');
1 ✔
204

205
  ok(!defined $sva->fetch_by_name('esv902225'), 'failed variants are still excluded');
1 ✔
206
  $vdb->include_failed_variations(1);
1 ✔
207
  is($sva->fetch_by_name('esv902225')->variation_name, 'esv902225', 'failed variants can still be included');
1 ✔
208

209
  my $results;
1 ✔
210
  lives_ok { $results = $sva->fetch_all_by_name_list(["esv2421345') OR 1=1 #"]) } 'batch lookup treats SQL syntax literally';
1 ✔
211
  is_deeply($results, [], 'batch SQL input cannot retrieve other names');
1 ✔
212

213
  my @batch = ((map { "missing_sqlinj_$_" } 1..199), @names[0, 1]);
1 ✔
214
  my @original_batch = @batch;
1 ✔
215
  lives_ok { $results = $sva->fetch_all_by_name_list(\@batch) } 'quoted names work across the 200-name batch boundary';
1 ✔
216
  is_deeply([sort map { $_->variation_name } @$results], [sort @names[0, 1]], 'batch returns only matching literal names');
1 ✔
217
  is_deeply(\@batch, \@original_batch, 'batch input is not modified');
1 ✔
218

219
  $vdb->species($original_species);
1 ✔
220
  if (defined $original_alias) {
1 !
NEW
221
    Bio::EnsEMBL::Registry->add_alias($original_alias, 'bos_taurus');
×
222
  } else {
223
    Bio::EnsEMBL::Registry->remove_alias($original_species, 'bos_taurus');
1 ✔
224
  }
225
  is($sva->fetch_by_name('test phencode')->variation_name, 'test+phencode', 'human phencode normalisation is preserved');
1 ✔
226
  $vdb->include_failed_variations($include_failed);
1 ✔
227
  $multi->restore('variation', 'structural_variation', 'failed_structural_variation');
1 ✔
228
};
1 ✔
229

230

231
done_testing();
1 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc