winforms webbrowser控件中的JavaScript访问问题

发布于 2025-01-07 19:41:10 字数 891 浏览 0 评论 0原文

预计到达时间:我已经成功了,答案发布在下面。我会将任何能够准确解释发生了什么情况以及如何清理它的人标记为答案。

我有一个类,其中包含一个带有 winforms webbrowser 控件的表单,用于显示 html 页面。我将一些脚本注入到页面的头部,以便我可以查询有关样式的信息。

当我从可执行文件启动应用程序时,一切正常。不过,我现在尝试在设计时从加载项启动该应用程序。发生的情况是,右键单击 .htm 类型文件,然后单击启动浏览器。然后,加载项启动浏览器,导航到提供的 .htm 文件路径。

我注意到的第一件事是浏览器现在显示以下消息:

为了帮助保护您的安全,您的网络浏览器已限制此文件显示可以访问您的计算机的活动内容。单击此处查看选项...”

然后我注意到,即使我单击启用活动内容,我所有的 javascript 调用现在都失败了。

作为测试,我尝试了这个简单的 javascript 调用(没有显式注入):

Me.Document.InvokeScript("execScript", New Object() {"alert('hello');", "JavaScript"})

这会导致一个 javascript 错误对话框,显示“访问被拒绝

因此,这是一个安全问题。我并不是想这样做,但我尝试暂时降低 IE 中的所有安全级别,但这没有什么区别。

我应该补充一点,该应用程序包含 2 个网络浏览器控件。第二个托管一个通过设置浏览器的 DocumentText 属性创建的网页。这不会遇到 javascript 访问问题。

ETA:我一直在研究 IInternetSecurityManager,它与此有什么关系吗?我希望不是:(

ETA: I've got it working and the answer is posted below. I'll mark as the answer anyone who can explain exactly what is going on and how I can clean it up.

I have a class that contains a form with a winforms webbrowser control to display an html page. I inject some script into the head of the page so that I can query information about styles.

When I launch the app from an executable, everything works fine. However I'm now attempting to launch the app from an add-in at design-time. What happens is you right-click on a .htm type file and click to launch the browser. The add-in then launches the browser which navigates to the supplied .htm file path.

The first thing I notice is that the browser now displays this message:

"To help protect your security, your web browser has restricted this file from showing active content that could access your computer. Click here for options..."

I then noticed that all my javascript calls were now failing even if I clicked to enable active content.

As a test I tried this simple javascript call (without explicit injection):

Me.Document.InvokeScript("execScript", New Object() {"alert('hello');", "JavaScript"})

This results in a javascript error dialog that says "access is denied"

So, this is a security issue. Not that I wanted to but, I tried temporarily reducing all security levels in IE but this made no difference.

I should add that the app contains 2 webbrowser controls. The second one hosts a web page that is created by setting the DocumentText property of the browser. This does not suffer from javascript access issues.

ETA: I've been looking into IInternetSecurityManager, could it be anything to do with that? I hope not :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2025-01-14 19:41:10

我已经设法让它与 IInternetSecurityManager 一起工作,IInternetSecurityManager 是由网络浏览器控件的站点返回的服务。
无论 url 是什么,我都通过在 ProcessUrlAction 方法中返回 Ok 来使其正常工作。

我根据在互联网上找到的内容将其抄写在一起,因此如果有人可以指出如何清理它并将其限制在 Intranet 中,那么我会将其标记为答案。

我想我需要检查 ProcessUrlAction 中的 url,并根据其内容返回“Ok”或“Default”。

这是代码:

Friend Class MainBrowser
Inherits WebBrowser

Private _Site As WebBrowserSite
Protected Overrides Function CreateWebBrowserSiteBase() As WebBrowserSiteBase
    If _Site Is Nothing Then
        _Site = New WebBrowserSite(Me)
    End If
    Return _Site
End Function

Protected Class WebBrowserSite
    Inherits System.Windows.Forms.WebBrowser.WebBrowserSite
    Implements NativeInterfaces.IServiceProvider
    Implements NativeInterfaces.IInternetSecurityManager

    Private Const INET_E_DEFAULT_ACTION As Integer = &H800C0011
    Private Const S_OK As Integer = 0
    Private Const E_NOINTERFACEX As Integer = &H80004002

    Private Shared IID_IInternetSecurityManager As Guid = Marshal.GenerateGuidForType(GetType(NativeInterfaces.IInternetSecurityManager))

    Private Owner As MainBrowser

    Public Sub New(ByVal owner As MainBrowser)
        MyBase.New(owner)
        owner = owner
    End Sub

    Public Function QueryService(ByRef guidService As System.Guid, ByRef riid As System.Guid, ByRef ppvObject As System.IntPtr) As Integer Implements NativeInterfaces.IServiceProvider.QueryService
        If guidService = IID_IInternetSecurityManager AndAlso riid = IID_IInternetSecurityManager Then
            ppvObject = Marshal.GetComInterfaceForObject(Me, GetType(NativeInterfaces.IInternetSecurityManager))
            Return S_OK
        End If
        ppvObject = IntPtr.Zero
        Return E_NOINTERFACEX
    End Function

    Public Function GetSecurityId(ByVal pwszUrl As String, ByVal pbSecurityId As System.IntPtr, ByRef pcbSecurityId As UInteger, ByRef dwReserved As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.GetSecurityId
        Return INET_E_DEFAULT_ACTION
    End Function

    Public Function GetSecuritySite(ByRef pSite As System.IntPtr) As Integer Implements NativeInterfaces.IInternetSecurityManager.GetSecuritySite
        pSite = IntPtr.Zero
        Return INET_E_DEFAULT_ACTION
    End Function

    Public Function SetSecuritySite(ByVal pSite As System.IntPtr) As Integer Implements NativeInterfaces.IInternetSecurityManager.SetSecuritySite
        Return INET_E_DEFAULT_ACTION
    End Function

    Public Function MapUrlToZone(ByVal pwszUrl As String, ByRef pdwZone As UInteger, ByVal dwFlags As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.MapUrlToZone
        pdwZone = 0 // URLZONE_LOCAL_MACHINE ?
        Return S_OK // no difference
        // Return INET_E_DEFAULT_ACTION
    End Function

    Public Function ProcessUrlAction(ByVal pwszUrl As String, ByVal dwAction As UInteger, ByVal pPolicy As System.IntPtr, ByVal cbPolicy As UInteger, ByVal pContext As System.IntPtr, ByVal cbContext As UInteger, ByVal dwFlags As UInteger, ByVal dwReserved As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.ProcessUrlAction
        // Return INET_E_DEFAULT_ACTION
        Return S_OK // This is what made the difference
    End Function

    Public Function QueryCustomPolicy(ByVal pwszUrl As String, ByRef guidKey As System.Guid, ByRef ppPolicy As System.IntPtr, ByRef pcbPolicy As UInteger, ByVal pContext As System.IntPtr, ByVal cbContext As UInteger, ByVal dwReserved As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.QueryCustomPolicy
        ppPolicy = IntPtr.Zero
        pcbPolicy = 0
        Return INET_E_DEFAULT_ACTION
    End Function

    Public Function SetZoneMapping1(ByVal dwZone As UInteger, ByVal lpszPattern As String, ByVal dwFlags As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.SetZoneMapping
        Return INET_E_DEFAULT_ACTION
    End Function

    Public Function GetZoneMappings(ByVal dwZone As UInteger, ByRef ppenumString As System.Runtime.InteropServices.ComTypes.IEnumString, ByVal dwFlags As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.GetZoneMappings
        ppenumString = Nothing
        Return INET_E_DEFAULT_ACTION
    End Function

End Class

End Class

接口:

 <ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("6d5140c1-7436-11ce-8034-00aa006009fa")> _
Interface IServiceProvider
    <PreserveSig()> _
    Function QueryService(ByRef guidService As Guid, ByRef riid As Guid, ByRef ppvObject As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer
End Interface


<ComImport(), GuidAttribute("79EAC9EE-BAF9-11CE-8C82-00AA004BA90B"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IInternetSecurityManager
    <PreserveSig()> _
    Function SetSecuritySite(<[In]()> ByVal pSite As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function GetSecuritySite(ByRef pSite As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function MapUrlToZone(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, ByRef pdwZone As UInt32, <[In]()> ByVal dwFlags As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function GetSecurityId(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, <Out()> ByVal pbSecurityId As IntPtr, <[In](), Out()> ByRef pcbSecurityId As UInt32, <[In]()> ByRef dwReserved As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function ProcessUrlAction(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, ByVal dwAction As UInt32, ByVal pPolicy As IntPtr, ByVal cbPolicy As UInt32, ByVal pContext As IntPtr, ByVal cbContext As UInt32, _
         ByVal dwFlags As UInt32, ByVal dwReserved As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function QueryCustomPolicy(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, ByRef guidKey As Guid, ByRef ppPolicy As IntPtr, ByRef pcbPolicy As UInt32, ByVal pContext As IntPtr, ByVal cbContext As UInt32, _
         ByVal dwReserved As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function SetZoneMapping(ByVal dwZone As UInt32, <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal lpszPattern As String, ByVal dwFlags As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function GetZoneMappings(<[In]()> ByVal dwZone As UInt32, ByRef ppenumString As ComTypes.IEnumString, <[In]()> ByVal dwFlags As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer
End Interface

I've managed to get it working with IInternetSecurityManager which is a service that is returned by the webbrowser control's site.
I got it working by returning Ok in the ProcessUrlAction method regardless of the url.

I cribbed this together from bits I found on the internet so if anyone can point out how it can be cleaned up and restricted to the intranet then i'll mark that as the answer.

I presume i need to examine the url, in ProcessUrlAction, and return Ok, or Default depending on its content.

Here's the code:

Friend Class MainBrowser
Inherits WebBrowser

Private _Site As WebBrowserSite
Protected Overrides Function CreateWebBrowserSiteBase() As WebBrowserSiteBase
    If _Site Is Nothing Then
        _Site = New WebBrowserSite(Me)
    End If
    Return _Site
End Function

Protected Class WebBrowserSite
    Inherits System.Windows.Forms.WebBrowser.WebBrowserSite
    Implements NativeInterfaces.IServiceProvider
    Implements NativeInterfaces.IInternetSecurityManager

    Private Const INET_E_DEFAULT_ACTION As Integer = &H800C0011
    Private Const S_OK As Integer = 0
    Private Const E_NOINTERFACEX As Integer = &H80004002

    Private Shared IID_IInternetSecurityManager As Guid = Marshal.GenerateGuidForType(GetType(NativeInterfaces.IInternetSecurityManager))

    Private Owner As MainBrowser

    Public Sub New(ByVal owner As MainBrowser)
        MyBase.New(owner)
        owner = owner
    End Sub

    Public Function QueryService(ByRef guidService As System.Guid, ByRef riid As System.Guid, ByRef ppvObject As System.IntPtr) As Integer Implements NativeInterfaces.IServiceProvider.QueryService
        If guidService = IID_IInternetSecurityManager AndAlso riid = IID_IInternetSecurityManager Then
            ppvObject = Marshal.GetComInterfaceForObject(Me, GetType(NativeInterfaces.IInternetSecurityManager))
            Return S_OK
        End If
        ppvObject = IntPtr.Zero
        Return E_NOINTERFACEX
    End Function

    Public Function GetSecurityId(ByVal pwszUrl As String, ByVal pbSecurityId As System.IntPtr, ByRef pcbSecurityId As UInteger, ByRef dwReserved As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.GetSecurityId
        Return INET_E_DEFAULT_ACTION
    End Function

    Public Function GetSecuritySite(ByRef pSite As System.IntPtr) As Integer Implements NativeInterfaces.IInternetSecurityManager.GetSecuritySite
        pSite = IntPtr.Zero
        Return INET_E_DEFAULT_ACTION
    End Function

    Public Function SetSecuritySite(ByVal pSite As System.IntPtr) As Integer Implements NativeInterfaces.IInternetSecurityManager.SetSecuritySite
        Return INET_E_DEFAULT_ACTION
    End Function

    Public Function MapUrlToZone(ByVal pwszUrl As String, ByRef pdwZone As UInteger, ByVal dwFlags As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.MapUrlToZone
        pdwZone = 0 // URLZONE_LOCAL_MACHINE ?
        Return S_OK // no difference
        // Return INET_E_DEFAULT_ACTION
    End Function

    Public Function ProcessUrlAction(ByVal pwszUrl As String, ByVal dwAction As UInteger, ByVal pPolicy As System.IntPtr, ByVal cbPolicy As UInteger, ByVal pContext As System.IntPtr, ByVal cbContext As UInteger, ByVal dwFlags As UInteger, ByVal dwReserved As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.ProcessUrlAction
        // Return INET_E_DEFAULT_ACTION
        Return S_OK // This is what made the difference
    End Function

    Public Function QueryCustomPolicy(ByVal pwszUrl As String, ByRef guidKey As System.Guid, ByRef ppPolicy As System.IntPtr, ByRef pcbPolicy As UInteger, ByVal pContext As System.IntPtr, ByVal cbContext As UInteger, ByVal dwReserved As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.QueryCustomPolicy
        ppPolicy = IntPtr.Zero
        pcbPolicy = 0
        Return INET_E_DEFAULT_ACTION
    End Function

    Public Function SetZoneMapping1(ByVal dwZone As UInteger, ByVal lpszPattern As String, ByVal dwFlags As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.SetZoneMapping
        Return INET_E_DEFAULT_ACTION
    End Function

    Public Function GetZoneMappings(ByVal dwZone As UInteger, ByRef ppenumString As System.Runtime.InteropServices.ComTypes.IEnumString, ByVal dwFlags As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.GetZoneMappings
        ppenumString = Nothing
        Return INET_E_DEFAULT_ACTION
    End Function

End Class

End Class

The Interfaces:

 <ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("6d5140c1-7436-11ce-8034-00aa006009fa")> _
Interface IServiceProvider
    <PreserveSig()> _
    Function QueryService(ByRef guidService As Guid, ByRef riid As Guid, ByRef ppvObject As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer
End Interface


<ComImport(), GuidAttribute("79EAC9EE-BAF9-11CE-8C82-00AA004BA90B"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _
Public Interface IInternetSecurityManager
    <PreserveSig()> _
    Function SetSecuritySite(<[In]()> ByVal pSite As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function GetSecuritySite(ByRef pSite As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function MapUrlToZone(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, ByRef pdwZone As UInt32, <[In]()> ByVal dwFlags As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function GetSecurityId(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, <Out()> ByVal pbSecurityId As IntPtr, <[In](), Out()> ByRef pcbSecurityId As UInt32, <[In]()> ByRef dwReserved As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function ProcessUrlAction(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, ByVal dwAction As UInt32, ByVal pPolicy As IntPtr, ByVal cbPolicy As UInt32, ByVal pContext As IntPtr, ByVal cbContext As UInt32, _
         ByVal dwFlags As UInt32, ByVal dwReserved As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function QueryCustomPolicy(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, ByRef guidKey As Guid, ByRef ppPolicy As IntPtr, ByRef pcbPolicy As UInt32, ByVal pContext As IntPtr, ByVal cbContext As UInt32, _
         ByVal dwReserved As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function SetZoneMapping(ByVal dwZone As UInt32, <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal lpszPattern As String, ByVal dwFlags As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer

    <PreserveSig()> _
    Function GetZoneMappings(<[In]()> ByVal dwZone As UInt32, ByRef ppenumString As ComTypes.IEnumString, <[In]()> ByVal dwFlags As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer
End Interface
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文