Bug 3491 - Get InvalidCastException when trying to use NSOpenPanel or NSSavePanel in sandboxed environment
Summary: Get InvalidCastException when trying to use NSOpenPanel or NSSavePanel in san...
Status: RESOLVED FIXED
Alias: None
Product: MonoMac
Classification: Desktop
Component: Bindings ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-02-16 00:52 UTC by Curtis Wensley
Modified: 2015-02-17 14:10 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test to repro the issue (22.49 KB, application/zip)
2012-02-16 00:52 UTC, Curtis Wensley
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Curtis Wensley 2012-02-16 00:52:12 UTC
Created attachment 1364 [details]
Test to repro the issue

When using a sandboxed app, the NSSavePanel.SavePanel and NSOpenPanel.OpenPanel throw an exception (see below).

A sample app is provided, along with a bash script to sign the entitlements correctly for the sandboxed app.  Steps to compile:

1) Compile app in monodevelop in RELEASE mode
2) Change macbuild script to change APP_CERT and INSTALLER_CERT to your own certificate(s)
3) run ./macbuild to create deploy/TestOpenFile.app
4) run deploy/TestOpenFile.app
5) Click on either "Open File" or "Save File"
6) Enjoy the crashiness

12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: Unhandled Exception: System.InvalidCastException: Cannot cast from source type to destination type.
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at MonoMac.AppKit.NSOpenPanel.get_OpenPanel () <0x00040>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at TestOpenFile.MainWindow.openFileClicked (MonoMac.Foundation.NSObject) <0x0000b>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at (wrapper dynamic-method) object.[TestOpenFile.MainWindow.Void openFileClicked(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) <0x00033>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at (wrapper native-to-managed) object.[TestOpenFile.MainWindow.Void openFileClicked(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) <0x000fb>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string[]) <0x00003>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at MonoMac.AppKit.NSApplication.Main (string[]) <0x00017>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at TestOpenFile.MainClass.Main (string[]) <0x00017>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: [ERROR] FATAL UNHANDLED EXCEPTION: System.InvalidCastException: Cannot cast from source type to destination type.
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at MonoMac.AppKit.NSOpenPanel.get_OpenPanel () <0x00040>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at TestOpenFile.MainWindow.openFileClicked (MonoMac.Foundation.NSObject) <0x0000b>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at (wrapper dynamic-method) object.[TestOpenFile.MainWindow.Void openFileClicked(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) <0x00033>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at (wrapper native-to-managed) object.[TestOpenFile.MainWindow.Void openFileClicked(MonoMac.Foundation.NSObject)] (MonoMac.Foundation.NSObject,MonoMac.ObjCRuntime.Selector,MonoMac.Foundation.NSObject) <0x000fb>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at (wrapper managed-to-native) MonoMac.AppKit.NSApplication.NSApplicationMain (int,string[]) <0x00003>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at MonoMac.AppKit.NSApplication.Main (string[]) <0x00017>
12-02-15 9:39:40.875 PM [0x0-0xe1de1d].com.yourcompany.TestOpenFile: at TestOpenFile.MainClass.Main (string[]) <0x00017>
Comment 1 Curtis Wensley 2012-02-16 12:44:57 UTC
Investigating further, it seems that NSSavePanel and NSOpenPanel do not inherit from NSPanel/NSWindow/etc when sandboxed, but only NSObject.  More details:

https://developer.apple.com/library/mac/#documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW4
Comment 2 Miguel de Icaza [MSFT] 2012-02-20 11:37:13 UTC
Thanks for the research, I will think of something to get this going.
Comment 4 Miguel de Icaza [MSFT] 2012-03-01 10:48:59 UTC
We will be looking at this problem once we have the new adding for MonoMac that Jeff is working on now.

We have accumulated too much technical debt and it is better to deal with the problem now.
Comment 5 Chris Hamons 2015-02-17 13:58:33 UTC
Curtis,

Apologies on the very long delay in wrapping up this bug. It was misfiled and overlooked.

I believe we've fixed this in Xamarin.Mac 1.10. Cocoa returns a different class in the sandboxed case that responds to the same selectors, but we needed changes on our end to handle this.
Comment 6 Curtis Wensley 2015-02-17 14:10:56 UTC
Hi Chris,

Yes, I think it has been fixed now, thank you!

Cheers,
Curtis.