Skip to content

Commit 5e5ffa6

Browse files
author
Shun Fan
committed
Fix issue with truncating recorded ip addresses in datastore
1 parent 486352c commit 5e5ffa6

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

managed_vms/datastore/src/main/java/com/example/managedvms/datastore/DatastoreServlet.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc
5151
String userIp = req.getRemoteAddr();
5252
InetAddress address = InetAddress.getByName(userIp);
5353
if (address instanceof Inet6Address) {
54-
userIp = userIp.substring(0, userIp.indexOf(":", 2)) + ":*:*:*:*:*:*";
54+
// nest indexOf calls to find the second occurrence of a character in a string
55+
// an alternative is to use Apache Commons Lang: StringUtils.ordinalIndexOf()
56+
userIp = userIp.substring(0, userIp.indexOf(":", userIp.indexOf(":") + 1)) + ":*:*:*:*:*:*";
5557
} else if (address instanceof Inet4Address) {
56-
userIp = userIp.substring(0, userIp.indexOf(".", 2)) + ".*.*";
58+
userIp = userIp.substring(0, userIp.indexOf(".", userIp.indexOf(".") + 1)) + ".*.*";
5759
}
5860

5961
Datastore datastore = DatastoreOptions.defaultInstance().service();

0 commit comments

Comments
 (0)