/// Copyright 2016 Google Inc. All rights reserved. /// /// Licensed under the Apache License, Version 2.0 (the "License"); /// you may not use this file except in compliance with the License. /// You may obtain a copy of the License at /// /// http://www.apache.org/licenses/LICENSE-2.0 /// /// Unless required by applicable law or agreed to in writing, software /// distributed under the License is distributed on an "AS IS" BASIS, /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. /// See the License for the specific language governing permissions and /// limitations under the License. #import "Source/common/SNTBlockMessage.h" #import "Source/common/SNTConfigurator.h" #import "Source/common/SNTFileAccessEvent.h" #import "Source/common/SNTLogging.h" #import "Source/common/SNTStoredEvent.h" #import "Source/common/SNTSystemInfo.h" static id ValueOrNull(id value) { return value ?: [NSNull null]; } @implementation SNTBlockMessage + (NSAttributedString *)formatMessage:(NSString *)message { NSString *htmlHeader = @""; // Support Dark Mode. Note, the returned NSAttributedString is static and does not update when // the OS switches modes. NSString *mode = [NSUserDefaults.standardUserDefaults stringForKey:@"AppleInterfaceStyle"]; BOOL dark = [mode isEqualToString:@"Dark"]; htmlHeader = [NSString stringWithFormat:htmlHeader, dark ? @"#ddd" : @"#333"]; NSString *htmlFooter = @""; NSString *fullHTML = [NSString stringWithFormat:@"%@%@%@", htmlHeader, message, htmlFooter]; #ifdef SANTAGUI NSData *htmlData = [fullHTML dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *options = @{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding), }; return [[NSAttributedString alloc] initWithHTML:htmlData options:options documentAttributes:NULL]; #else NSString *strippedHTML = [self stringFromHTML:fullHTML]; if (!strippedHTML) { return [[NSAttributedString alloc] initWithString:@"This binary has been blocked."]; } return [[NSAttributedString alloc] initWithString:strippedHTML]; #endif } + (NSAttributedString *)attributedBlockMessageForEvent:(SNTStoredEvent *)event customMessage:(NSString *)customMessage { NSString *message; if (customMessage.length) { message = customMessage; } else if (event.decision == SNTEventStateBlockUnknown) { message = [[SNTConfigurator configurator] unknownBlockMessage]; if (!message) { message = @"The following application has been blocked from executing
" @"because its trustworthiness cannot be determined."; } } else { message = [[SNTConfigurator configurator] bannedBlockMessage]; if (!message) { message = @"The following application has been blocked from executing
" @"because it has been deemed malicious."; } } return [SNTBlockMessage formatMessage:message]; } + (NSAttributedString *)attributedBlockMessageForFileAccessEvent:(SNTFileAccessEvent *)event customMessage:(NSString *)customMessage { NSString *message = customMessage; if (!message.length) { message = [[SNTConfigurator configurator] fileAccessBlockMessage]; if (!message.length) { message = @"Access to a file has been denied."; } } return [SNTBlockMessage formatMessage:message]; } + (NSString *)stringFromHTML:(NSString *)html { NSError *error; NSXMLDocument *xml = [[NSXMLDocument alloc] initWithXMLString:html options:0 error:&error]; if (!xml && error.code == NSXMLParserEmptyDocumentError) { html = [NSString stringWithFormat:@"%@", html]; xml = [[NSXMLDocument alloc] initWithXMLString:html options:0 error:&error]; if (!xml) return html; } // Strip any HTML tags out of the message. Also remove any content inside