Skip to content

Commit e4df528

Browse files
committed
fix: remove hardcoded salts
Signed-off-by: Oleh Astappiev <4512729+astappiev@users.noreply.github.com>
1 parent 55354b5 commit e4df528

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/main/java/de/l3s/learnweb/app/ConfigProvider.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import jakarta.enterprise.inject.spi.DeploymentException;
2121
import jakarta.inject.Named;
2222

23+
import org.apache.commons.lang3.RandomStringUtils;
2324
import org.apache.commons.lang3.StringUtils;
2425
import org.apache.logging.log4j.LogManager;
2526
import org.apache.logging.log4j.Logger;
@@ -42,6 +43,11 @@ public class ConfigProvider implements Serializable {
4243
*/
4344
private final Properties properties = new Properties();
4445

46+
/**
47+
* Application secret key, used for links generation, encryption, etc. Might break links if changed.
48+
*/
49+
private String appSecret;
50+
4551
/**
4652
* A version of the application from pom.xml (extracted from web.xml, maven should put it there on build).
4753
*/
@@ -251,6 +257,18 @@ public boolean getPropertyBoolean(final String key, final boolean defaultValue)
251257
return Boolean.parseBoolean(properties.getProperty(key, String.valueOf(defaultValue)));
252258
}
253259

260+
public String getAppSecret() {
261+
if (appSecret == null) {
262+
appSecret = properties.getProperty("app_secret");
263+
if (appSecret == null) {
264+
appSecret = RandomStringUtils.secure().nextAlphanumeric(56);
265+
log.warn("No app secret found, new token generated: {}", appSecret);
266+
log.warn("Please set app_secret in application.properties or .env file to avoid token regeneration on every restart.");
267+
}
268+
}
269+
return appSecret;
270+
}
271+
254272
public String getEnvironment() {
255273
if (environment == null) {
256274
if (!isDevelopment()) {

src/main/java/de/l3s/learnweb/app/Learnweb.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
@ApplicationScoped
3939
@SuppressWarnings("AssignmentToStaticFieldFromInstanceMethod")
4040
public final class Learnweb {
41-
public static final String SALT_1 = "ff4a9ff19306ee0407cf69d592";
42-
public static final String SALT_2 = "3a129713cc1b33650816d61450";
4341

4442
private static Learnweb learnweb;
4543

src/main/java/de/l3s/learnweb/forum/ForumNotificator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ private void sendMailWithNewTopics(User user, List<ForumTopic> topics) throws Me
7777
}
7878

7979
public static String getHash(User user) {
80-
return user.getId() + ":" + HashHelper.sha512(Learnweb.SALT_1 + user.getId() + Learnweb.SALT_2 + "notification");
80+
return user.getId() + ":" + HashHelper.sha256(user.getId() + "unsubscribe" + Learnweb.config().getAppSecret());
8181
}
8282
}

src/main/java/de/l3s/learnweb/resource/glossary/GlossaryBean.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import de.l3s.learnweb.resource.search.solrClient.FileInspector;
5757
import de.l3s.learnweb.user.Organisation.Option;
5858
import de.l3s.learnweb.user.User;
59+
import de.l3s.util.HashHelper;
5960
import de.l3s.util.Image;
6061
import de.l3s.util.bean.BeanHelper;
6162

@@ -458,13 +459,21 @@ public void postProcessXls(Object document) {
458459

459460
HSSFCellStyle copyrightStyle = wb.createCellStyle();
460461
copyrightStyle.setLocked(true);
461-
sheet.protectSheet(Learnweb.SALT_1); // use SALT as password
462+
sheet.protectSheet(getXlsPassword());
462463
}
463464
} catch (RuntimeException | IOException e) {
464465
throw new HttpException("Error in postprocessing Glossary XLS for resource: " + glossaryResource.getId(), e);
465466
}
466467
}
467468

469+
private String getXlsPassword() {
470+
String glossaryPassword = config().getProperty("glossary_password");
471+
if (glossaryPassword == null) {
472+
return HashHelper.sha256(config().getAppSecret() + "glossary");
473+
}
474+
return glossaryPassword;
475+
}
476+
468477
public void rotatePDF(Object document) {
469478
Document doc = (Document) document;
470479
doc.setPageSize(PageSize.A4.rotate());

0 commit comments

Comments
 (0)