Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use character set instead of regex for string sanitizer #46

Merged
merged 6 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Demo/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ platform :ios, '8.0'

target 'librato-iOS Demo' do
pod 'librato-iOS', :path => '../librato-iOS.podspec'

target 'librato-iOS DemoTests' do
inherit! :search_paths
end
end
64 changes: 63 additions & 1 deletion Demo/librato-iOS Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0D6D45DA41B71E92FB0B9E41 /* libPods-librato-iOS DemoTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C644C1E73D9E5B07F26ABA47 /* libPods-librato-iOS DemoTests.a */; };
6692261E180358AF00237E77 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6692261D180358AF00237E77 /* Foundation.framework */; };
66922620180358AF00237E77 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6692261F180358AF00237E77 /* CoreGraphics.framework */; };
66922622180358AF00237E77 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 66922621180358AF00237E77 /* UIKit.framework */; };
Expand All @@ -21,6 +22,7 @@
66922643180358B000237E77 /* librato_iOS_DemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 66922642180358B000237E77 /* librato_iOS_DemoTests.m */; };
6692264E18035E8400237E77 /* LibratoDemoEventTracker.m in Sources */ = {isa = PBXBuildFile; fileRef = 6692264D18035E8400237E77 /* LibratoDemoEventTracker.m */; };
669C48111D8764040059EDBD /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 669C48101D8764040059EDBD /* MainStoryboard.storyboard */; };
D953BD3F1DBAA49B005EEF1B /* NSString+ALMSanitizedForMetricTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D9263DD61DB946660035CC6E /* NSString+ALMSanitizedForMetricTests.m */; };
E83CC217E62447107D693D91 /* libPods-librato-iOS Demo.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F8E7DC6EE1A84E81B6B7B7A2 /* libPods-librato-iOS Demo.a */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -55,8 +57,12 @@
6692264D18035E8400237E77 /* LibratoDemoEventTracker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LibratoDemoEventTracker.m; sourceTree = "<group>"; };
669C48101D8764040059EDBD /* MainStoryboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = MainStoryboard.storyboard; sourceTree = "<group>"; };
71FE433FB985CA1AB0DF6BDA /* Pods-librato-iOS Demo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-librato-iOS Demo.debug.xcconfig"; path = "Pods/Target Support Files/Pods-librato-iOS Demo/Pods-librato-iOS Demo.debug.xcconfig"; sourceTree = "<group>"; };
801077638741FA5C51076C77 /* Pods-librato-iOS DemoTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-librato-iOS DemoTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-librato-iOS DemoTests/Pods-librato-iOS DemoTests.release.xcconfig"; sourceTree = "<group>"; };
8E8188DE6A6BC172BA811B1C /* Pods-librato-iOS DemoTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-librato-iOS DemoTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-librato-iOS DemoTests/Pods-librato-iOS DemoTests.debug.xcconfig"; sourceTree = "<group>"; };
B3CFB32942D9D0C3245EEB4E /* Pods-librato-iOS Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-librato-iOS Demo.release.xcconfig"; path = "Pods/Target Support Files/Pods-librato-iOS Demo/Pods-librato-iOS Demo.release.xcconfig"; sourceTree = "<group>"; };
C5A2D3C7197B48C6A9ACC07D /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
C644C1E73D9E5B07F26ABA47 /* libPods-librato-iOS DemoTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-librato-iOS DemoTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
D9263DD61DB946660035CC6E /* NSString+ALMSanitizedForMetricTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+ALMSanitizedForMetricTests.m"; sourceTree = "<group>"; };
F8E7DC6EE1A84E81B6B7B7A2 /* libPods-librato-iOS Demo.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-librato-iOS Demo.a"; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

Expand All @@ -79,6 +85,7 @@
66922637180358AF00237E77 /* XCTest.framework in Frameworks */,
66922639180358B000237E77 /* UIKit.framework in Frameworks */,
66922638180358AF00237E77 /* Foundation.framework in Frameworks */,
0D6D45DA41B71E92FB0B9E41 /* libPods-librato-iOS DemoTests.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -114,6 +121,7 @@
66922636180358AF00237E77 /* XCTest.framework */,
C5A2D3C7197B48C6A9ACC07D /* libPods.a */,
F8E7DC6EE1A84E81B6B7B7A2 /* libPods-librato-iOS Demo.a */,
C644C1E73D9E5B07F26ABA47 /* libPods-librato-iOS DemoTests.a */,
);
name = Frameworks;
sourceTree = "<group>";
Expand Down Expand Up @@ -147,6 +155,7 @@
isa = PBXGroup;
children = (
66922642180358B000237E77 /* librato_iOS_DemoTests.m */,
D9263DD61DB946660035CC6E /* NSString+ALMSanitizedForMetricTests.m */,
6692263D180358B000237E77 /* Supporting Files */,
);
path = "librato-iOS DemoTests";
Expand All @@ -166,6 +175,8 @@
children = (
71FE433FB985CA1AB0DF6BDA /* Pods-librato-iOS Demo.debug.xcconfig */,
B3CFB32942D9D0C3245EEB4E /* Pods-librato-iOS Demo.release.xcconfig */,
8E8188DE6A6BC172BA811B1C /* Pods-librato-iOS DemoTests.debug.xcconfig */,
801077638741FA5C51076C77 /* Pods-librato-iOS DemoTests.release.xcconfig */,
);
name = Pods;
sourceTree = "<group>";
Expand Down Expand Up @@ -197,9 +208,12 @@
isa = PBXNativeTarget;
buildConfigurationList = 66922649180358B000237E77 /* Build configuration list for PBXNativeTarget "librato-iOS DemoTests" */;
buildPhases = (
60F4CC329F21EB0D532804DC /* [CP] Check Pods Manifest.lock */,
66922631180358AF00237E77 /* Sources */,
66922632180358AF00237E77 /* Frameworks */,
66922633180358AF00237E77 /* Resources */,
BAACFE891AE2BDA71802DAB9 /* [CP] Embed Pods Frameworks */,
B8F1461E083D696A5571BDE5 /* [CP] Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -293,7 +307,52 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
60F4CC329F21EB0D532804DC /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Check Pods Manifest.lock";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
B8F1461E083D696A5571BDE5 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-librato-iOS DemoTests/Pods-librato-iOS DemoTests-resources.sh\"\n";
showEnvVarsInLog = 0;
};
BAACFE891AE2BDA71802DAB9 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-librato-iOS DemoTests/Pods-librato-iOS DemoTests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
CF311E2286F14B79A814F7BE /* [CP] Copy Pods Resources */ = {
Expand Down Expand Up @@ -328,6 +387,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D953BD3F1DBAA49B005EEF1B /* NSString+ALMSanitizedForMetricTests.m in Sources */,
66922643180358B000237E77 /* librato_iOS_DemoTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -479,6 +539,7 @@
};
6692264A180358B000237E77 /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 8E8188DE6A6BC172BA811B1C /* Pods-librato-iOS DemoTests.debug.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/librato-iOS Demo.app/librato-iOS Demo";
FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -501,6 +562,7 @@
};
6692264B180358B000237E77 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 801077638741FA5C51076C77 /* Pods-librato-iOS DemoTests.release.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(BUILT_PRODUCTS_DIR)/librato-iOS Demo.app/librato-iOS Demo";
FRAMEWORK_SEARCH_PATHS = (
Expand Down
36 changes: 36 additions & 0 deletions Demo/librato-iOS DemoTests/NSString+ALMSanitizedForMetricTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// NSString+SanitizedForMetricTests.m
// librato-iOS
//
// Created by Sergey Kuryanov on 20.10.16.
// Copyright © 2016 Amco International Education Services, LLC. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "NSString+ALMSanitizedForMetric.h"

@interface NSString_ALMSanitizedForMetricTests : XCTestCase
@property (nonatomic, strong) NSString *testString;
@end

@implementation NSString_ALMSanitizedForMetricTests

- (void)setUp {
[super setUp];

self.testString = @"A-Za-z0-9.:-_<>{}[];\'\"!@#$%^&*()_+=œ∑´®†¥¨ˆøπ“‘«åß∂ƒ©˙∆˚¬…æΩ≈ç√∫˜µ≤≥÷";
}

- (void)tearDown {
self.testString = nil;

[super tearDown];
}

- (void)testThatOnlyAllowedCharactersPresent {
NSString *expectedString = @"A-Za-z0-9.:-_-------------------_------------------------------------";

XCTAssertEqualObjects(self.testString.alm_sanitizedForMetric, expectedString);
}

@end
4 changes: 0 additions & 4 deletions Demo/librato-iOS DemoTests/librato_iOS_DemoTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,5 @@ - (void)tearDown
[super tearDown];
}

- (void)testExample
{
XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__);
}

@end
12 changes: 6 additions & 6 deletions librato-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
6692259917FE44B800237E77 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6692258117FE44B800237E77 /* UIKit.framework */; };
669225A117FE44B800237E77 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6692259F17FE44B800237E77 /* InfoPlist.strings */; };
669225A317FE44B800237E77 /* librato_iOSTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 669225A217FE44B800237E77 /* librato_iOSTests.m */; };
669225B117FE457800237E77 /* NSString+SanitizedForMetric.m in Sources */ = {isa = PBXBuildFile; fileRef = 669225B017FE457800237E77 /* NSString+SanitizedForMetric.m */; };
669225B117FE457800237E77 /* NSString+ALMSanitizedForMetric.m in Sources */ = {isa = PBXBuildFile; fileRef = 669225B017FE457800237E77 /* NSString+ALMSanitizedForMetric.m */; };
669225C017FE458100237E77 /* LibratoClient.m in Sources */ = {isa = PBXBuildFile; fileRef = 669225B317FE458100237E77 /* LibratoClient.m */; };
669225C117FE458100237E77 /* LibratoConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = 669225B517FE458100237E77 /* LibratoConnection.m */; };
669225C217FE458100237E77 /* LibratoDirectPersister.m in Sources */ = {isa = PBXBuildFile; fileRef = 669225B717FE458100237E77 /* LibratoDirectPersister.m */; };
Expand Down Expand Up @@ -57,8 +57,8 @@
6692259E17FE44B800237E77 /* librato-iOSTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "librato-iOSTests-Info.plist"; sourceTree = "<group>"; };
669225A017FE44B800237E77 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
669225A217FE44B800237E77 /* librato_iOSTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = librato_iOSTests.m; sourceTree = "<group>"; };
669225AF17FE457800237E77 /* NSString+SanitizedForMetric.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+SanitizedForMetric.h"; sourceTree = "<group>"; };
669225B017FE457800237E77 /* NSString+SanitizedForMetric.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+SanitizedForMetric.m"; sourceTree = "<group>"; };
669225AF17FE457800237E77 /* NSString+ALMSanitizedForMetric.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSString+ALMSanitizedForMetric.h"; sourceTree = "<group>"; };
669225B017FE457800237E77 /* NSString+ALMSanitizedForMetric.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSString+ALMSanitizedForMetric.m"; sourceTree = "<group>"; };
669225B217FE458100237E77 /* LibratoClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LibratoClient.h; sourceTree = "<group>"; };
669225B317FE458100237E77 /* LibratoClient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LibratoClient.m; sourceTree = "<group>"; };
669225B417FE458100237E77 /* LibratoConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LibratoConnection.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -184,8 +184,8 @@
669225AC17FE454800237E77 /* Categories */ = {
isa = PBXGroup;
children = (
669225AF17FE457800237E77 /* NSString+SanitizedForMetric.h */,
669225B017FE457800237E77 /* NSString+SanitizedForMetric.m */,
669225AF17FE457800237E77 /* NSString+ALMSanitizedForMetric.h */,
669225B017FE457800237E77 /* NSString+ALMSanitizedForMetric.m */,
);
path = Categories;
sourceTree = "<group>";
Expand Down Expand Up @@ -335,7 +335,7 @@
669225C417FE458100237E77 /* LibratoProcessor.m in Sources */,
669225C517FE458100237E77 /* LibratoQueue.m in Sources */,
6692260A17FF3C2A00237E77 /* Librato.m in Sources */,
669225B117FE457800237E77 /* NSString+SanitizedForMetric.m in Sources */,
669225B117FE457800237E77 /* NSString+ALMSanitizedForMetric.m in Sources */,
669225C617FE458100237E77 /* LibratoVersion.m in Sources */,
669225CC17FE458800237E77 /* LibratoMetric.m in Sources */,
669225C017FE458100237E77 /* LibratoClient.m in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// NSString+SanitizedForMetric.h
// NSString+ALMSanitizedForMetric.h
// Librato-iOS
//
// Created by Adam Yanalunas on 10/3/13.
Expand All @@ -8,8 +8,8 @@

#import <Foundation/Foundation.h>

@interface NSString (SanitizedForMetric)
@interface NSString (ALMSanitizedForMetric)

- (NSString *)sanitizedForMetric;
- (NSString *)alm_sanitizedForMetric;

@end
21 changes: 21 additions & 0 deletions librato-iOS/Categories/NSString+ALMSanitizedForMetric.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// NSString+ALMSanitizedForMetric.m
// Librato-iOS
//
// Created by Adam Yanalunas on 10/3/13.
// Copyright (c) 2013 Amco International Education Services, LLC. All rights reserved.
//

#import "NSString+ALMSanitizedForMetric.h"

@implementation NSString (ALMSanitizedForMetric)

- (NSString *)alm_sanitizedForMetric
{
NSCharacterSet *allowedSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.:-_"];
NSString *cleaned = [[self componentsSeparatedByCharactersInSet:allowedSet.invertedSet] componentsJoinedByString:@"-"];
return [cleaned substringToIndex:(self.length < 255 ? self.length : 255)];
}


@end
21 changes: 0 additions & 21 deletions librato-iOS/Categories/NSString+SanitizedForMetric.m

This file was deleted.

10 changes: 5 additions & 5 deletions librato-iOS/Metrics/LibratoMetric.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "LibratoMetric.h"
#import "NSString+SanitizedForMetric.h"
#import "NSString+ALMSanitizedForMetric.h"
#import "MTLValueTransformer.h"

NSString *const LibratoMetricMeasureTimeKey = @"measure_time";
Expand Down Expand Up @@ -81,19 +81,19 @@ + (NSValueTransformer *)nameJSONTransformer
{
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^id(NSString *name) {
NSAssert(name.length > 0, @"Measurements must be named");
return name.sanitizedForMetric;
return name.alm_sanitizedForMetric;
} reverseBlock:^id(NSString *name) {
return name.sanitizedForMetric;
return name.alm_sanitizedForMetric;
}];
}


+ (NSValueTransformer *)sourceJSONTransformer
{
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^id(NSString *source) {
return source.sanitizedForMetric;
return source.alm_sanitizedForMetric;
} reverseBlock:^id(NSString *source) {
return (source.length ? source.sanitizedForMetric : nil);
return (source.length ? source.alm_sanitizedForMetric : nil);
}];
}

Expand Down