Skip to content

Commit 4b828eb

Browse files
authored
Lift (#109)
* address lift hint: resource leak, stream not closed * lift: predictable random * lift: sql injection * lift null deference * sql injection
1 parent acccafc commit 4b828eb

File tree

4 files changed

+31
-18
lines changed

4 files changed

+31
-18
lines changed

rebot-plugins/rebot-welcome-message-plugin/src/main/java/xyz/rebasing/rebot/plugin/welcome/kogito/WelcomeChallenge.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright (c) 2017 Rebasing.xyz ReBot
4+
* Copyright (c) 2017 Rebasing.xyz ReBot
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy of
77
* this software and associated documentation files (the "Software"), to deal in
@@ -23,9 +23,9 @@
2323

2424
package xyz.rebasing.rebot.plugin.welcome.kogito;
2525

26+
import java.security.SecureRandom;
2627
import java.util.ArrayList;
2728
import java.util.List;
28-
import java.util.Random;
2929

3030
public class WelcomeChallenge {
3131

@@ -46,6 +46,7 @@ public class WelcomeChallenge {
4646

4747
/**
4848
* randomize two numbers and a math operator to start the challenge
49+
*
4950
* @param user that will answer the challenge
5051
*/
5152
public WelcomeChallenge(String user) {
@@ -196,7 +197,7 @@ private String defineMathOp() {
196197
* @return random integer number
197198
*/
198199
private int randomNumber(int max) {
199-
return new Random().nextInt(max);
200+
return new SecureRandom().nextInt(max);
200201
}
201202

202203
@Override

rebot-services/rebot-persistence-service/src/main/java/xyz/rebasing/rebot/service/persistence/repository/ApiRepository.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ public void persist(BotStatus botStatus) {
6262
*/
6363
public void remove(long chatId) {
6464
log.debugv("Enabling bot for chat {0}", chatId);
65-
Query q = em.createNativeQuery("DELETE FROM BOT_STATUS where ID=" + chatId + ";");
65+
Query q = em.createNativeQuery("DELETE FROM BOT_STATUS where ID= :chatId").setParameter("chatId", chatId);
6666
q.executeUpdate();
6767
em.flush();
6868
}
6969

7070
/**
71-
* @return if the bot is enabled or not
72-
* In case there is no state saved return true.
73-
*
71+
* Verify if the bot is enabled
7472
* @param chatId chat id to verify if the bos enabled
73+
* @return true or false. In case there is no state saved return true.
7574
*/
7675
public boolean isBotEnabled(long chatId) {
7776
try {
78-
Query q = em.createNativeQuery("SELECT isEnabled from BOT_STATUS where ID=" + chatId + ";");
77+
Query q = em.createNativeQuery("SELECT isEnabled from BOT_STATUS where ID= :chatId")
78+
.setParameter("chatId", chatId);
7979
return (boolean) q.getSingleResult();
8080
} catch (final Exception e) {
8181
return true;
@@ -85,9 +85,9 @@ public boolean isBotEnabled(long chatId) {
8585
/**
8686
* Check if the given command is active in the provided chat group
8787
*
88-
* @param groupId chat group to be verified
88+
* @param groupId chat group to be verified
8989
* @param commandName command to verify
90-
* @return if the given command is enabled is enabled or not
90+
* @return if the given command is enabled or not
9191
*/
9292
public boolean isCommandEnabled(long groupId, String commandName) {
9393
try {
@@ -101,7 +101,7 @@ public boolean isCommandEnabled(long groupId, String commandName) {
101101
/**
102102
* Enable the given command in the provided chatId
103103
*
104-
* @param chatId chat id or group to be verified
104+
* @param chatId chat id or group to be verified
105105
* @param commandName command to be enabled
106106
*/
107107
public void enableCommand(long chatId, String commandName) {

rebot-services/rebot-persistence-service/src/main/resources/application.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ quarkus.log.category."org.hibernate.cache".level=DEBUG
2121
# dev
2222
%dev.quarkus.datasource.db-kind=h2
2323
%dev.quarkus.datasource.jdbc.url=jdbc:h2:mem:testdb
24-
#%dev.quarkus.hibernate-orm.log.sql=true
25-
#%dev.quarkus.log.category."org.hibernate".level=DEBUG
26-
#%dev.quarkus.log.category."org.hibernate.cache".level=DEBUG
24+
%dev.quarkus.hibernate-orm.log.sql=true
25+
%dev.quarkus.log.category."org.hibernate".level=DEBUG
26+
%dev.quarkus.log.category."org.hibernate.cache".level=DEBUG
2727
%dev.xyz.rebasing.rebot.telegram.userId=userid
2828
%dev.xyz.rebasing.rebot.telegram.token=token
2929

rebot-telegram-api/rebot-telegram-api-spi/src/main/java/xyz/rebasing/rebot/api/i18n/BundleUTF8Control.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.io.InputStream;
55
import java.io.InputStreamReader;
6+
import java.net.HttpURLConnection;
67
import java.net.URL;
78
import java.net.URLConnection;
89
import java.util.Locale;
@@ -22,13 +23,24 @@ public ResourceBundle newBundle(String baseName,
2223
String resourceName = toResourceName(bundleName, "properties");
2324
ResourceBundle bundle = null;
2425
InputStream stream = null;
26+
URLConnection connection = null;
2527
if (reload) {
2628
URL url = loader.getResource(resourceName);
2729
if (url != null) {
28-
URLConnection connection = url.openConnection();
29-
if (connection != null) {
30-
connection.setUseCaches(false);
31-
stream = connection.getInputStream();
30+
try {
31+
connection = url.openConnection();
32+
if (connection != null) {
33+
connection.setUseCaches(false);
34+
stream = connection.getInputStream();
35+
}
36+
} finally {
37+
if (stream != null) {
38+
connection.getInputStream().close();
39+
connection.getOutputStream().close();
40+
HttpURLConnection c = (HttpURLConnection) connection;
41+
c.disconnect();
42+
stream.close();
43+
}
3244
}
3345
}
3446
} else {

0 commit comments

Comments
 (0)