Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vscode.env.openExternal double encodes the data if you pass a param in the query that also requires encoding #135949

Closed
danechitoaie opened this issue Oct 27, 2021 · 3 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug *duplicate Issue identified as a duplicate of another issue(s) help wanted Issues identified as good community contribution opportunities uri

Comments

@danechitoaie
Copy link

danechitoaie commented Oct 27, 2021

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: Version: 1.61.2
  • OS Version: Darwin x64 20.6.0

Steps to Reproduce:
I have the following code in a VSC extension:

const callbackUri = await vscode.env.asExternalUri(vscode.Uri.parse(`${vscode.env.uriScheme}://${context.extension.id}/callback`));
const redirectUri = vscode.Uri.parse("http://localhost:3000/?callbackUri=" + encodeURIComponent(callbackUri.toString()) + "&test=ok");
await vscode.env.openExternal(redirectUri);

When vscode.env.openExternal is executed this messes up with the proper encoding of the URL query params and the browser is opened to a URL like: http://localhost:3000/?callbackUri=vscode://hello.vsc-develop/callback%253FwindowId%253D13&test=ok

Notice how the ? and = are encoded twice (%253F, %253D13)

I would expect callbackUri to be encoded as vscode%3A%2F%2Fhello.vsc-develop%2Fcallback%3FwindowId%3D13 as that how encodeURIComponent encodes it.

So two questions here:

  1. Why aren't the :// and / parts also encoded by vscode.Uri.parse + vscode.env.openExternal?
  2. And why is the ? and = characters encoded twice?
@mjbvz mjbvz added bug Issue identified by VS Code Team member as probable bug help wanted Issues identified as good community contribution opportunities labels Oct 27, 2021
@zardoy
Copy link
Contributor

zardoy commented Dec 9, 2021

There are also real-world cases in which vscode.env.openExternal would open incorrect URL.
For example I have an ext that contributes code action to test regex against strings at regex101

// Example: /.+:.+;/
// const urlToOpen = `https://regex101.com/?regex=${encodeURIComponent('.+:.+;')}?&flags=gi`
// resolved: https://regex101.com/?regex=.%2B%3A.%2B%3B%3F&flags=gi

At very first time I though this is problem with vscode.Uri.from and parse as they decode parts for some reason (like seriously, I didn't understand why then even this repo uses encodeURIComponent).

I was using workaround with a package called open for a while, but then realized from src that vscode.env.openExternal actually receives string, so you can just do that:

const callbackUri = await vscode.env.asExternalUri(vscode.Uri.parse(`${vscode.env.uriScheme}://${extensionCtx.extension.id}/callback`))
const redirectUrl = `http://localhost:3000/?callbackUri=${encodeURIComponent(callbackUri.toString())}&test=ok`
vscode.env.openExternal(redirectUrl as any)// doesn't change string

As I understand vscode.env.openExternal is just failing to use toString(true) on URI at some point.

The difference:

image

THIS should be definitely tested with openExternal as it fails on stable.

Why aren't the :// and / parts also encoded by vscode.Uri.parse + vscode.env.openExternal ?

Do you mean scheme? Its never being encoded

/cc @jrieken why there is no continuation of #83645? URL support would not only be extremely convenient in some cases, but I also believe would resolve the case described above. And why there isn't a word in API that from does decode components?

zardoy added a commit to zardoy/vscode-experiments that referenced this issue Dec 9, 2021
feat: basic fixCss (only command for now)
fix: openUrl correctly opens url (microsoft/vscode#135949) fixes regex issues
@jrieken jrieken added the uri label Dec 9, 2021
@jrieken
Copy link
Member

jrieken commented Dec 9, 2021

/duplicate of #85930

@github-actions github-actions bot locked and limited conversation to collaborators Jan 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug *duplicate Issue identified as a duplicate of another issue(s) help wanted Issues identified as good community contribution opportunities uri
Projects
None yet
Development

No branches or pull requests

5 participants
@jrieken @danechitoaie @mjbvz @zardoy and others