Skip to content

Commit c694a19

Browse files
committed
moved the attribute validator to the BlockAttributeValidator class to try to address concerns.
1 parent 47ff808 commit c694a19

File tree

6 files changed

+73
-70
lines changed

6 files changed

+73
-70
lines changed

Agent/Analytics/BlockAttributeValidator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ NS_ASSUME_NONNULL_BEGIN
2222
valueValidator:(ValueValidator)valueValidator
2323
andEventTypeValidator:(EventTypeValidator)eventTypeValidator;
2424

25+
+ (BlockAttributeValidator *) attributeValidator;
26+
2527
@end
2628

2729
NS_ASSUME_NONNULL_END

Agent/Analytics/BlockAttributeValidator.m

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
//
88

99
#import "BlockAttributeValidator.h"
10+
#import "Constants.h"
11+
#import "NRMAAnalytics.h"
12+
#import "NRLogger.h"
1013

1114
@implementation BlockAttributeValidator
1215

@@ -35,4 +38,64 @@ - (BOOL)valueValidator:(id)value {
3538
return self.valueValidator(value);
3639
}
3740

41+
static BlockAttributeValidator *_attributeValidator;
42+
+ (BlockAttributeValidator *) attributeValidator
43+
{
44+
static dispatch_once_t onceToken;
45+
dispatch_once(&onceToken, ^{
46+
_attributeValidator = [[BlockAttributeValidator alloc] initWithNameValidator:^BOOL(NSString *name) {
47+
if ([name length] == 0) {
48+
NRLOG_AGENT_ERROR(@"invalid attribute: name length = 0");
49+
return false;
50+
}
51+
if ([name hasPrefix:@" "]) {
52+
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix = \" \"");
53+
return false;
54+
}
55+
// check if attribute name is reserved or attribute name matches reserved prefix.
56+
for (NSString* key in [NRMAAnalytics reservedKeywords]) {
57+
if ([key isEqualToString:name]) {
58+
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
59+
return false;
60+
}
61+
}
62+
for (NSString* key in [NRMAAnalytics reservedPrefixes]) {
63+
if ([name hasPrefix:key]) {
64+
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
65+
return false;
66+
}
67+
}
68+
69+
// check if attribute name exceeds max length.
70+
if ([name length] > kNRMA_Attrib_Max_Name_Length) {
71+
NRLOG_AGENT_ERROR(@"invalid attribute: name length exceeds limit");
72+
return false;
73+
}
74+
return true;
75+
76+
} valueValidator:^BOOL(id value) {
77+
if ([value isKindOfClass:[NSString class]]) {
78+
if ([(NSString*)value length] == 0) {
79+
NRLOG_AGENT_ERROR(@"invalid attribute: value length = 0");
80+
return false;
81+
}
82+
else if ([(NSString*)value length] >= kNRMA_Attrib_Max_Value_Size_Bytes) {
83+
NRLOG_AGENT_ERROR(@"invalid attribute: value exceeded maximum byte size exceeded");
84+
return false;
85+
}
86+
}
87+
if (value == nil || [value isKindOfClass:[NSNull class]]) {
88+
NRLOG_AGENT_ERROR(@"invalid attribute: value cannot be nil");
89+
return false;
90+
}
91+
92+
return true;
93+
} andEventTypeValidator:^BOOL(NSString *eventType) {
94+
return YES;
95+
}];
96+
});
97+
98+
return _attributeValidator;
99+
}
100+
38101
@end

Agent/Analytics/NRMAAnalytics.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ - (id) initWithSessionStartTimeMS:(long long) sessionStartTime {
116116
PersistentEventStore *eventStore = [[PersistentEventStore alloc] initWithFilename:filename andMinimumDelay:.025];
117117

118118
_eventManager = [[NRMAEventManager alloc] initWithPersistentStore:eventStore];
119-
_attributeValidator = [NewRelicInternalUtils attributeValidator];
119+
_attributeValidator = [BlockAttributeValidator attributeValidator];
120120
_sessionAttributeManager = [[NRMASAM alloc] initWithAttributeValidator:_attributeValidator];
121121

122122
NSString* attributes = [self sessionAttributeJSONString];

Agent/HandledException/NRMAHandledExceptions.mm

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#import "NRMABool.h"
2323
#import "NRMASupportMetricHelper.h"
2424
#import "Constants.h"
25+
#import "BlockAttributeValidator.h"
2526

2627
@interface NRMAAnalytics(Protected)
2728
// Because the NRMAAnalytics class interfaces with non Objective-C++ files, we cannot expose the API on the header. Therefore, we must use this reference.
@@ -198,7 +199,7 @@ - (void) recordError:(NSError * _Nonnull)error
198199
resultMap,
199200
[self createThreadVector:callstack length:frames]
200201
);
201-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
202+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[BlockAttributeValidator attributeValidator]] autorelease];
202203

203204
if (attributes != nil) {
204205
[contextAdapter addAttributesNewValidation:attributes];
@@ -218,7 +219,7 @@ - (void) recordError:(NSError * _Nonnull)error
218219
[self createThreadVector:callstack length:frames]
219220
);
220221

221-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
222+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[BlockAttributeValidator attributeValidator]] autorelease];
222223

223224
if (attributes != nil) {
224225
[contextAdapter addAttributes:attributes];
@@ -268,7 +269,7 @@ - (void) recordHandledException:(NSException*)exception
268269

269270
[self checkOffline:report];
270271

271-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
272+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[BlockAttributeValidator attributeValidator]] autorelease];
272273

273274
if (attributes != nil) {
274275
[contextAdapter addAttributesNewValidation:attributes];
@@ -287,7 +288,7 @@ - (void) recordHandledException:(NSException*)exception
287288

288289
[self checkOffline:report];
289290

290-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
291+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[BlockAttributeValidator attributeValidator]] autorelease];
291292

292293
if (attributes != nil) {
293294
[contextAdapter addAttributes:attributes];
@@ -371,7 +372,7 @@ - (void) recordHandledExceptionWithStackTrace:(NSDictionary*)exceptionDictionary
371372
report->setAttributeNoValidation("timeSinceLoad", [[[NSDate new] autorelease] timeIntervalSinceDate:self.sessionStartDate]);
372373
[self checkOffline:report];
373374

374-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
375+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[BlockAttributeValidator attributeValidator]] autorelease];
375376

376377
if (exceptionDictionary != nil) {
377378
[contextAdapter addAttributesNewValidation:exceptionDictionary];
@@ -388,7 +389,7 @@ - (void) recordHandledExceptionWithStackTrace:(NSDictionary*)exceptionDictionary
388389
report->setAttribute("timeSinceLoad", [[[NSDate new] autorelease] timeIntervalSinceDate:self.sessionStartDate]);
389390
[self checkOffline:report];
390391

391-
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];
392+
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[BlockAttributeValidator attributeValidator]] autorelease];
392393

393394
if (exceptionDictionary != nil) {
394395
[contextAdapter addAttributes:exceptionDictionary];

Agent/Utilities/NewRelicInternalUtils.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#import "NRMAReachability.h"
1010
#import "NRConstants.h"
1111
#import "NRMANetworkMonitor.h"
12-
#import "BlockAttributeValidator.h"
1312

1413
#if __LP64__
1514
#define NRMA_NSI "ld"
@@ -105,7 +104,5 @@ NSTimeInterval NRMAMillisecondTimestamp(void);
105104

106105
+ (NRMAReachability*) reachability;
107106

108-
+ (id<AttributeValidatorProtocol>) attributeValidator;
109-
110107
@end
111108

Agent/Utilities/NewRelicInternalUtils.m

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -689,64 +689,4 @@ + (BOOL) isSimulator {
689689
return simulator != nil;
690690
}
691691

692-
static id<AttributeValidatorProtocol> _attributeValidator;
693-
+ (id<AttributeValidatorProtocol>) attributeValidator
694-
{
695-
static dispatch_once_t onceToken;
696-
dispatch_once(&onceToken, ^{
697-
_attributeValidator = [[BlockAttributeValidator alloc] initWithNameValidator:^BOOL(NSString *name) {
698-
if ([name length] == 0) {
699-
NRLOG_AGENT_ERROR(@"invalid attribute: name length = 0");
700-
return false;
701-
}
702-
if ([name hasPrefix:@" "]) {
703-
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix = \" \"");
704-
return false;
705-
}
706-
// check if attribute name is reserved or attribute name matches reserved prefix.
707-
for (NSString* key in [NRMAAnalytics reservedKeywords]) {
708-
if ([key isEqualToString:name]) {
709-
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
710-
return false;
711-
}
712-
}
713-
for (NSString* key in [NRMAAnalytics reservedPrefixes]) {
714-
if ([name hasPrefix:key]) {
715-
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
716-
return false;
717-
}
718-
}
719-
720-
// check if attribute name exceeds max length.
721-
if ([name length] > kNRMA_Attrib_Max_Name_Length) {
722-
NRLOG_AGENT_ERROR(@"invalid attribute: name length exceeds limit");
723-
return false;
724-
}
725-
return true;
726-
727-
} valueValidator:^BOOL(id value) {
728-
if ([value isKindOfClass:[NSString class]]) {
729-
if ([(NSString*)value length] == 0) {
730-
NRLOG_AGENT_ERROR(@"invalid attribute: value length = 0");
731-
return false;
732-
}
733-
else if ([(NSString*)value length] >= kNRMA_Attrib_Max_Value_Size_Bytes) {
734-
NRLOG_AGENT_ERROR(@"invalid attribute: value exceeded maximum byte size exceeded");
735-
return false;
736-
}
737-
}
738-
if (value == nil || [value isKindOfClass:[NSNull class]]) {
739-
NRLOG_AGENT_ERROR(@"invalid attribute: value cannot be nil");
740-
return false;
741-
}
742-
743-
return true;
744-
} andEventTypeValidator:^BOOL(NSString *eventType) {
745-
return YES;
746-
}];
747-
});
748-
749-
return _attributeValidator;
750-
}
751-
752692
@end

0 commit comments

Comments
 (0)