Bug 224577 - [Cocoa] re-enable test case WKInspectorDelegate.InspectorConfiguration
Summary: [Cocoa] re-enable test case WKInspectorDelegate.InspectorConfiguration
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Inspector (show other bugs)
Version: WebKit Nightly Build
Hardware: All All
: P2 Normal
Assignee: BJ Burg
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-04-14 14:20 PDT by BJ Burg
Modified: 2021-04-22 17:09 PDT (History)
7 users (show)

See Also:


Attachments
Patch v1.0 (24.55 KB, patch)
2021-04-19 12:17 PDT, BJ Burg
no flags Details | Formatted Diff | Diff
Patch v1.1 (23.86 KB, patch)
2021-04-22 16:16 PDT, BJ Burg
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description BJ Burg 2021-04-14 14:20:25 PDT
<rdar://70505272>
Comment 1 BJ Burg 2021-04-19 11:03:45 PDT
The fix for this is.. involved. Patch coming.
Comment 2 BJ Burg 2021-04-19 12:17:53 PDT
Created attachment 426457 [details]
Patch v1.0
Comment 3 Devin Rousso 2021-04-20 12:24:15 PDT
Comment on attachment 426457 [details]
Patch v1.0

View in context: https://bugs.webkit.org/attachment.cgi?id=426457&action=review

r=me

This looks generally fine to me, but I'd recommend getting someone more familiar with CSP (both the concept and the related WebKit logic) to look at it as well.

BTW I was poking around some other CSP tests and I came across `LegacySchemeRegistry::registerURLSchemeAsBypassingContentSecurityPolicy`.  Would that also be a valid solution to this?  Or does the "Legacy" part suggest that maybe we don't want to use this πŸ˜…

Also, is it possible to write a layout/API test that confirms the CSP state in Web Inspector so that we're not opening up any new CSP "holes"?

> Source/WebKit/ChangeLog:34
> +        Drive-by, fix memory leak the right way.

Could you explain this more?

> Source/WebKit/ChangeLog:37
> +        (WebKit::WebInspectorUIProxy::frontendLoaded): Call the client.

ditto (:34)

> Source/WebKit/UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.mm:51
> +    return _allowedURLSchemesForCSP.autorelease();

IIRC, `RetainPtr::autorelease` will `std::exchange` the underlying pointer.  Do we want that here?  If so, can you rename this to `releaseAllowedURLSchemesForCSP`?  If not, should this just be `.get()` instead?  Or maybe `[_allowedURLSchemesForCSP copy]`?

> Source/WebKit/UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.mm:61
> +    NSMutableSet<NSURL *> *result = [[NSMutableSet alloc] initWithCapacity:2];
> +    [result addObject:[NSURL URLWithString:WebKit::WebInspectorUIProxy::inspectorPageURL()]];
> +    [result addObject:[NSURL URLWithString:WebKit::WebInspectorUIProxy::inspectorTestPageURL()]];
> +    _mainResourceURLsForCSP = result;

Do we expect/allow these values to change?  If not, do we need to recreate/reassign `_mainResourceURLsForCSP` each time, or can we make a lazy getter that initializes it once?

> Source/WebKit/UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.mm:112
> +        NSMutableDictionary *headerFields = @{

Should we `adoptNS`?

> Source/WebKit/UIProcess/Inspector/mac/WKInspectorViewController.mm:118
> +    RetainPtr<NSMutableSet<NSString *>> allowedURLSchemes = adoptNS([NSMutableSet setWithObjects:WKInspectorResourceScheme, @"ws", @"wss", nil]);

Are `ws` and `wss` actually used in the Web Inspector frontend?

> Source/WebKit/UIProcess/Inspector/mac/WKInspectorViewController.mm:119
> +    for (auto pair : _configuration->_configuration->urlSchemeHandlers())

`auto&`
Comment 4 Joseph Pecoraro 2021-04-20 12:47:02 PDT
Comment on attachment 426457 [details]
Patch v1.0

View in context: https://bugs.webkit.org/attachment.cgi?id=426457&action=review

> Source/WebKit/UIProcess/API/Cocoa/_WKInspectorTesting.mm:62
> +        _inspector->evaluateInFrontendForTesting(JavaScriptSnippetToOpenURLExternally(url));

Ideally the memory fix would happen as a separate commit.

> Source/WebKit/UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.mm:56
> +    _allowedURLSchemesForCSP = [allowedURLSchemes copy];

`_allowedURLSchemesForCSP` is a RetainPtr, so you're copying and retaining. This should probably be:

```
_allowedURLSchemesForCSP = adoptNS([allowedURLSchemes copy]);
```

>> Source/WebKit/UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.mm:61
>> +    _mainResourceURLsForCSP = result;
> 
> Do we expect/allow these values to change?  If not, do we need to recreate/reassign `_mainResourceURLsForCSP` each time, or can we make a lazy getter that initializes it once?

+1 for Devin's comment.

But again this is a RetainPtr so you're seeing an alloc + retain. This should probably be:

```
_mainResourceURLsForCSP = adoptNS([[NSMutableSet alloc] initWithCapacity:2]);
[_mainResourceURLsForCSP addObject:...];
[_mainResourceURLsForCSP addObject:...];
```

We prefer the `adoptNS` immediately on the line with copy/alloc.

>> Source/WebKit/UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.mm:112
>> +        NSMutableDictionary *headerFields = @{
> 
> Should we `adoptNS`?

+1. The `mutableCopy` would be leaked otherwise.

>> Source/WebKit/UIProcess/Inspector/mac/WKInspectorViewController.mm:118
>> +    RetainPtr<NSMutableSet<NSString *>> allowedURLSchemes = adoptNS([NSMutableSet setWithObjects:WKInspectorResourceScheme, @"ws", @"wss", nil]);
> 
> Are `ws` and `wss` actually used in the Web Inspector frontend?

+1 to Devin's comment. Looks like the macOS one may not need it. It was added with:

    commit 835af68ab239f1577787fd759f67f08e8aad2f8d
    Author: commit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
    Date:   Thu Apr 14 17:44:20 2016 +0000

        REGRESSION: Web Inspector: Remote inspector doesn't work
        https://bugs.webkit.org/show_bug.cgi?id=156543
    
        Patch by Carlos Garcia Campos <cgarcia@igalia.com> on 2016-04-14
        Reviewed by Timothy Hatcher.
    
        WebSocket connection is blocked by CSP, but needed by the remote web inspector to work, so allow connect to ws
        URLs from the web inspector. Also add stubs for zoomFactor and setZoomFactor to InspectorFrontendHostStub,
        required after r199396.

---

Also, this is one where we do not want to adopt, because it is already an auto released object.

Either:
```
RetainPtr<NSMutableSet<NSString *>> allowedURLSchemes = [NSMutableSet setWithObjects:WKInspectorResourceScheme, @"ws", @"wss", nil];
```

Preferred:
```
RetainPtr<NSMutableSet<NSString *>> allowedURLSchemes = adoptNS([[NSMutableSet alloc] initWithObjects:WKInspectorResourceScheme, @"ws", @"wss", nil]);
```

> Source/WebKit/UIProcess/Inspector/mac/WKInspectorViewController.mm:122
> +    [inspectorSchemeHandler setAllowedURLSchemesForCSP:allowedURLSchemes.autorelease()];

Not sure we would want to autorelease here. Just let the method copy/retain appropriately, then we don't touch autorelease pools at all.
Comment 5 Darin Adler 2021-04-20 20:40:52 PDT
Comment on attachment 426457 [details]
Patch v1.0

View in context: https://bugs.webkit.org/attachment.cgi?id=426457&action=review

>> Source/WebKit/UIProcess/Inspector/mac/WKInspectorViewController.mm:122
>> +    [inspectorSchemeHandler setAllowedURLSchemesForCSP:allowedURLSchemes.autorelease()];
> 
> Not sure we would want to autorelease here. Just let the method copy/retain appropriately, then we don't touch autorelease pools at all.

The correct code for a case like this is to just use get(), not autorelease().

> Source/WebKit/UIProcess/Inspector/mac/WKInspectorViewController.mm:123
>      [configuration setURLSchemeHandler:inspectorSchemeHandler.autorelease() forURLScheme:WKInspectorResourceScheme];

Same here.
Comment 6 Darin Adler 2021-04-20 20:43:09 PDT
Comment on attachment 426457 [details]
Patch v1.0

View in context: https://bugs.webkit.org/attachment.cgi?id=426457&action=review

>> Source/WebKit/UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.mm:51
>> +    return _allowedURLSchemesForCSP.autorelease();
> 
> IIRC, `RetainPtr::autorelease` will `std::exchange` the underlying pointer.  Do we want that here?  If so, can you rename this to `releaseAllowedURLSchemesForCSP`?  If not, should this just be `.get()` instead?  Or maybe `[_allowedURLSchemesForCSP copy]`?

This should be one of these:

    return _allowedURLSchemesForCSP.get();
    return adoptNS([_allowedURLSchemesForCSP copy]).autorelease();

We don’t want to null out the pointer. Not sure we need autorelease.
Comment 7 BJ Burg 2021-04-22 16:16:32 PDT
Created attachment 426862 [details]
Patch v1.1
Comment 8 EWS 2021-04-22 17:09:13 PDT
Committed r276476 (236935@main): <https://commits.webkit.org/236935@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 426862 [details].