Skip to content

Commit 4f0789f

Browse files
code-asheryiliang114
authored andcommitted
Enable secret storage (coder#6450)
* Remove unused dependency patch * Enable secret storage based on local storage * Remove unnecessary GitHub auth patch It works now without the patch.
1 parent b3832ae commit 4f0789f

File tree

5 files changed

+53
-171
lines changed

5 files changed

+53
-171
lines changed

patches/base-path.diff

+22-2
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,35 @@ Index: code-server/lib/vscode/src/vs/code/browser/workbench/workbench.ts
265265
}
266266

267267
private startListening(): void {
268-
@@ -569,7 +570,7 @@ function readCookie(name: string): strin
268+
@@ -550,17 +551,6 @@ class WorkspaceProvider implements IWork
269+
}
270+
}
271+
272+
-function readCookie(name: string): string | undefined {
273+
- const cookies = document.cookie.split('; ');
274+
- for (const cookie of cookies) {
275+
- if (cookie.startsWith(name + '=')) {
276+
- return cookie.substring(name.length + 1);
277+
- }
278+
- }
279+
-
280+
- return undefined;
281+
-}
282+
-
283+
(function () {
284+
285+
// Find config by checking for DOM
286+
@@ -569,8 +559,8 @@ function readCookie(name: string): strin
269287
if (!configElement || !configElementAttribute) {
270288
throw new Error('Missing web configuration element');
271289
}
272290
- const config: IWorkbenchConstructionOptions & { folderUri?: UriComponents; workspaceUri?: UriComponents; callbackRoute: string } = JSON.parse(configElementAttribute);
291+
- const secretStorageKeyPath = readCookie('vscode-secret-key-path');
273292
+ const config: IWorkbenchConstructionOptions & { folderUri?: UriComponents; workspaceUri?: UriComponents; callbackRoute: string } = { ...JSON.parse(configElementAttribute), remoteAuthority: location.host }
274-
const secretStorageKeyPath = readCookie('vscode-secret-key-path');
293+
+ const secretStorageKeyPath = (window.location.pathname + "/mint-key").replace(/\/\/+/g, "/");
275294
const secretStorageCrypto = secretStorageKeyPath && ServerKeyedAESCrypto.supported()
276295
? new ServerKeyedAESCrypto(secretStorageKeyPath) : new TransparentCrypto();
296+
277297
Index: code-server/lib/vscode/src/vs/platform/extensionResourceLoader/common/extensionResourceLoader.ts
278298
===================================================================
279299
--- code-server.orig/lib/vscode/src/vs/platform/extensionResourceLoader/common/extensionResourceLoader.ts

patches/dependencies.diff

-62
This file was deleted.

patches/github-auth.diff

-106
This file was deleted.

patches/series

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ update-check.diff
99
logout.diff
1010
store-socket.diff
1111
proxy-uri.diff
12-
github-auth.diff
1312
unique-db.diff
1413
local-storage.diff
1514
service-worker.diff

src/node/routes/vscode.ts

+31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { logger } from "@coder/logger"
2+
import * as crypto from "crypto"
23
import * as express from "express"
4+
import { promises as fs } from "fs"
35
import * as http from "http"
46
import * as net from "net"
57
import * as path from "path"
@@ -32,6 +34,7 @@ export class CodeServerRouteWrapper {
3234
private _wsRouterWrapper = WsRouter()
3335
private _socketProxyProvider = new SocketProxyProvider()
3436
public router = express.Router()
37+
private mintKeyPromise: Promise<Buffer> | undefined
3538

3639
public get wsRouter() {
3740
return this._wsRouterWrapper.router
@@ -66,6 +69,33 @@ export class CodeServerRouteWrapper {
6669
)
6770
}
6871

72+
private mintKey: express.Handler = async (req, res, next) => {
73+
if (!this.mintKeyPromise) {
74+
this.mintKeyPromise = new Promise(async (resolve) => {
75+
const keyPath = path.join(req.args["user-data-dir"], "serve-web-key-half")
76+
logger.debug(`Reading server web key half from ${keyPath}`)
77+
try {
78+
resolve(await fs.readFile(keyPath))
79+
return
80+
} catch (error: any) {
81+
if (error.code !== "ENOENT") {
82+
logError(logger, `read ${keyPath}`, error)
83+
}
84+
}
85+
// VS Code wants 256 bits.
86+
const key = crypto.randomBytes(32)
87+
try {
88+
await fs.writeFile(keyPath, key)
89+
} catch (error: any) {
90+
logError(logger, `write ${keyPath}`, error)
91+
}
92+
resolve(key)
93+
})
94+
}
95+
const key = await this.mintKeyPromise
96+
res.end(key)
97+
}
98+
6999
private $root: express.Handler = async (req, res, next) => {
70100
const isAuthenticated = await authenticated(req)
71101
const NO_FOLDER_OR_WORKSPACE_QUERY = !req.query.folder && !req.query.workspace
@@ -173,6 +203,7 @@ export class CodeServerRouteWrapper {
173203
constructor() {
174204
this.router.get("/", this.ensureCodeServerLoaded, this.$root)
175205
this.router.get("/manifest.json", this.manifest)
206+
this.router.post("/mint-key", this.mintKey)
176207
this.router.all("*", ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyRequest)
177208
this._wsRouterWrapper.ws("*", ensureOrigin, ensureAuthenticated, this.ensureCodeServerLoaded, this.$proxyWebsocket)
178209
}

0 commit comments

Comments
 (0)