Skip to content

Commit

Permalink
NR-359870 moved the attribute validator to utils so it can be static …
Browse files Browse the repository at this point in the history
…and used anywhere it's needed
  • Loading branch information
mbruin-NR committed Jan 22, 2025
1 parent 9b5f166 commit 47ff808
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 58 deletions.
53 changes: 1 addition & 52 deletions Agent/Analytics/NRMAAnalytics.mm
Original file line number Diff line number Diff line change
Expand Up @@ -116,59 +116,9 @@ - (id) initWithSessionStartTimeMS:(long long) sessionStartTime {
PersistentEventStore *eventStore = [[PersistentEventStore alloc] initWithFilename:filename andMinimumDelay:.025];

_eventManager = [[NRMAEventManager alloc] initWithPersistentStore:eventStore];
_attributeValidator = [[BlockAttributeValidator alloc] initWithNameValidator:^BOOL(NSString *name) {
if ([name length] == 0) {
NRLOG_AGENT_ERROR(@"invalid attribute: name length = 0");
return false;
}
if ([name hasPrefix:@" "]) {
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix = \" \"");
return false;
}
// check if attribute name is reserved or attribute name matches reserved prefix.
for (NSString* key in [NRMAAnalytics reservedKeywords]) {
if ([key isEqualToString:name]) {
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
return false;
}
}
for (NSString* key in [NRMAAnalytics reservedPrefixes]) {
if ([name hasPrefix:key]) {
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
return false;
}
}

// check if attribute name exceeds max length.
if ([name length] > kNRMA_Attrib_Max_Name_Length) {
NRLOG_AGENT_ERROR(@"invalid attribute: name length exceeds limit");
return false;
}
return true;

} valueValidator:^BOOL(id value) {
if ([value isKindOfClass:[NSString class]]) {
if ([(NSString*)value length] == 0) {
NRLOG_AGENT_ERROR(@"invalid attribute: value length = 0");
return false;
}
else if ([(NSString*)value length] >= kNRMA_Attrib_Max_Value_Size_Bytes) {
NRLOG_AGENT_ERROR(@"invalid attribute: value exceeded maximum byte size exceeded");
return false;
}
}
if (value == nil || [value isKindOfClass:[NSNull class]]) {
NRLOG_AGENT_ERROR(@"invalid attribute: value cannot be nil");
return false;
}

return true;
} andEventTypeValidator:^BOOL(NSString *eventType) {
return YES;
}];
_attributeValidator = [NewRelicInternalUtils attributeValidator];
_sessionAttributeManager = [[NRMASAM alloc] initWithAttributeValidator:_attributeValidator];


NSString* attributes = [self sessionAttributeJSONString];
if (attributes != nil && [attributes length] > 0) {
NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:[attributes dataUsingEncoding:NSUTF8StringEncoding]
Expand Down Expand Up @@ -257,7 +207,6 @@ - (void) dealloc {
[__eventTypeRegex release];
[_eventManager dealloc];
[_sessionAttributeManager dealloc];
[_attributeValidator release];
[_sessionStartTime release];

[super dealloc];
Expand Down
12 changes: 6 additions & 6 deletions Agent/HandledException/NRMAHandledExceptions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ - (void) recordError:(NSError * _Nonnull)error
resultMap,
[self createThreadVector:callstack length:frames]
);
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];

if (attributes != nil) {
[contextAdapter addAttributesNewValidation:attributes];
Expand All @@ -218,7 +218,7 @@ - (void) recordError:(NSError * _Nonnull)error
[self createThreadVector:callstack length:frames]
);

NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];

if (attributes != nil) {
[contextAdapter addAttributes:attributes];
Expand Down Expand Up @@ -268,7 +268,7 @@ - (void) recordHandledException:(NSException*)exception

[self checkOffline:report];

NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];

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

[self checkOffline:report];

NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];

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

NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];

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

NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[analyticsParent getAttributeValidator]] autorelease];
NRMAExceptionReportAdaptor* contextAdapter = [[[NRMAExceptionReportAdaptor alloc] initWithReport:report attributeValidator:[NewRelicInternalUtils attributeValidator]] autorelease];

if (exceptionDictionary != nil) {
[contextAdapter addAttributes:exceptionDictionary];
Expand Down
3 changes: 3 additions & 0 deletions Agent/Utilities/NewRelicInternalUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "NRMAReachability.h"
#import "NRConstants.h"
#import "NRMANetworkMonitor.h"
#import "BlockAttributeValidator.h"

#if __LP64__
#define NRMA_NSI "ld"
Expand Down Expand Up @@ -104,5 +105,7 @@ NSTimeInterval NRMAMillisecondTimestamp(void);

+ (NRMAReachability*) reachability;

+ (id<AttributeValidatorProtocol>) attributeValidator;

@end

60 changes: 60 additions & 0 deletions Agent/Utilities/NewRelicInternalUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,64 @@ + (BOOL) isSimulator {
return simulator != nil;
}

static id<AttributeValidatorProtocol> _attributeValidator;
+ (id<AttributeValidatorProtocol>) attributeValidator
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_attributeValidator = [[BlockAttributeValidator alloc] initWithNameValidator:^BOOL(NSString *name) {
if ([name length] == 0) {
NRLOG_AGENT_ERROR(@"invalid attribute: name length = 0");
return false;
}
if ([name hasPrefix:@" "]) {
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix = \" \"");
return false;
}
// check if attribute name is reserved or attribute name matches reserved prefix.
for (NSString* key in [NRMAAnalytics reservedKeywords]) {
if ([key isEqualToString:name]) {
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
return false;
}
}
for (NSString* key in [NRMAAnalytics reservedPrefixes]) {
if ([name hasPrefix:key]) {
NRLOG_AGENT_ERROR(@"invalid attribute: name prefix disallowed");
return false;
}
}

// check if attribute name exceeds max length.
if ([name length] > kNRMA_Attrib_Max_Name_Length) {
NRLOG_AGENT_ERROR(@"invalid attribute: name length exceeds limit");
return false;
}
return true;

} valueValidator:^BOOL(id value) {
if ([value isKindOfClass:[NSString class]]) {
if ([(NSString*)value length] == 0) {
NRLOG_AGENT_ERROR(@"invalid attribute: value length = 0");
return false;
}
else if ([(NSString*)value length] >= kNRMA_Attrib_Max_Value_Size_Bytes) {
NRLOG_AGENT_ERROR(@"invalid attribute: value exceeded maximum byte size exceeded");
return false;
}
}
if (value == nil || [value isKindOfClass:[NSNull class]]) {
NRLOG_AGENT_ERROR(@"invalid attribute: value cannot be nil");
return false;
}

return true;
} andEventTypeValidator:^BOOL(NSString *eventType) {
return YES;
}];
});

return _attributeValidator;
}

@end

0 comments on commit 47ff808

Please sign in to comment.