Repository: stateful/vscode-runme
Pull request #185 (2023-02-09 03:46:16)

Sanitize urls before using them in terminal
cc @sourishkrout 


Patch:
diff --git a/src/extension/handler/uri.ts b/src/extension/handler/uri.ts
index 49072210..2b61123b 100644
--- a/src/extension/handler/uri.ts
+++ b/src/extension/handler/uri.ts
@@ -25,8 +25,8 @@ export class RunmeUriHandler implements UriHandler {
         }
 
         if (command === 'setup') {
-            const fileToOpen = params.get('fileToOpen') || DEFAULT_START_FILE
-            const repository = params.get('repository')!
+            const fileToOpen = Uri.parse(params.get('fileToOpen') || DEFAULT_START_FILE).toString()
+            const repository = params.get('repository')
 
             if (!repository && fileToOpen.match(REGEX_WEB_RESOURCE)) {
                 TelemetryReporter.sendTelemetryEvent('extension.uriHandler', { command, type: 'file' })
@@ -35,7 +35,7 @@ export class RunmeUriHandler implements UriHandler {
             }
 
             TelemetryReporter.sendTelemetryEvent('extension.uriHandler', { command, type: 'project' })
-            await this._setupProject(fileToOpen, repository)
+            await this._setupProject(fileToOpen, Uri.parse(repository as string).toString())
             return
         }
 


Test patch:
diff --git a/tests/extension/handler/uri.test.ts b/tests/extension/handler/uri.test.ts
index ba0f0080..e3c5e8d7 100644
--- a/tests/extension/handler/uri.test.ts
+++ b/tests/extension/handler/uri.test.ts
@@ -57,22 +57,25 @@ describe('RunmeUriHandler', () => {
         })
 
         it('runs _setupProject if command was "setup"', async () => {
-            vi.mocked(Uri.parse).mockReturnValue({ query: {
+            vi.mocked(Uri.parse).mockReturnValue({ toString: () => 'some url', query: {
                 command: 'setup',
                 fileToOpen: '/sub/file.md',
                 repository: 'git@github.com:/foo/bar'
             }} as any)
             await handler.handleUri(Uri.parse('vscode://stateful.runme?foo=bar'))
-            expect(handler['_setupProject']).toBeCalledWith('/sub/file.md', 'git@github.com:/foo/bar')
+            expect(handler['_setupProject']).toBeCalledWith('some url', 'some url')
             expect(TelemetryReporter.sendTelemetryEvent)
               .toBeCalledWith('extension.uriHandler', { command: 'setup', type: 'project' })
         })
 
         it('runs _setupFile if command was "setup" but no repository param', async () => {
             const fileToOpen = 'https://raw.githubusercontent.com/stateful/vscode-runme/main/examples/k8s/README.md'
-            vi.mocked(Uri.parse).mockReturnValue({ query: { command: 'setup', fileToOpen }, fsPath: '/foo/bar' } as any)
+            vi.mocked(Uri.parse).mockReturnValue({
+              toString: () => 'https://some url',
+              query: { command: 'setup', fileToOpen }, fsPath: '/foo/bar' } as any
+            )
             await handler.handleUri(Uri.parse('vscode://stateful.runme?foo=bar'))
-            expect(handler['_setupFile']).toBeCalledWith(fileToOpen)
+            expect(handler['_setupFile']).toBeCalledWith('https://some url')
             expect(TelemetryReporter.sendTelemetryEvent)
               .toBeCalledWith('extension.uriHandler', { command: 'setup', type: 'file' })
         })