Skip to content

Commit e7945be

Browse files
committed
Enable password authentication by default
Fixes #1062.
1 parent 91f49e1 commit e7945be

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ yarn binary ${vscodeVersion} ${codeServerVersion} # Or you can package it into a
7373
## Security
7474

7575
### Authentication
76-
To enable built-in password authentication use `code-server --auth password`. By
77-
default it will use a randomly generated password but you can set the
78-
`$PASSWORD` environment variable to use your own.
76+
By default `code-server` enables password authentication using a randomly
77+
generated password. You can set the `PASSWORD` environment variable to use your
78+
own instead or use `--auth none` to disable password authentication.
7979

8080
Do not expose `code-server` to the open internet without some form of
8181
authentication.

src/node/cli.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const startVscode = async (): Promise<void | void[]> => {
8686
const args = getArgs();
8787
const extra = args["_"] || [];
8888
const options = {
89-
auth: args.auth,
89+
auth: args.auth || AuthType.Password,
9090
basePath: args["base-path"],
9191
cert: args.cert,
9292
certKey: args["cert-key"],
@@ -95,9 +95,9 @@ const startVscode = async (): Promise<void | void[]> => {
9595
password: process.env.PASSWORD,
9696
};
9797

98-
if (options.auth && enumToArray(AuthType).filter((t) => t === options.auth).length === 0) {
98+
if (enumToArray(AuthType).filter((t) => t === options.auth).length === 0) {
9999
throw new Error(`'${options.auth}' is not a valid authentication type.`);
100-
} else if (options.auth && !options.password) {
100+
} else if (options.auth === "password" && !options.password) {
101101
options.password = await generatePassword();
102102
}
103103

@@ -125,10 +125,13 @@ const startVscode = async (): Promise<void | void[]> => {
125125
]);
126126
logger.info(`Server listening on ${serverAddress}`);
127127

128-
if (options.auth && !process.env.PASSWORD) {
128+
if (options.auth === "password" && !process.env.PASSWORD) {
129129
logger.info(` - Password is ${options.password}`);
130-
logger.info(" - To use your own password, set the PASSWORD environment variable");
131-
} else if (options.auth) {
130+
logger.info(" - To use your own password, set the PASSWORD environment variable");
131+
if (!args.auth) {
132+
logger.info(" - To disable use `--auth none`");
133+
}
134+
} else if (options.auth === "password") {
132135
logger.info(" - Using custom password for authentication");
133136
} else {
134137
logger.info(" - No authentication");

src/node/server.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class HttpError extends Error {
110110
}
111111

112112
export interface ServerOptions {
113-
readonly auth?: AuthType;
113+
readonly auth: AuthType;
114114
readonly basePath?: string;
115115
readonly connectionToken?: string;
116116
readonly cert?: string;
@@ -133,7 +133,7 @@ export abstract class Server {
133133

134134
public constructor(options: ServerOptions) {
135135
this.options = {
136-
host: options.auth && options.cert ? "0.0.0.0" : "localhost",
136+
host: options.auth === "password" && options.cert ? "0.0.0.0" : "localhost",
137137
...options,
138138
basePath: options.basePath ? options.basePath.replace(/\/+$/, "") : "",
139139
};
@@ -269,7 +269,7 @@ export abstract class Server {
269269
base = path.normalize(base);
270270
requestPath = path.normalize(requestPath || "/index.html");
271271

272-
if (base !== "/login" || !this.options.auth || requestPath !== "/index.html") {
272+
if (base !== "/login" || this.options.auth !== "password" || requestPath !== "/index.html") {
273273
this.ensureGet(request);
274274
}
275275

@@ -300,7 +300,7 @@ export abstract class Server {
300300
response.cache = true;
301301
return response;
302302
case "/login":
303-
if (!this.options.auth || requestPath !== "/index.html") {
303+
if (this.options.auth !== "password" || requestPath !== "/index.html") {
304304
throw new HttpError("Not found", HttpCode.NotFound);
305305
}
306306
return this.tryLogin(request);
@@ -421,7 +421,7 @@ export abstract class Server {
421421
}
422422

423423
private authenticate(request: http.IncomingMessage, payload?: LoginPayload): boolean {
424-
if (!this.options.auth) {
424+
if (this.options.auth !== "password") {
425425
return true;
426426
}
427427
const safeCompare = localRequire<typeof import("safe-compare")>("safe-compare/index");

src/node/util.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { mkdirp } from "vs/base/node/pfs";
1414

1515
export enum AuthType {
1616
Password = "password",
17+
None = "none",
1718
}
1819

1920
export enum FormatType {
@@ -127,7 +128,7 @@ export const enumToArray = (t: any): string[] => {
127128

128129
export const buildAllowedMessage = (t: any): string => {
129130
const values = enumToArray(t);
130-
return `Allowed value${values.length === 1 ? " is" : "s are"} ${values.map((t) => `'${t}'`).join(",")}`;
131+
return `Allowed value${values.length === 1 ? " is" : "s are"} ${values.map((t) => `'${t}'`).join(", ")}`;
131132
};
132133

133134
/**

0 commit comments

Comments
 (0)