Skip to content

Allow configuring trusted domains at run-time #7339

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
May 6, 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
1 change: 1 addition & 0 deletions patches/series
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ getting-started.diff
keepalive.diff
clipboard.diff
display-language.diff
trusted-domains.diff
49 changes: 49 additions & 0 deletions patches/trusted-domains.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Allow configuring trusted domains via product.json or flag.

Index: code-server/lib/vscode/src/vs/server/node/serverEnvironmentService.ts
===================================================================
--- code-server.orig/lib/vscode/src/vs/server/node/serverEnvironmentService.ts
+++ code-server/lib/vscode/src/vs/server/node/serverEnvironmentService.ts
@@ -20,6 +20,7 @@ export const serverOptions: OptionDescri
'disable-file-uploads': { type: 'boolean' },
'disable-getting-started-override': { type: 'boolean' },
'locale': { type: 'string' },
+ 'link-protection-trusted-domains': { type: 'string[]' },

/* ----- server setup ----- */

@@ -108,6 +109,7 @@ export interface ServerParsedArgs {
'disable-file-uploads'?: boolean;
'disable-getting-started-override'?: boolean,
'locale'?: string
+ 'link-protection-trusted-domains'?: string[],

/* ----- server setup ----- */

Index: code-server/lib/vscode/src/vs/server/node/webClientServer.ts
===================================================================
--- code-server.orig/lib/vscode/src/vs/server/node/webClientServer.ts
+++ code-server/lib/vscode/src/vs/server/node/webClientServer.ts
@@ -339,6 +339,14 @@ export class WebClientServer {
scopes: [['user:email'], ['repo']]
} : undefined;

+ const linkProtectionTrustedDomains: string[] = [];
+ if (this._environmentService.args['link-protection-trusted-domains']) {
+ linkProtectionTrustedDomains.push(...this._environmentService.args['link-protection-trusted-domains']);
+ }
+ if (this._productService.linkProtectionTrustedDomains) {
+ linkProtectionTrustedDomains.push(...this._productService.linkProtectionTrustedDomains);
+ }
+
const productConfiguration = {
codeServerVersion: this._productService.codeServerVersion,
rootEndpoint: rootBase,
@@ -353,6 +361,7 @@ export class WebClientServer {
telemetryEndpoint: this._productService.telemetryEndpoint,
embedderIdentifier: 'server-distro',
extensionsGallery: this._productService.extensionsGallery,
+ linkProtectionTrustedDomains,
} satisfies Partial<IProductConfiguration>;

if (!this._environmentService.isBuilt) {
28 changes: 19 additions & 9 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export enum LogLevel {
export class OptionalString extends Optional<string> {}

/**
* Code flags provided by the user.
* (VS) Code flags provided by the user.
*/
export interface UserProvidedCodeArgs {
"disable-telemetry"?: boolean
Expand All @@ -53,7 +53,9 @@ export interface UserProvidedCodeArgs {
"disable-getting-started-override"?: boolean
"disable-proxy"?: boolean
"session-socket"?: string
"abs-proxy-base-path"?: string
"link-protection-trusted-domains"?: string[]
// locale is used by both VS Code and code-server.
locale?: string
}

/**
Expand All @@ -73,7 +75,6 @@ export interface UserProvidedArgs extends UserProvidedCodeArgs {
enable?: string[]
help?: boolean
host?: string
locale?: string
port?: number
json?: boolean
log?: LogLevel
Expand All @@ -91,6 +92,7 @@ export interface UserProvidedArgs extends UserProvidedCodeArgs {
verbose?: boolean
"app-name"?: string
"welcome-text"?: string
"abs-proxy-base-path"?: string
/* Positional arguments. */
_?: string[]
}
Expand Down Expand Up @@ -194,6 +196,10 @@ export const options: Options<Required<UserProvidedArgs>> = {
enable: { type: "string[]" },
help: { type: "boolean", short: "h", description: "Show this output." },
json: { type: "boolean" },
"link-protection-trusted-domains": {
type: "string[]",
description: "Links matching a trusted domain can be opened without link protection.",
},
locale: {
// The preferred way to set the locale is via the UI.
type: "string",
Expand Down Expand Up @@ -707,12 +713,16 @@ export function parseConfigFile(configFile: string, configPath: string): ConfigA

// We convert the config file into a set of flags.
// This is a temporary measure until we add a proper CLI library.
const configFileArgv = Object.entries(config).map(([optName, opt]) => {
if (opt === true) {
return `--${optName}`
}
return `--${optName}=${opt}`
})
const configFileArgv = Object.entries(config)
.map(([optName, opt]) => {
if (opt === true) {
return `--${optName}`
} else if (Array.isArray(opt)) {
return opt.map((o) => `--${optName}=${o}`)
}
return `--${optName}=${opt}`
Comment on lines +718 to +723
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (opt === true) {
return `--${optName}`
} else if (Array.isArray(opt)) {
return opt.map((o) => `--${optName}=${o}`)
}
return `--${optName}=${opt}`
if (opt === true) {
return `--${optName}`
}
if (Array.isArray(opt)) {
return opt.map((o) => `--${optName}=${o}`)
}
return `--${optName}=${opt}`

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically equivalent, true, but stylistically I like having one less line 😛

})
.flat()
const args = parse(configFileArgv, {
configFile: configPath,
})
Expand Down
20 changes: 20 additions & 0 deletions test/unit/node/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
bindAddrFromArgs,
defaultConfigFile,
parse,
parseConfigFile,
setDefaults,
shouldOpenInExistingInstance,
toCodeArgs,
Expand Down Expand Up @@ -287,12 +288,17 @@ describe("parser", () => {
})

it("should support repeatable flags", async () => {
expect(() => parse(["--proxy-domain", ""])).toThrowError(/--proxy-domain requires a value/)
expect(parse(["--proxy-domain", "*.coder.com"])).toEqual({
"proxy-domain": ["*.coder.com"],
})
expect(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"])).toEqual({
"proxy-domain": ["*.coder.com", "test.com"],
})
// Commas are literal, at the moment.
expect(parse(["--proxy-domain", "*.coder.com,test.com"])).toEqual({
"proxy-domain": ["*.coder.com,test.com"],
})
})

it("should enforce cert-key with cert value or otherwise generate one", async () => {
Expand Down Expand Up @@ -490,6 +496,20 @@ describe("parser", () => {
}),
).toThrowError(expectedErrMsg)
})
it("should fail to parse invalid config", () => {
expect(() => parseConfigFile("test", "/fake-config-path")).toThrowError("invalid config: test")
})
it("should parse repeatable options", () => {
const configContents = `
install-extension:
- extension.number1
- extension.number2
`
expect(parseConfigFile(configContents, "/fake-config-path")).toEqual({
config: "/fake-config-path",
"install-extension": ["extension.number1", "extension.number2"],
})
})
it("should ignore optional strings set to false", async () => {
expect(parse(["--cert=false"])).toEqual({})
})
Expand Down
Loading