Skip to content

feat: add handling for insecure requests #106

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

Merged
merged 3 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix linting
  • Loading branch information
kylecarbs committed Jun 9, 2023
commit 5985fceaffc5ff98d7a353adb66d835fee3a8089
4 changes: 2 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { getAuthenticatedUser, getWorkspaces, updateWorkspaceVersion } from "cod
import { Workspace, WorkspaceAgent } from "coder/site/src/api/typesGenerated"
import * as vscode from "vscode"
import { extractAgents } from "./api-helper"
import { SelfSignedCertificateError } from "./error"
import { Remote } from "./remote"
import { Storage } from "./storage"
import { OpenableTreeItem } from "./workspacesProvider"
import { SelfSignedCertificateError } from "./error"

export class Commands {
public constructor(private readonly vscodeProposed: typeof vscode, private readonly storage: Storage) {}
Expand Down Expand Up @@ -64,7 +64,7 @@ export class Commands {
}
if (err instanceof SelfSignedCertificateError) {
err.showInsecureNotification(this.storage)

return {
message: err.message,
severity: vscode.InputBoxValidationSeverity.Error,
Expand Down
15 changes: 11 additions & 4 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as vscode from "vscode"
import * as fs from "fs/promises"
import * as jsonc from "jsonc-parser"
import * as vscode from "vscode"
import { Storage } from "./storage"

export class SelfSignedCertificateError extends Error {
public static Notification = "Your Coder deployment is using a self-signed certificate. VS Code uses a version of Electron that does not support registering self-signed intermediate certificates with extensions."
public static Notification =
"Your Coder deployment is using a self-signed certificate. VS Code uses a version of Electron that does not support registering self-signed intermediate certificates with extensions."
public static ActionAllowInsecure = "Allow Insecure"
public static ActionViewMoreDetails = "View More Details"

Expand All @@ -28,11 +29,17 @@ export class SelfSignedCertificateError extends Error {
const edits = jsonc.modify(settingsContent, ["coder.insecure"], true, {})
await fs.writeFile(storage.getUserSettingsPath(), jsonc.applyEdits(settingsContent, edits))

vscode.window.showInformationMessage("The Coder extension will no longer verify TLS on HTTPS requests. You can change this at any time with the \"coder.insecure\" property in your VS Code settings.")
vscode.window.showInformationMessage(
'The Coder extension will no longer verify TLS on HTTPS requests. You can change this at any time with the "coder.insecure" property in your VS Code settings.',
)
}

public async showInsecureNotification(storage: Storage): Promise<void> {
const value = await vscode.window.showErrorMessage(SelfSignedCertificateError.Notification, SelfSignedCertificateError.ActionAllowInsecure, SelfSignedCertificateError.ActionViewMoreDetails)
const value = await vscode.window.showErrorMessage(
SelfSignedCertificateError.Notification,
SelfSignedCertificateError.ActionAllowInsecure,
SelfSignedCertificateError.ActionViewMoreDetails,
)
if (value === SelfSignedCertificateError.ActionViewMoreDetails) {
await this.viewMoreDetails()
return
Expand Down
37 changes: 22 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import * as https from "https"
import * as module from "module"
import * as vscode from "vscode"
import { Commands } from "./commands"
import { SelfSignedCertificateError } from "./error"
import { Remote } from "./remote"
import { Storage } from "./storage"
import { WorkspaceQuery, WorkspaceProvider } from "./workspacesProvider"
import { SelfSignedCertificateError } from "./error"

export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
// The Remote SSH extension's proposed APIs are used to override
Expand All @@ -35,7 +35,6 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
// certificates if the insecure setting is true.
const applyInsecure = () => {
const insecure = Boolean(vscode.workspace.getConfiguration().get("coder.insecure"))
console.log("updating insecure", insecure)

axios.defaults.httpsAgent = new https.Agent({
// rejectUnauthorized defaults to true, so we need to explicitly set it to false
Expand All @@ -44,16 +43,19 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
})
}

axios.interceptors.response.use(r => r, (err) => {
if (err) {
const msg = err.toString() as string
if (msg.indexOf("unable to verify the first certificate") !== -1) {
throw new SelfSignedCertificateError(msg)
axios.interceptors.response.use(
(r) => r,
(err) => {
if (err) {
const msg = err.toString() as string
if (msg.indexOf("unable to verify the first certificate") !== -1) {
throw new SelfSignedCertificateError(msg)
}
}
}

throw err
})
throw err
},
)

vscode.workspace.onDidChangeConfiguration((e) => {
e.affectsConfiguration("coder.insecure") && applyInsecure()
Expand Down Expand Up @@ -143,11 +145,16 @@ export async function activate(ctx: vscode.ExtensionContext): Promise<void> {
await remote.setup(vscodeProposed.env.remoteAuthority)
} catch (ex) {
if (ex instanceof SelfSignedCertificateError) {
const prompt = await vscodeProposed.window.showErrorMessage("Failed to open workspace", {
detail: SelfSignedCertificateError.Notification,
modal: true,
useCustom: true,
}, SelfSignedCertificateError.ActionAllowInsecure, SelfSignedCertificateError.ActionViewMoreDetails)
const prompt = await vscodeProposed.window.showErrorMessage(
"Failed to open workspace",
{
detail: SelfSignedCertificateError.Notification,
modal: true,
useCustom: true,
},
SelfSignedCertificateError.ActionAllowInsecure,
SelfSignedCertificateError.ActionViewMoreDetails,
)
if (prompt === SelfSignedCertificateError.ActionAllowInsecure) {
await ex.allowInsecure(storage)
await remote.reloadWindow()
Expand Down