Brion VIBBER has uploaded a new change for review. https://gerrit.wikimedia.org/r/183899
Change subject: Fail gracefully instead of crashing when failing to save cached image ...................................................................... Fail gracefully instead of crashing when failing to save cached image Also uses shorter, unencoded filenames for cached images: makes them more human-readable in cases of non-ASCII characters, and helps keep non-ASCII filenames short enough to fit in the filesystem! https://trello.com/c/BEhNodlY/29-crash-in-image-caching Change-Id: Id9a17c8689eb2e5a8a8436805992003cf9debd8d --- M MediaWikiKit/MediaWikiKit/MWKDataStore.m M MediaWikiKit/MediaWikiKitTests/MWKDataStorePathTests.m M wikipedia/Web Image Interception/URLCache.m 3 files changed, 20 insertions(+), 8 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/apps/ios/wikipedia refs/changes/99/183899/1 diff --git a/MediaWikiKit/MediaWikiKit/MWKDataStore.m b/MediaWikiKit/MediaWikiKit/MWKDataStore.m index c32c0f4..86fadf7 100644 --- a/MediaWikiKit/MediaWikiKit/MWKDataStore.m +++ b/MediaWikiKit/MediaWikiKit/MWKDataStore.m @@ -120,15 +120,21 @@ NSString *prefix = @"//upload.wikimedia.org/"; if ([str hasPrefix:prefix]) { NSString *suffix = [str substringFromIndex:[prefix length]]; + NSString *fileName = [suffix lastPathComponent]; // Image URLs are already percent-encoded, so don't double-encode em. - - // "/" occurs in those nasty paths! but ":" cannot so let's use it - // just like Mac OS X does ;) - //NSString *noslashes = [suffix stringByReplacingOccurrencesOfString:@"/" withString:@":"]; + // In fact, we want to decode them... + // If we don't, long Unicode filenames may not fit in the filesystem. + NSString *decodedFileName = [fileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; - NSString *fileName = [suffix lastPathComponent]; - return fileName; + // Just to be safe, confirm no path explostions! + if ([decodedFileName rangeOfString:@"/"].location != NSNotFound) { + @throw [NSException exceptionWithName:@"MWKDataStoreException" + reason:@"Tried to save URL with encoded slash" + userInfo:@{@"str": str}]; + } + + return decodedFileName; } else { @throw [NSException exceptionWithName:@"MWKDataStoreException" reason:@"Tried to save non-upload.wikimedia.org URL as image" diff --git a/MediaWikiKit/MediaWikiKitTests/MWKDataStorePathTests.m b/MediaWikiKit/MediaWikiKitTests/MWKDataStorePathTests.m index 36be87a..69b6561 100644 --- a/MediaWikiKit/MediaWikiKitTests/MWKDataStorePathTests.m +++ b/MediaWikiKit/MediaWikiKitTests/MWKDataStorePathTests.m @@ -104,7 +104,7 @@ - (void)testImagePathUnicode { NSString *urlForbiddenCityImage = @"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/%E5%8C%97%E4%BA%AC%E6%95%85%E5%AE%AB12.JPG/440px-%E5%8C%97%E4%BA%AC%E6%95%85%E5%AE%AB12.JPG"; - XCTAssertEqualObjects([dataStore pathForImageURL:urlForbiddenCityImage title:titleForbiddenCity], [basePath stringByAppendingPathComponent:@"sites/wikipedia.org/en/articles/Forbidden_City/Images/440px-%E5%8C%97%E4%BA%AC%E6%95%85%E5%AE%AB12.JPG"]); + XCTAssertEqualObjects([dataStore pathForImageURL:urlForbiddenCityImage title:titleForbiddenCity], [basePath stringByAppendingPathComponent:@"sites/wikipedia.org/en/articles/Forbidden_City/Images/440px-北京故宫12.JPG"]); } @end diff --git a/wikipedia/Web Image Interception/URLCache.m b/wikipedia/Web Image Interception/URLCache.m index afde536..bac164e 100644 --- a/wikipedia/Web Image Interception/URLCache.m +++ b/wikipedia/Web Image Interception/URLCache.m @@ -109,7 +109,13 @@ // (This one has no thread safety issues.) //imageDataToUse = self.debuggingPlaceHolderImageData; - [self.article importImageData:imageDataToUse image:image]; + @try { + [self.article importImageData:imageDataToUse image:image]; + } + @catch (NSException *e) { + NSLog(@"Failure to save cached image data: %@", e); + return; + } // Broadcast the image data so things like the table of contents can update // itself as images arrive. -- To view, visit https://gerrit.wikimedia.org/r/183899 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id9a17c8689eb2e5a8a8436805992003cf9debd8d Gerrit-PatchSet: 1 Gerrit-Project: apps/ios/wikipedia Gerrit-Branch: master Gerrit-Owner: Brion VIBBER <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
