Skip to content

Commit 536ffae

Browse files
committed
Increase cookie authentication IP length to 45->1000 characters
Affects issues: - Fixed #4654
1 parent 9bbc40c commit 536ffae

6 files changed

Lines changed: 50 additions & 5 deletions

File tree

Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/auth/CookieAuthentication.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
import com.djrapitops.plan.delivery.domain.auth.User;
2020
import com.djrapitops.plan.utilities.dev.Untrusted;
21+
import org.apache.commons.lang3.StringUtils;
2122

2223
import java.util.Objects;
2324

25+
import static com.djrapitops.plan.storage.database.sql.tables.CookieTable.MAX_IP_ADDRESS_LENGTH;
26+
2427
public class CookieAuthentication implements Authentication {
2528

2629
private final ActiveCookieStore activeCookieStore;
@@ -32,7 +35,7 @@ public class CookieAuthentication implements Authentication {
3235
public CookieAuthentication(ActiveCookieStore activeCookieStore, @Untrusted String cookie, @Untrusted String accessAddress) {
3336
this.activeCookieStore = activeCookieStore;
3437
this.cookie = cookie;
35-
this.accessAddress = accessAddress;
38+
this.accessAddress = StringUtils.truncate(accessAddress, MAX_IP_ADDRESS_LENGTH);
3639
}
3740

3841
@Override

Plan/common/src/main/java/com/djrapitops/plan/delivery/webserver/auth/CookieMetadata.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
package com.djrapitops.plan.delivery.webserver.auth;
1818

1919
import com.djrapitops.plan.delivery.domain.auth.User;
20+
import org.apache.commons.lang3.StringUtils;
2021

2122
import java.util.Objects;
2223

24+
import static com.djrapitops.plan.storage.database.sql.tables.CookieTable.MAX_IP_ADDRESS_LENGTH;
25+
2326
/**
2427
* @author AuroraLS3
2528
*/
@@ -48,7 +51,7 @@ public void setExpires(long expires) {
4851
}
4952

5053
public String getIpAddress() {
51-
return ipAddress;
54+
return StringUtils.truncate(ipAddress, MAX_IP_ADDRESS_LENGTH);
5255
}
5356

5457
@Override

Plan/common/src/main/java/com/djrapitops/plan/storage/database/Patches.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public static Patch[] getAll(PluginLogger logger, PlanConfig config) {
9090
new TPSTableMSPTPatch(),
9191
new AllowlistIncorrectUniqueConstraintPatch(),
9292
new TPSTableIdPatch(),
93-
new DeleteUrlOpenEventsFromExtensionComponentsPatch()
93+
new DeleteUrlOpenEventsFromExtensionComponentsPatch(),
94+
new CookieTableIPLengthPatch()
9495
};
9596
}
9697
}

Plan/common/src/main/java/com/djrapitops/plan/storage/database/sql/tables/CookieTable.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
*/
3030
public class CookieTable {
3131

32+
public static final int MAX_IP_ADDRESS_LENGTH = 1000;
33+
3234
public static final String TABLE_NAME = "plan_cookies";
3335

3436
public static final String ID = "id";
@@ -64,7 +66,7 @@ public static String createTableSQL(DBType dbType) {
6466
.column(WEB_USERNAME, Sql.varchar(100)).notNull()
6567
.column(EXPIRES, Sql.LONG).notNull()
6668
.column(COOKIE, Sql.varchar(64)).notNull()
67-
.column(IP_ADDRESS, Sql.varchar(45)) // Max IPv6 text length 45 chars
69+
.column(IP_ADDRESS, Sql.varchar(MAX_IP_ADDRESS_LENGTH)) // Max IPv6 text length 45 chars
6870
.toString();
6971
}
7072
}

Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/CookieChangeTransaction.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.sql.PreparedStatement;
2727
import java.sql.SQLException;
2828

29+
import static com.djrapitops.plan.storage.database.sql.tables.CookieTable.MAX_IP_ADDRESS_LENGTH;
30+
2931
public class CookieChangeTransaction extends Transaction {
3032

3133
private final String username;
@@ -102,7 +104,7 @@ public void prepare(PreparedStatement statement) throws SQLException {
102104
statement.setString(1, username);
103105
statement.setString(2, cookie);
104106
statement.setLong(3, expires);
105-
statement.setString(4, ipAddress);
107+
statement.setString(4, StringUtils.truncate(ipAddress, MAX_IP_ADDRESS_LENGTH));
106108
}
107109
});
108110
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of Player Analytics (Plan).
3+
*
4+
* Plan is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License v3 as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Plan is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
package com.djrapitops.plan.storage.database.transactions.patches;
18+
19+
import com.djrapitops.plan.storage.database.DBType;
20+
import com.djrapitops.plan.storage.database.sql.building.Sql;
21+
import com.djrapitops.plan.storage.database.sql.tables.CookieTable;
22+
23+
public class CookieTableIPLengthPatch extends Patch {
24+
@Override
25+
public boolean hasBeenApplied() {
26+
return dbType == DBType.SQLITE || // SQLite does not limit varchar lengths
27+
columnVarcharLength(CookieTable.TABLE_NAME, CookieTable.IP_ADDRESS) >= CookieTable.MAX_IP_ADDRESS_LENGTH;
28+
}
29+
30+
@Override
31+
protected void applyPatch() {
32+
execute("ALTER TABLE " + CookieTable.TABLE_NAME + " MODIFY " + CookieTable.IP_ADDRESS + " " + Sql.varchar(CookieTable.MAX_IP_ADDRESS_LENGTH));
33+
}
34+
}

0 commit comments

Comments
 (0)