Skip to content

Commit 1f571a5

Browse files
azorinmsuphp-coder
authored andcommitted
Fix MysqlDataTruncation exception when user requests page with long method name.
Fix #398
1 parent cd6fd8a commit 1f571a5

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/main/java/ru/mystamps/web/Db.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static final class Series {
3333

3434
public static final class SuspiciousActivity {
3535
public static final int PAGE_URL_LENGTH = 100;
36+
public static final int METHOD_LENGTH = 7;
3637
public static final int REFERER_PAGE_LENGTH = 255;
3738
public static final int USER_AGENT_LENGTH = 255;
3839
}

src/main/java/ru/mystamps/web/service/SiteServiceImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ protected void logEvent(
144144
activity.setType(type);
145145
activity.setOccurredAt(date == null ? new Date() : date);
146146
activity.setPage(abbreviatePage(page));
147-
activity.setMethod(method);
147+
activity.setMethod(abbreviateMethod(method));
148148
activity.setUserId(userId);
149149
activity.setIp(StringUtils.defaultString(ip));
150150
activity.setRefererPage(StringUtils.stripToNull(abbreviateRefererPage(referer)));
@@ -153,6 +153,16 @@ protected void logEvent(
153153
suspiciousActivities.add(activity);
154154
}
155155

156+
/**
157+
* Abbreviate name of HTTP method.
158+
* @param method name of HTTP method
159+
* @return name of the method as-is or its abbreviation with three points at the end
160+
* @author Aleksandr Zorin
161+
*/
162+
private static String abbreviateMethod(String method) {
163+
return abbreviateIfLengthGreaterThan(method, Db.SuspiciousActivity.METHOD_LENGTH, "method");
164+
}
165+
156166
private static String abbreviatePage(String page) {
157167
return abbreviateIfLengthGreaterThan(page, Db.SuspiciousActivity.PAGE_URL_LENGTH, "page");
158168
}

src/test/groovy/ru/mystamps/web/service/SiteServiceImplTest.groovy

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,20 @@ class SiteServiceImplTest extends Specification {
201201
'OPTIONS' | _
202202
null | _
203203
}
204+
205+
def "logEvent() should pass abbreviated method when it's too long"() {
206+
given:
207+
String method = "PROPFIND"
208+
and:
209+
String exceptedMethod = method.take(Db.SuspiciousActivity.METHOD_LENGTH-3) + '...'
210+
when:
211+
serviceImpl.logEvent(TEST_TYPE, TEST_PAGE, method, null, null, null, TEST_USER_AGENT, null)
212+
then:
213+
1 * suspiciousActivityDao.add({ AddSuspiciousActivityDbDto activity ->
214+
assert activity?.method == exceptedMethod
215+
return true
216+
})
217+
}
204218

205219
def "logEvent() should pass null to dao for unknown user id"() {
206220
when:

0 commit comments

Comments
 (0)