amodm/webbrowser-rs

Opening https:// links opens a temporary file on Windows 10 when using Firefox or Chrome as default browser

Closed this issue · 7 comments

drojf commented

Hi, I'm not sure if I'm doing something wrong here since I just started using this library. I have previously used the python webbrowser library before, though.

I'm on Windows 10

Note that Microsoft Edge does not have this issue.

I briefly looked at the Windows code and I'm guessing you need to specify https as the URI protocol scheme/lpClass argument when using a https URLs (currently it always specifies http), but I haven't tested whether that fixes it.

I usually use the Python webbrowser library, and that can open https links fine in Firefox.

Opening HTTP:// links

The following works fine for me on Firefox/Chrome. When I use the link http://github.com, it opens https://github.com

fn main() {
    webbrowser::open("http://github.com").unwrap(); // HTTP link
}

Opening HTTPS:// links

However, if I use the link https://github.com, it opens some sort of temporary file related to the website being opened:

file:///C:/Users/drojf/AppData/Local/Microsoft/Windows/INetCache/IE/5M7ZY6EG/2F611R12

On Firefox, this actually shows a github page, but it is as if I'm not logged into github, so I'm guessing it's opening a temporary file from Microsoft Edge

fn main() {
    webbrowser::open("https://github.com").unwrap(); // HTTPS link
}

Firefox:
image

Chrome:

image

amodm commented

@drojf, sorry that you're facing this, but this is very unusual and I'm unable to reproduce this in 2 different windows setups, so will need your help in debugging it a little.

Can you please copy the following, and run it in powershell.exe, and share the output?

$Signature = @"
using System;
using System.Runtime.InteropServices;
using System.Text;
public static class Win32Api
{

    [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra,[Out] System.Text.StringBuilder pszOut, ref uint pcchOut);

    public static string GetDefaultBrowser()
    {
        AssocF assocF = AssocF.IsProtocol;
        AssocStr association = AssocStr.Command;
        string assocString = "http";

        uint length = 1024; // we assume 1k is sufficient memory to hold the command
        var sb = new System.Text.StringBuilder((int) length);
        uint ret = ret = AssocQueryString(assocF, association, assocString, null, sb, ref length);

        return (ret != 0) ? null : sb.ToString();
    }

    [Flags]
    internal enum AssocF : uint
    {
        IsProtocol = 0x1000,
    }

    internal enum AssocStr
    {
        Command = 1,
        Executable,
    }
}
"@

Add-Type -TypeDefinition $Signature

Write-Output $([Win32Api]::GetDefaultBrowser())

Is the output different, if you were to replace string assocString = "http"; with string assocString = "https";

Thanks

amodm commented

The lpClass argument is used only to determine the default handler that needs to be invoked. It shouldn't matter, unless the handlers are different. At the minimum, it shouldn't really open a file like the way it's doing right now.

amodm commented

Never mind, I was too hasty in reading this issue. I'm able to reproduce this when using Firefox. There's no actionable on you for this @drojf - I'll look into it.

drojf commented

oh ok, good to know you could reproduce it

FWIW I tested on my Windows 11 Laptop (another computer) and could reproduce it on Firefox as well


The output seems to be the same either way, so I guess the lpClass arg isn't the problem

http

PS C:\temp\New folder (8)> powershell -ExecutionPolicy Bypass -File test.ps1
"C:\Program Files\Mozilla Firefox\firefox.exe" -osint -url "%1"

https

PS C:\temp\New folder (8)> powershell -ExecutionPolicy Bypass -File test.ps1
"C:\Program Files\Mozilla Firefox\firefox.exe" -osint -url "%1"
amodm commented

@drojf, this should be fixed now. Can you please check it with the latest push to main?

You can use it by specifying in your Cargo.toml:

[dependencies]
webbrowser = { git = "https://github.com/amodm/webbrowser-rs" }

I've tested it on my sandboxes, and it works. If this looks good, I'd like to release it without delay.

amodm commented

Given the nature of the issue, and my testing, I've put out a release for this: v0.8.4.

I'm treating this as closed, unless your testing throws up otherwise, in which case this can be reopened.

drojf commented

Thanks for fixing this, I re-tested my minimal test case, and also my actual application I was using it in, and they both work fine now.