Skip to content

Commit a4a642c

Browse files
committed
Improve password handling
- Error out if auth is enabled but no password is passed in - Indicate password location on login page
1 parent 09378a6 commit a4a642c

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ rules:
2222
# For overloads.
2323
no-dupe-class-members: off
2424
"@typescript-eslint/no-use-before-define": off
25+
"@typescript-eslint/no-non-null-assertion": off
2526

2627
settings:
2728
# Does not work with CommonJS unfortunately.

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@
5656
"stylelint": "^13.0.0",
5757
"stylelint-config-recommended": "^3.0.0",
5858
"ts-node": "^8.4.1",
59-
"typescript": "3.7.2",
60-
"yarn": "^1.22.4"
59+
"typescript": "3.7.2"
6160
},
6261
"resolutions": {
6362
"@types/node": "^12.12.7",
@@ -79,7 +78,8 @@
7978
"tar": "^6.0.1",
8079
"tar-fs": "^2.0.0",
8180
"ws": "^7.2.0",
82-
"xdg-basedir": "^4.0.0"
81+
"xdg-basedir": "^4.0.0",
82+
"yarn": "^1.22.4"
8383
},
8484
"bin": {
8585
"code-server": "out/node/entry.js"

src/browser/pages/login.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<div class="card-box">
2727
<div class="header">
2828
<h1 class="main">Welcome to code-server</h1>
29-
<div class="sub">Please log in below. Check code-server's logs for the generated password.</div>
29+
<div class="sub">Please log in below. {{PASSWORD_MSG}}</div>
3030
</div>
3131
<div class="content">
3232
<form class="login-form" method="post">

src/node/app/login.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as http from "http"
22
import * as limiter from "limiter"
33
import * as querystring from "querystring"
44
import { HttpCode, HttpError } from "../../common/http"
5-
import { AuthType, HttpProvider, HttpResponse, Route } from "../http"
6-
import { hash } from "../util"
5+
import { AuthType, HttpProvider, HttpProviderOptions, HttpResponse, Route } from "../http"
6+
import { hash, humanPath } from "../util"
77

88
interface LoginPayload {
99
password?: string
@@ -18,6 +18,14 @@ interface LoginPayload {
1818
* Login HTTP provider.
1919
*/
2020
export class LoginHttpProvider extends HttpProvider {
21+
public constructor(
22+
options: HttpProviderOptions,
23+
private readonly configFile: string,
24+
private readonly envPassword: boolean,
25+
) {
26+
super(options)
27+
}
28+
2129
public async handleRequest(route: Route, request: http.IncomingMessage): Promise<HttpResponse> {
2230
if (this.options.auth !== AuthType.Password || !this.isRoot(route)) {
2331
throw new HttpError("Not found", HttpCode.NotFound)
@@ -46,6 +54,11 @@ export class LoginHttpProvider extends HttpProvider {
4654
public async getRoot(route: Route, error?: Error): Promise<HttpResponse> {
4755
const response = await this.getUtf8Resource(this.rootPath, "src/browser/pages/login.html")
4856
response.content = response.content.replace(/{{ERROR}}/, error ? `<div class="error">${error.message}</div>` : "")
57+
let passwordMsg = `Check the config file at ${humanPath(this.configFile)} for the password.`
58+
if (this.envPassword) {
59+
passwordMsg = "Password was set from $PASSWORD."
60+
}
61+
response.content = response.content.replace(/{{PASSWORD_MSG}}/g, passwordMsg)
4962
return this.replaceTemplates(route, response)
5063
}
5164

src/node/entry.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,21 @@ const main = async (cliArgs: Args): Promise<void> => {
3636
// This prioritizes the flags set in args over the ones in the config file.
3737
let args = Object.assign(configArgs, cliArgs)
3838

39+
if (!args.auth) {
40+
args = {
41+
...args,
42+
auth: AuthType.Password,
43+
}
44+
}
45+
3946
logger.trace(`Using extensions-dir at ${humanPath(args["extensions-dir"])}`)
4047
logger.trace(`Using user-data-dir at ${humanPath(args["user-data-dir"])}`)
4148

49+
const envPassword = process.env.PASSWORD !== undefined
4250
const password = args.auth === AuthType.Password && (process.env.PASSWORD || args.password)
51+
if (args.auth === AuthType.Password && !password) {
52+
throw new Error("Please pass in a password via the config file or $PASSWORD")
53+
}
4354
const [host, port] = bindAddrFromAllSources(cliArgs, configArgs)
4455

4556
// Spawn the main HTTP server.
@@ -69,7 +80,7 @@ const main = async (cliArgs: Args): Promise<void> => {
6980
const api = httpServer.registerHttpProvider("/api", ApiHttpProvider, httpServer, vscode, args["user-data-dir"])
7081
const update = httpServer.registerHttpProvider("/update", UpdateHttpProvider, false)
7182
httpServer.registerHttpProvider("/proxy", ProxyHttpProvider)
72-
httpServer.registerHttpProvider("/login", LoginHttpProvider)
83+
httpServer.registerHttpProvider("/login", LoginHttpProvider, args.config!, envPassword)
7384
httpServer.registerHttpProvider("/static", StaticHttpProvider)
7485
httpServer.registerHttpProvider("/dashboard", DashboardHttpProvider, api, update)
7586

@@ -79,15 +90,8 @@ const main = async (cliArgs: Args): Promise<void> => {
7990
const serverAddress = await httpServer.listen()
8091
logger.info(`HTTP server listening on ${serverAddress}`)
8192

82-
if (!args.auth) {
83-
args = {
84-
...args,
85-
auth: AuthType.Password,
86-
}
87-
}
88-
8993
if (args.auth === AuthType.Password) {
90-
if (process.env.PASSWORD) {
94+
if (envPassword) {
9195
logger.info(" - Using password from $PASSWORD")
9296
} else {
9397
logger.info(` - Using password from ${humanPath(args.config)}`)

0 commit comments

Comments
 (0)