Skip to content

Avoid using undefined token header #466

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 1 commit into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 9 additions & 6 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { getProxyForUrl } from "./proxy"
import { Storage } from "./storage"
import { expandPath } from "./util"

export const coderSessionTokenHeader = "Coder-Session-Token"

/**
* Return whether the API will need a token for authorization.
* If mTLS is in use (as specified by the cert or key files being set) then
Expand Down Expand Up @@ -242,14 +244,15 @@ export async function waitForBuild(
const baseUrl = new URL(baseUrlRaw)
const proto = baseUrl.protocol === "https:" ? "wss:" : "ws:"
const socketUrlRaw = `${proto}//${baseUrl.host}${path}`
const token = restClient.getAxiosInstance().defaults.headers.common[coderSessionTokenHeader] as string | undefined
const socket = new ws.WebSocket(new URL(socketUrlRaw), {
headers: {
"Coder-Session-Token": restClient.getAxiosInstance().defaults.headers.common["Coder-Session-Token"] as
| string
| undefined,
},
followRedirects: true,
agent: agent,
followRedirects: true,
headers: token
? {
[coderSessionTokenHeader]: token,
}
: undefined,
})
socket.binaryType = "nodebuffer"
socket.on("message", (data) => {
Expand Down
15 changes: 8 additions & 7 deletions src/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Workspace, GetInboxNotificationResponse } from "coder/site/src/api/type
import { ProxyAgent } from "proxy-agent"
import * as vscode from "vscode"
import { WebSocket } from "ws"
import { coderSessionTokenHeader } from "./api"
import { errToStr } from "./api-helper"
import { type Storage } from "./storage"

Expand Down Expand Up @@ -37,15 +38,15 @@ export class Inbox implements vscode.Disposable {
const socketProto = baseUrl.protocol === "https:" ? "wss:" : "ws:"
const socketUrl = `${socketProto}//${baseUrl.host}/api/v2/notifications/inbox/watch?format=plaintext&templates=${watchTemplatesParam}&targets=${watchTargetsParam}`

const coderSessionTokenHeader = "Coder-Session-Token"
const token = restClient.getAxiosInstance().defaults.headers.common[coderSessionTokenHeader] as string | undefined
this.#socket = new WebSocket(new URL(socketUrl), {
followRedirects: true,
agent: httpAgent,
headers: {
[coderSessionTokenHeader]: restClient.getAxiosInstance().defaults.headers.common[coderSessionTokenHeader] as
| string
| undefined,
},
followRedirects: true,
headers: token
? {
[coderSessionTokenHeader]: token,
}
: undefined,
})

this.#socket.on("open", () => {
Expand Down