Changeset 247414 in webkit


Ignore:
Timestamp:
Jul 12, 2019 10:14:00 PM (5 years ago)
Author:
aestes@apple.com
Message:

[Cocoa] -loadFileURL:allowingReadAccessToURL: should fully resolve file URLs
https://bugs.webkit.org/show_bug.cgi?id=199768
<rdar://problem/52002206>

Reviewed by Geoffrey Garen.

Source/WebKit:

-loadFileURL:allowingReadAccessToURL: used -_web_originalDataAsWTFString from WKNSURLExtras
to convert the file and read access NSURLs to strings, which under the hood calls
CFURLGetBytes(). CFURLGetBytes() gets the URL's string without considering the base URL, so
if the client creates a URL like this:

NSURL *url = [NSURL fileURLWithPath:@"tmpfile.txt" relativeToURL:[NSURL fileURLWithPath:@"/tmp"]]

... then -_web_originalDataAsWTFString will merely return the string "tmpfile.txt". When
that is later converted back to a URL in WebPageProxy::loadFile(), we lose track of the base
component and refuse to load something that no longer looks like a file: URL.

Fixed this by fully resolving the URLs passed to -loadFileURL:allowingReadAccessToURL: when
converting to strings by using -[NSURL absoluteString] instead of -_web_originalDataAsWTFString.

  • Shared/Cocoa/WKNSURLExtras.mm:

(-[NSURL _web_originalDataAsWTFString]):

  • UIProcess/API/Cocoa/WKWebView.mm:

(-[WKWebView loadFileURL:allowingReadAccessToURL:]):

Tools:

  • TestWebKitAPI/Tests/WebKitCocoa/LoadFileURL.mm:

(TEST):

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r247411 r247414  
     12019-07-12  Andy Estes  <aestes@apple.com>
     2
     3        [Cocoa] -loadFileURL:allowingReadAccessToURL: should fully resolve file URLs
     4        https://bugs.webkit.org/show_bug.cgi?id=199768
     5        <rdar://problem/52002206>
     6
     7        Reviewed by Geoffrey Garen.
     8
     9        -loadFileURL:allowingReadAccessToURL: used -_web_originalDataAsWTFString from WKNSURLExtras
     10        to convert the file and read access NSURLs to strings, which under the hood calls
     11        CFURLGetBytes(). CFURLGetBytes() gets the URL's string without considering the base URL, so
     12        if the client creates a URL like this:
     13
     14            NSURL *url = [NSURL fileURLWithPath:@"tmpfile.txt" relativeToURL:[NSURL fileURLWithPath:@"/tmp"]]
     15
     16        ... then -_web_originalDataAsWTFString will merely return the string "tmpfile.txt". When
     17        that is later converted back to a URL in WebPageProxy::loadFile(), we lose track of the base
     18        component and refuse to load something that no longer looks like a file: URL.
     19
     20        Fixed this by fully resolving the URLs passed to -loadFileURL:allowingReadAccessToURL: when
     21        converting to strings by using -[NSURL absoluteString] instead of -_web_originalDataAsWTFString.
     22
     23        * Shared/Cocoa/WKNSURLExtras.mm:
     24        (-[NSURL _web_originalDataAsWTFString]):
     25        * UIProcess/API/Cocoa/WKWebView.mm:
     26        (-[WKWebView loadFileURL:allowingReadAccessToURL:]):
     27
    1282019-07-12  Megan Gardner  <megan_gardner@apple.com>
    229
  • trunk/Source/WebKit/Shared/Cocoa/WKNSURLExtras.mm

    r238771 r247414  
    4848- (String)_web_originalDataAsWTFString
    4949{
     50    // FIXME: Why is it OK to ignore base URL here?
    5051    CString originalData;
    5152    WTF::getURLBytes((__bridge CFURLRef)self, originalData);
  • trunk/Source/WebKit/UIProcess/API/Cocoa/WKWebView.mm

    r247344 r247414  
    953953        [NSException raise:NSInvalidArgumentException format:@"%@ is not a file URL", readAccessURL];
    954954
    955     return wrapper(_page->loadFile([URL _web_originalDataAsWTFString], [readAccessURL _web_originalDataAsWTFString]));
     955    return wrapper(_page->loadFile(URL.absoluteString, readAccessURL.absoluteString));
    956956}
    957957
  • trunk/Tools/ChangeLog

    r247413 r247414  
     12019-07-12  Andy Estes  <aestes@apple.com>
     2
     3        [Cocoa] -loadFileURL:allowingReadAccessToURL: should fully resolve file URLs
     4        https://bugs.webkit.org/show_bug.cgi?id=199768
     5        <rdar://problem/52002206>
     6
     7        Reviewed by Geoffrey Garen.
     8
     9        * TestWebKitAPI/Tests/WebKitCocoa/LoadFileURL.mm:
     10        (TEST):
     11
    1122019-07-12  Aakash Jain  <aakash_jain@apple.com>
    213
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/LoadFileURL.mm

    r246694 r247414  
    6767    EXPECT_WK_STREQ(webView.get()._resourceDirectoryURL.path, file.URLByDeletingLastPathComponent.path);
    6868}
     69
     70TEST(WKWebView, LoadRelativeFileURL)
     71{
     72    auto webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)]);
     73   
     74    auto delegate = adoptNS([[TestNavigationDelegate alloc] init]);
     75    [webView setNavigationDelegate:delegate.get()];
     76   
     77    NSString *path = [NSBundle.mainBundle pathForResource:@"simple" ofType:@"html" inDirectory:@"TestWebKitAPI.resources"];
     78    NSURL *baseURL = [NSURL fileURLWithPath:path.stringByDeletingLastPathComponent];
     79    NSURL *fileURL = [NSURL fileURLWithPath:path.lastPathComponent relativeToURL:baseURL];
     80    EXPECT_NOT_NULL([webView loadFileURL:fileURL allowingReadAccessToURL:fileURL.URLByDeletingLastPathComponent]);
     81    [delegate waitForDidFinishNavigation];
     82    EXPECT_WK_STREQ([webView _resourceDirectoryURL].path, fileURL.URLByDeletingLastPathComponent.path);
     83}
Note: See TracChangeset for help on using the changeset viewer.