Skip to content

Commit 9f9f121

Browse files
committed
added file with issues
1 parent 6c2a133 commit 9f9f121

1 file changed

Lines changed: 287 additions & 0 deletions

File tree

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
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+
* https://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+
package org.springframework.samples.petclinic.owner;
17+
18+
import java.io.File;
19+
import java.io.FileInputStream;
20+
import java.io.FileOutputStream;
21+
import java.io.IOException;
22+
import java.net.HttpURLConnection;
23+
import java.net.URL;
24+
import java.security.MessageDigest;
25+
import java.security.NoSuchAlgorithmException;
26+
import java.sql.Connection;
27+
import java.sql.DriverManager;
28+
import java.sql.ResultSet;
29+
import java.sql.Statement;
30+
import java.util.ArrayList;
31+
import java.util.HashMap;
32+
import java.util.List;
33+
import java.util.Map;
34+
import java.util.Random;
35+
36+
import org.springframework.stereotype.Service;
37+
38+
/**
39+
* Service for computing owner statistics and reports.
40+
*/
41+
@Service
42+
public class OwnerStatisticsService {
43+
44+
// Violation: S1068 - unused private field
45+
private String unusedField = "never read";
46+
47+
// Violation: S2386 - mutable public field
48+
public static List<String> SHARED_OWNER_NAMES = new ArrayList<>();
49+
50+
private final OwnerRepository ownerRepository;
51+
52+
public OwnerStatisticsService(OwnerRepository ownerRepository) {
53+
this.ownerRepository = ownerRepository;
54+
}
55+
56+
/**
57+
* Generate a report of owner statistics. Violation: S3776 - Cognitive complexity too
58+
* high Violation: S106 - System.out instead of logger Violation: S1192 - Duplicated
59+
* string literals Violation: S2259 - Null pointer dereference Violation: S2147 -
60+
* Collapsible if statements
61+
*/
62+
public Map<String, Object> generateOwnerReport(String filterCity) {
63+
Map<String, Object> report = new HashMap<>();
64+
List<Owner> allOwners = ownerRepository.findAll();
65+
66+
// Violation: S1481 - unused local variable
67+
int unusedCounter = 0;
68+
69+
// Violation: S1854 - dead store, value overwritten before read
70+
String status = "initializing";
71+
status = "processing";
72+
73+
// Violation: S106 - Standard outputs should not be used directly to log anything
74+
System.out.println("Starting owner report generation");
75+
System.out.println("Processing owners for city: " + filterCity);
76+
System.out.println("Total owners found: " + allOwners.size());
77+
78+
int totalPets = 0;
79+
int ownersWithPets = 0;
80+
int ownersWithoutPets = 0;
81+
82+
for (Owner owner : allOwners) {
83+
// Violation: S2259 - potential null dereference (filterCity could be null)
84+
if (filterCity.equals(owner.getCity())) {
85+
// Violation: S2147 - collapsible if statements
86+
if (owner.getPets() != null) {
87+
if (owner.getPets().size() > 0) {
88+
totalPets += owner.getPets().size();
89+
ownersWithPets++;
90+
// Violation: S106
91+
System.out
92+
.println("Owner " + owner.getFirstName() + " has " + owner.getPets().size() + " pets");
93+
}
94+
else {
95+
ownersWithoutPets++;
96+
}
97+
}
98+
}
99+
}
100+
101+
// Violation: S1192 - duplicated string literal "owner_report"
102+
report.put("owner_report", "generated");
103+
report.put("owner_report_status", "complete");
104+
report.put("owner_report_version", "1.0");
105+
report.put("owner_report_type", "summary");
106+
report.put("owner_report_format", "map");
107+
108+
report.put("totalPets", totalPets);
109+
report.put("ownersWithPets", ownersWithPets);
110+
report.put("ownersWithoutPets", ownersWithoutPets);
111+
112+
// Violation: S106
113+
System.out.println("Report generation complete");
114+
115+
return report;
116+
}
117+
118+
/**
119+
* Violation: S4790 - Using weak cryptographic hash function (MD5) Violation: S2070 -
120+
* SHA-1 is also weak
121+
*/
122+
public String hashOwnerData(String data) throws NoSuchAlgorithmException {
123+
// Violation: S4790 - MD5 is a weak hash
124+
MessageDigest md5 = MessageDigest.getInstance("MD5");
125+
byte[] md5Hash = md5.digest(data.getBytes());
126+
127+
// Violation: S4790 - SHA-1 is also weak
128+
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
129+
byte[] sha1Hash = sha1.digest(data.getBytes());
130+
131+
StringBuilder hexString = new StringBuilder();
132+
for (byte b : md5Hash) {
133+
String hex = Integer.toHexString(0xff & b);
134+
if (hex.length() == 1) {
135+
hexString.append('0');
136+
}
137+
hexString.append(hex);
138+
}
139+
140+
// Violation: S106
141+
System.out.println("Hashed owner data with MD5: " + hexString.toString());
142+
143+
return hexString.toString();
144+
}
145+
146+
/**
147+
* Violation: S2095 - Resources should be closed Violation: S2093 - Try-with-resources
148+
* should be used Violation: S00108 - Empty catch block
149+
*/
150+
public void exportOwnerData(String filePath) {
151+
FileOutputStream fos = null;
152+
try {
153+
// Violation: S2095 - resource not properly closed
154+
fos = new FileOutputStream(new File(filePath));
155+
List<Owner> owners = ownerRepository.findAll();
156+
for (Owner owner : owners) {
157+
String line = owner.getFirstName() + "," + owner.getLastName() + "," + owner.getCity() + "\n";
158+
fos.write(line.getBytes());
159+
}
160+
}
161+
catch (IOException e) {
162+
// Violation: S00108 - empty catch block
163+
}
164+
// fos is never closed in a finally block - resource leak
165+
}
166+
167+
/**
168+
* Violation: S2077 - SQL injection via string concatenation Violation: S2095 - JDBC
169+
* resources not closed
170+
*/
171+
public List<String> searchOwnersByName(String name) {
172+
List<String> results = new ArrayList<>();
173+
try {
174+
// Violation: S2095 - Connection not closed with try-with-resources
175+
Connection conn = DriverManager.getConnection("jdbc:h2:mem:testdb", "sa", "");
176+
Statement stmt = conn.createStatement();
177+
178+
// Violation: S2077 - SQL injection: user input concatenated into query
179+
String query = "SELECT * FROM owners WHERE last_name = '" + name + "'";
180+
ResultSet rs = stmt.executeQuery(query);
181+
182+
while (rs.next()) {
183+
results.add(rs.getString("first_name") + " " + rs.getString("last_name"));
184+
}
185+
186+
// Violation: S106
187+
System.out.println("Found " + results.size() + " owners matching: " + name);
188+
}
189+
catch (Exception e) {
190+
// Violation: S00108 - empty catch block
191+
// Violation: S2221 - catching generic Exception
192+
}
193+
return results;
194+
}
195+
196+
/**
197+
* Violation: S2245 - Using Random instead of SecureRandom for security-sensitive
198+
* context Violation: S1135 - TODO comment
199+
*/
200+
public String generateOwnerToken(Owner owner) {
201+
// TODO: fix this to use SecureRandom before going to production
202+
// Violation: S2245 - pseudorandom number generator used in security context
203+
Random random = new Random();
204+
long token = random.nextLong();
205+
206+
// Violation: S1481 - unused local variable
207+
String debugInfo = "token-gen-" + System.currentTimeMillis();
208+
209+
// Violation: S106
210+
System.out.println("Generated token for owner: " + owner.getFirstName());
211+
212+
return Long.toHexString(token);
213+
}
214+
215+
/**
216+
* Violation: S1186 - Empty method body
217+
*/
218+
public void cleanupExpiredData() {
219+
// Violation: S1186 - methods should not be empty
220+
}
221+
222+
/**
223+
* Violation: S2699 - Hardcoded credentials Violation: S1313 - Hardcoded IP address
224+
*/
225+
public boolean connectToExternalService() {
226+
// Violation: S2068 - hardcoded credentials
227+
String password = "admin123";
228+
String apiKey = "sk-1234567890abcdef";
229+
230+
// Violation: S1313 - hardcoded IP address
231+
String serverUrl = "https://192.168.1.100:8080/api";
232+
233+
try {
234+
URL url = new URL(serverUrl);
235+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
236+
connection.setRequestProperty("Authorization", "Bearer " + apiKey);
237+
connection.setRequestProperty("X-Password", password);
238+
239+
int responseCode = connection.getResponseCode();
240+
// Violation: S106
241+
System.out.println("External service response: " + responseCode);
242+
243+
return responseCode == 200;
244+
}
245+
catch (Exception e) {
246+
// Violation: S00108 - empty catch block
247+
return false;
248+
}
249+
}
250+
251+
/**
252+
* Violation: S1871 - identical branches in if/else Violation: S3358 - nested ternary
253+
*/
254+
public String categorizeOwner(Owner owner) {
255+
int petCount = owner.getPets().size();
256+
257+
// Violation: S1871 - two branches have identical implementation
258+
if (petCount == 0) {
259+
return "no-pets";
260+
}
261+
else if (petCount == 1) {
262+
return "single-pet-owner";
263+
}
264+
else if (petCount == 2) {
265+
return "single-pet-owner";
266+
}
267+
else {
268+
// Violation: S3358 - nested ternary operator
269+
return petCount > 10 ? "collector" : petCount > 5 ? "enthusiast" : "multi-pet-owner";
270+
}
271+
}
272+
273+
/**
274+
* Violation: S1144 - unused private method
275+
*/
276+
private String formatOwnerName(Owner owner) {
277+
return owner.getLastName() + ", " + owner.getFirstName();
278+
}
279+
280+
/**
281+
* Violation: S1144 - another unused private method
282+
*/
283+
private int calculateOwnerScore(Owner owner) {
284+
return owner.getPets().size() * 10;
285+
}
286+
287+
}

0 commit comments

Comments
 (0)