Refine handling of unsafe filename characters.

This commit is contained in:
Matthew Chen 2018-02-22 17:56:21 -05:00
parent de5d17a396
commit 47a6d844c9
8 changed files with 49 additions and 11 deletions

View File

@ -284,6 +284,10 @@ NS_ASSUME_NONNULL_BEGIN
actionBlock:^{
[DebugUIMessages testZalgoTextInThread:thread];
}],
[OWSTableItem itemWithTitle:@"Test Directional Filenames"
actionBlock:^{
[DebugUIMessages testDirectionalFilenamesInThread:thread];
}],
] mutableCopy];
if ([thread isKindOfClass:[TSContactThread class]]) {
TSContactThread *contactThread = (TSContactThread *)thread;
@ -1684,6 +1688,32 @@ NS_ASSUME_NONNULL_BEGIN
}];
}
+ (void)testDirectionalFilenamesInThread:(TSThread *)thread
{
for (NSString *filename in @[
@"a_test\u202Dabc.exe",
@"b_test\u202Eabc.exe",
@"c_testabc.exe",
]) {
OWSMessageSender *messageSender = [Environment current].messageSender;
NSString *utiType = (NSString *)kUTTypeData;
const NSUInteger kDataLength = 32;
DataSource *_Nullable dataSource =
[DataSourceValue dataSourceWithData:[self createRandomNSDataOfSize:kDataLength] utiType:utiType];
[dataSource setSourceFilename:filename];
SignalAttachment *attachment =
[SignalAttachment attachmentWithDataSource:dataSource dataUTI:utiType imageQuality:TSImageQualityOriginal];
OWSAssert(attachment);
if ([attachment hasError]) {
DDLogError(@"attachment[%@]: %@", [attachment sourceFilename], [attachment errorName]);
[DDLog flushLog];
}
OWSAssert(![attachment hasError]);
[ThreadUtil sendMessageWithAttachment:attachment inThread:thread messageSender:messageSender completion:nil];
}
}
@end
NS_ASSUME_NONNULL_END

View File

@ -130,7 +130,7 @@ public class SignalAttachment: NSObject {
@objc
public var sourceFilename: String? {
return dataSource.sourceFilename?.filterStringForDisplay()
return dataSource.sourceFilename?.filterFilename()
}
@objc
@ -311,7 +311,7 @@ public class SignalAttachment: NSObject {
@objc
public var filenameOrDefault: String {
if let filename = sourceFilename {
return filename.filterStringForDisplay()
return filename.filterFilename()
} else {
let kDefaultAttachmentName = "signal"
@ -335,7 +335,7 @@ public class SignalAttachment: NSObject {
if let filename = sourceFilename {
let fileExtension = (filename as NSString).pathExtension
if fileExtension.count > 0 {
return fileExtension.filterStringForDisplay()
return fileExtension.filterFilename()
}
}
if isOversizeText {

View File

@ -192,12 +192,12 @@ NSUInteger const TSAttachmentSchemaVersion = 4;
- (nullable NSString *)sourceFilename
{
return _sourceFilename.filterStringForDisplay;
return _sourceFilename.filterFilename;
}
- (NSString *)contentType
{
return _contentType.filterStringForDisplay;
return _contentType.filterFilename;
}
@end

View File

@ -529,6 +529,7 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec
[builder setId:attachmentStream.serverId];
OWSAssert(attachmentStream.contentType.length > 0);
[builder setContentType:attachmentStream.contentType];
DDLogVerbose(@"%@ Sending attachment with filename: '%@'", self.logTag, filename);
[builder setFileName:filename];
[builder setSize:attachmentStream.byteCount];
[builder setKey:attachmentStream.encryptionKey];

View File

@ -357,7 +357,7 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE
ofClass:[TSOutgoingMessage class]
withTransaction:transaction];
if (messages.count > 1) {
OWSFail(@"%@ More than one matching message with timestamp: %llu.", self.logTag, sentTimestamp);
DDLogError(@"%@ More than one matching message with timestamp: %llu.", self.logTag, sentTimestamp);
}
if (messages.count > 0) {
// TODO: We might also need to "mark as read by recipient" any older messages

View File

@ -80,7 +80,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)setSourceFilename:(nullable NSString *)sourceFilename
{
_sourceFilename = sourceFilename.filterStringForDisplay;
_sourceFilename = sourceFilename.filterFilename;
}
@end

View File

@ -10,6 +10,8 @@ NS_ASSUME_NONNULL_BEGIN
- (NSString *)filterStringForDisplay;
- (NSString *)filterFilename;
- (BOOL)isValidE164;
@end

View File

@ -144,7 +144,7 @@ NS_ASSUME_NONNULL_BEGIN
return [filteredForIndic copy];
}
+ (NSCharacterSet *)unsafeCharacterSet
+ (NSCharacterSet *)unsafeFilenameCharacterSet
{
static NSCharacterSet *characterSet;
static dispatch_once_t onceToken;
@ -160,9 +160,9 @@ NS_ASSUME_NONNULL_BEGIN
return characterSet;
}
- (NSString *)filterUnsafeCharacters
- (NSString *)filterUnsafeFilenameCharacters
{
NSCharacterSet *unsafeCharacterSet = [[self class] unsafeCharacterSet];
NSCharacterSet *unsafeCharacterSet = [[self class] unsafeFilenameCharacterSet];
NSRange range = [self rangeOfCharacterFromSet:unsafeCharacterSet];
if (range.location == NSNotFound) {
return self;
@ -184,7 +184,12 @@ NS_ASSUME_NONNULL_BEGIN
- (NSString *)filterStringForDisplay
{
return self.ows_stripped.filterForIndicScripts.filterForExcessiveDiacriticals.filterUnsafeCharacters;
return self.ows_stripped.filterForIndicScripts.filterForExcessiveDiacriticals;
}
- (NSString *)filterFilename
{
return self.ows_stripped.filterForIndicScripts.filterForExcessiveDiacriticals.filterUnsafeFilenameCharacters;
}
- (NSString *)filterForExcessiveDiacriticals