Skip to content

Commit

Permalink
Don't crash in oghtmlparser on big sites
Browse files Browse the repository at this point in the history
Some sites ignore the byterange, make sure to not crash for those sites
if they are big. Also limit the size to 64kb instead of 512kb.
  • Loading branch information
tmolitor-stud-tu committed Aug 25, 2024
1 parent 115b876 commit c6b272c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions Monal/Classes/chatViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -2998,7 +2998,7 @@ -(void) loadPreviewWithUrlForRow:(NSIndexPath *) indexPath withResultHandler:(mo
return;
}
//limit to 512KB of html
if(contentLength.intValue > 524288)
if(contentLength.intValue > 65536)
{
DDLogWarn(@"Now loading preview HTML for %@ with byte range 0-512k...", row.url);
[self downloadPreviewWithRow:indexPath usingByterange:YES andResultHandler:resultHandler];
Expand Down Expand Up @@ -3038,7 +3038,7 @@ -(void) downloadPreviewWithRow:(NSIndexPath*) indexPath usingByterange:(BOOL) us
request.requiresDNSSECValidation = YES;
[request setValue:@"facebookexternalhit/1.1" forHTTPHeaderField:@"User-Agent"]; //required on some sites for og tags e.g. youtube
if(useByterange)
[request setValue:@"bytes=0-524288" forHTTPHeaderField:@"Range"];
[request setValue:@"bytes=0-65536" forHTTPHeaderField:@"Range"];
request.timeoutInterval = 10;
NSURLSession* session = [HelperTools createEphemeralURLSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData* _Nullable data, NSURLResponse* _Nullable response, NSError* _Nullable error) {
Expand All @@ -3047,10 +3047,14 @@ -(void) downloadPreviewWithRow:(NSIndexPath*) indexPath usingByterange:(BOOL) us
else
{
NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSURL* baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@%@", row.url.scheme, row.url.host, row.url.path]];
MLOgHtmlParser* ogParser = [[MLOgHtmlParser alloc] initWithHtml:body andBaseUrl:baseURL];
MLOgHtmlParser* ogParser = nil;
NSString* text = nil;
NSURL* image = nil;
if([body length] <= 65536)
{
NSURL* baseURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@%@", row.url.scheme, row.url.host, row.url.path]];
ogParser = [[MLOgHtmlParser alloc] initWithHtml:body andBaseUrl:baseURL];
}
if(ogParser != nil)
{
text = [ogParser getOgTitle];
Expand Down

0 comments on commit c6b272c

Please sign in to comment.