LP#1652382: handle cases where supplied key is longer than 250 bytes
authorGalen Charlton <gmc@equinoxinitiative.org>
Wed, 15 Feb 2017 19:12:34 +0000 (14:12 -0500)
committerGalen Charlton <gmc@equinoxinitiative.org>
Thu, 16 Feb 2017 19:54:58 +0000 (14:54 -0500)
With this patch, if cache clients want to use a key longer
than the memcached text protocol limit of 250 bytes, the
key is normalized to 'shortened_' + md5_hex(normalized_key).

Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Signed-off-by: Jeff Davis <jdavis@sitka.bclibraries.ca>

src/libopensrf/osrf_cache.c
src/perl/lib/OpenSRF/Utils/Cache.pm

index 4fccfd0..dd489e1 100644 (file)
@@ -15,6 +15,8 @@ GNU General Public License for more details.
 
 #include <opensrf/osrf_cache.h>
 
+#define MAX_KEY_LEN 250
+
 static struct memcached_st* _osrfCache = NULL;
 static time_t _osrfCacheMaxSeconds = -1;
 static char* _clean_key( const char* );
@@ -57,7 +59,14 @@ char* _clean_key( const char* key ) {
     char* clean_key = (char*)strdup(key);
     char* d = clean_key;
     char* s = clean_key;
-    do while(isspace(*s)) s++; while(*d++ = *s++);
+    do while(isspace(*s) || iscntrl(*s)) s++; while(*d++ = *s++);
+    if (strlen(clean_key) > MAX_KEY_LEN) {
+        char *hashed = md5sum(clean_key);
+        clean_key[0] = '\0';
+        strncat(clean_key, "shortened_", 11);
+        strncat(clean_key, hashed, MAX_KEY_LEN);
+        free(hashed);
+    }
     return clean_key;
 }
 
index ba9f1a1..36721d9 100644 (file)
@@ -2,6 +2,7 @@ package OpenSRF::Utils::Cache;
 use strict; use warnings;
 use base qw/OpenSRF/;
 use Cache::Memcached;
+use Digest::MD5 qw(md5_hex);
 use OpenSRF::Utils::Logger qw/:level/;
 use OpenSRF::Utils::Config;
 use OpenSRF::Utils::SettingsClient;
@@ -281,6 +282,9 @@ sub _clean_cache_key {
     my $key = shift;
 
     $key =~ s{(\p{Cntrl}|\s)}{}g;
+    if (length($key) > 250) { # max length of memcahed key
+        $key = 'shortened_' . md5_hex($key);
+    }
 
     return $key;
 }