Skip to content

Commit

Permalink
Increase user attribute value length limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Dozor committed Apr 27, 2016
1 parent 6c0932b commit 4e0988e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {

allprojects {
group = 'com.mparticle'
version = '4.4.3'
version = '4.4.4'
repositories {
mavenLocal()
jcenter()
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/main/java/com/mparticle/MParticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ public void setUserAttribute(String key, Object value) {
ConfigManager.log(LogLevel.DEBUG, "Set user attribute: " + key);
}

if (MPUtility.setCheckedAttribute(mUserAttributes, key, value, false)) {
if (MPUtility.setCheckedAttribute(mUserAttributes, key, value, false, true)) {
mPreferences.edit().putString(PrefKeys.USER_ATTRS + mApiKey, mUserAttributes.toString()).apply();
mKitManager.setUserAttributes(mUserAttributes);
}
Expand All @@ -962,7 +962,7 @@ public void incrementUserAttribute(String key, int value) {
}
ConfigManager.log(LogLevel.DEBUG, "Incrementing user attribute: " + key + " with value " + value);

if (MPUtility.setCheckedAttribute(mUserAttributes, key, value, true)) {
if (MPUtility.setCheckedAttribute(mUserAttributes, key, value, true, true)) {
mPreferences.edit().putString(PrefKeys.USER_ATTRS + mApiKey, mUserAttributes.toString()).apply();
mKitManager.setUserAttributes(mUserAttributes);
}
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/main/java/com/mparticle/internal/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ public class Constants {
public static final String BAGS_FILE = "mParticlePrefs_productbags";

public static final int LIMIT_ATTR_COUNT = 100;
public static final int LIMIT_ATTR_NAME = 255;
public static final int LIMIT_ATTR_VALUE = 255;
public static final int LIMIT_ATTR_NAME = 256;
public static final int LIMIT_ATTR_VALUE = 256;
public static final int LIMIT_USER_ATTR_VALUE = 4096;
public static final int LIMIT_NAME = 255;
public static final byte[] LICENSE_CHECK_SALT = new byte[]{
-46, 65, 30, -128, -103, -57, 74, 10, 51, 88, -95, -45, -43, -117, -36, 99, -11, 32, -64,
Expand Down
17 changes: 10 additions & 7 deletions sdk/src/main/java/com/mparticle/internal/MPUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,15 +660,15 @@ public static JSONObject enforceAttributeConstraints(Map<String, String> attribu
for (Map.Entry<String, String> entry : attributes.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
setCheckedAttribute(checkedAttributes, key, value, false);
setCheckedAttribute(checkedAttributes, key, value, false, false);
}
return checkedAttributes;
}
public static Boolean setCheckedAttribute(JSONObject attributes, String key, Object value, boolean increment) {
return setCheckedAttribute(attributes, key, value, false, increment);
public static Boolean setCheckedAttribute(JSONObject attributes, String key, Object value, boolean increment, boolean userAttribute) {
return setCheckedAttribute(attributes, key, value, false, increment, userAttribute);
}

public static Boolean setCheckedAttribute(JSONObject attributes, String key, Object value, Boolean caseInsensitive, boolean increment) {
public static Boolean setCheckedAttribute(JSONObject attributes, String key, Object value, Boolean caseInsensitive, boolean increment, boolean userAttribute) {
if (null == attributes || null == key) {
return false;
}
Expand All @@ -681,9 +681,12 @@ public static Boolean setCheckedAttribute(JSONObject attributes, String key, Obj
ConfigManager.log(LogLevel.ERROR, "Attribute count exceeds limit. Discarding attribute: " + key);
return false;
}
if (null != value && value.toString().length() > Constants.LIMIT_ATTR_VALUE) {
ConfigManager.log(LogLevel.ERROR, "Attribute value length exceeds limit. Discarding attribute: " + key);
return false;
if (value != null) {
String stringValue = value.toString();
if ((userAttribute && stringValue.length() > Constants.LIMIT_USER_ATTR_VALUE) || (!userAttribute && stringValue.length() > Constants.LIMIT_ATTR_VALUE) ){
ConfigManager.log(LogLevel.ERROR, "Attribute value length exceeds limit. Discarding attribute: " + key);
return false;
}
}
if (key.length() > Constants.LIMIT_ATTR_NAME) {
ConfigManager.log(LogLevel.ERROR, "Attribute name length exceeds limit. Discarding attribute: " + key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ public MPMessage logEvent(CommerceEvent event) {
try {
MPMessage message = new MPMessage.Builder(event, mAppStateManager.getSession(), mLocation)
.timestamp(mAppStateManager.getSession().mLastEventTime)
.attributes(MPUtility.enforceAttributeConstraints(event.getCustomAttributes()))
.build();
mMessageHandler.sendMessage(mMessageHandler.obtainMessage(MessageHandler.STORE_MESSAGE, message));
return message;
Expand Down
3 changes: 1 addition & 2 deletions sdk/src/main/java/com/mparticle/kits/KitFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ private boolean loadKit(KitManager ekManager, int kitId, String className) {
try {
Class clazz = Class.forName(className);
try {
Constructor<AbstractKit> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
Constructor<AbstractKit> constructor = clazz.getConstructor();
AbstractKit kit = constructor.newInstance()
.setKitManager(ekManager)
.setId(kitId);
Expand Down
65 changes: 65 additions & 0 deletions sdk/src/test/java/com/mparticle/internal/MPUtilityTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.mparticle.internal;

import org.json.JSONObject;
import org.junit.Test;

import static org.junit.Assert.*;

public class MPUtilityTest {

@Test
public void testSetCheckedAttribute() throws Exception {
JSONObject attributes = new JSONObject();

MPUtility.setCheckedAttribute(attributes, "some key", "some value", false, true);
assertEquals("some value", attributes.getString("some key"));


MPUtility.setCheckedAttribute(attributes, "some key 2", "some value 2", false, false);
assertEquals("some value 2", attributes.getString("some key 2"));

}

@Test
public void testSetKeyThatsTooLong() throws Exception {
JSONObject attributes = new JSONObject();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 257; i++) {
builder.append("a");
}
String keyThatsTooLong = builder.toString();
MPUtility.setCheckedAttribute(attributes, keyThatsTooLong, "some value 2", false, true);

assertFalse(attributes.has(keyThatsTooLong));

}


@Test
public void testSetValueThatsTooLong() throws Exception {
JSONObject attributes = new JSONObject();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 257; i++) {
builder.append("a");
}
String valueThatsTooLong = builder.toString();
MPUtility.setCheckedAttribute(attributes, "mykey", valueThatsTooLong, false, false);

assertFalse(attributes.has("mykey"));

}

@Test
public void testSetUserValueThatsTooLong() throws Exception {
JSONObject attributes = new JSONObject();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 4097; i++) {
builder.append("a");
}
String valueThatsTooLong = builder.toString();
MPUtility.setCheckedAttribute(attributes, "mykey", valueThatsTooLong, false, true);

assertFalse(attributes.has("mykey"));

}
}

0 comments on commit 4e0988e

Please sign in to comment.