From 4e0988ec150f787e6fcce41ea5321b0b4c412ec5 Mon Sep 17 00:00:00 2001 From: Sam Dozor Date: Wed, 27 Apr 2016 17:30:22 -0400 Subject: [PATCH] Increase user attribute value length limit --- build.gradle | 2 +- .../main/java/com/mparticle/MParticle.java | 4 +- .../com/mparticle/internal/Constants.java | 5 +- .../com/mparticle/internal/MPUtility.java | 17 +++-- .../mparticle/internal/MessageManager.java | 1 - .../java/com/mparticle/kits/KitFactory.java | 3 +- .../com/mparticle/internal/MPUtilityTest.java | 65 +++++++++++++++++++ 7 files changed, 82 insertions(+), 15 deletions(-) create mode 100644 sdk/src/test/java/com/mparticle/internal/MPUtilityTest.java diff --git a/build.gradle b/build.gradle index 191eb697e..a3066aa79 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ buildscript { allprojects { group = 'com.mparticle' - version = '4.4.3' + version = '4.4.4' repositories { mavenLocal() jcenter() diff --git a/sdk/src/main/java/com/mparticle/MParticle.java b/sdk/src/main/java/com/mparticle/MParticle.java index 2e5846e18..ca8637f40 100644 --- a/sdk/src/main/java/com/mparticle/MParticle.java +++ b/sdk/src/main/java/com/mparticle/MParticle.java @@ -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); } @@ -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); } diff --git a/sdk/src/main/java/com/mparticle/internal/Constants.java b/sdk/src/main/java/com/mparticle/internal/Constants.java index 3a0ff9aea..8f25950ef 100644 --- a/sdk/src/main/java/com/mparticle/internal/Constants.java +++ b/sdk/src/main/java/com/mparticle/internal/Constants.java @@ -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, diff --git a/sdk/src/main/java/com/mparticle/internal/MPUtility.java b/sdk/src/main/java/com/mparticle/internal/MPUtility.java index bbe32e47d..7e0a99882 100644 --- a/sdk/src/main/java/com/mparticle/internal/MPUtility.java +++ b/sdk/src/main/java/com/mparticle/internal/MPUtility.java @@ -660,15 +660,15 @@ public static JSONObject enforceAttributeConstraints(Map attribu for (Map.Entry 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; } @@ -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); diff --git a/sdk/src/main/java/com/mparticle/internal/MessageManager.java b/sdk/src/main/java/com/mparticle/internal/MessageManager.java index e44f6cc5c..c3e8f6735 100644 --- a/sdk/src/main/java/com/mparticle/internal/MessageManager.java +++ b/sdk/src/main/java/com/mparticle/internal/MessageManager.java @@ -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; diff --git a/sdk/src/main/java/com/mparticle/kits/KitFactory.java b/sdk/src/main/java/com/mparticle/kits/KitFactory.java index eede5a320..18cc36730 100644 --- a/sdk/src/main/java/com/mparticle/kits/KitFactory.java +++ b/sdk/src/main/java/com/mparticle/kits/KitFactory.java @@ -83,8 +83,7 @@ private boolean loadKit(KitManager ekManager, int kitId, String className) { try { Class clazz = Class.forName(className); try { - Constructor constructor = clazz.getDeclaredConstructor(); - constructor.setAccessible(true); + Constructor constructor = clazz.getConstructor(); AbstractKit kit = constructor.newInstance() .setKitManager(ekManager) .setId(kitId); diff --git a/sdk/src/test/java/com/mparticle/internal/MPUtilityTest.java b/sdk/src/test/java/com/mparticle/internal/MPUtilityTest.java new file mode 100644 index 000000000..5ed9acbcc --- /dev/null +++ b/sdk/src/test/java/com/mparticle/internal/MPUtilityTest.java @@ -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")); + + } +} \ No newline at end of file