Skip to content

Add hashedPassword config #2409

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
Dec 8, 2020
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
3 changes: 3 additions & 0 deletions doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ and then restart `code-server` with:
sudo systemctl restart code-server@$USER
```

Alternatively, you can specify the SHA-256 of your password at the `hashedPassword` field in the config file.
The `hashedPassword` field takes precedence over `password`.

### How do I securely access development web services?

If you're working on a web service and want to access it locally, `code-server` can proxy it for you.
Expand Down
24 changes: 22 additions & 2 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface Args extends VsArgs {
config?: string
auth?: AuthType
password?: string
hashedPassword?: string
cert?: OptionalString
"cert-host"?: string
"cert-key"?: string
Expand Down Expand Up @@ -104,6 +105,12 @@ const options: Options<Required<Args>> = {
type: "string",
description: "The password for password authentication (can only be passed in via $PASSWORD or the config file).",
},
hashedPassword: {
type: "string",
description:
"The password hashed with SHA-256 for password authentication (can only be passed in via $HASHED_PASSWORD or the config file). \n" +
"Takes precedence over 'password'.",
},
cert: {
type: OptionalString,
path: true,
Expand Down Expand Up @@ -279,6 +286,10 @@ export const parse = (
throw new Error("--password can only be set in the config file or passed in via $PASSWORD")
}

if (key === "hashedPassword" && !opts?.configFile) {
throw new Error("--hashedPassword can only be set in the config file or passed in via $HASHED_PASSWORD")
}

const option = options[key]
if (option.type === "boolean") {
;(args[key] as boolean) = true
Expand Down Expand Up @@ -361,6 +372,7 @@ export interface DefaultedArgs extends ConfigArgs {
"proxy-domain": string[]
verbose: boolean
usingEnvPassword: boolean
usingEnvHashedPassword: boolean
"extensions-dir": string
"user-data-dir": string
}
Expand Down Expand Up @@ -448,13 +460,20 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi
args["cert-key"] = certKey
}

const usingEnvPassword = !!process.env.PASSWORD
let usingEnvPassword = !!process.env.PASSWORD
if (process.env.PASSWORD) {
args.password = process.env.PASSWORD
}

// Ensure it's not readable by child processes.
const usingEnvHashedPassword = !!process.env.HASHED_PASSWORD
if (process.env.HASHED_PASSWORD) {
args.hashedPassword = process.env.HASHED_PASSWORD
usingEnvPassword = false
}

// Ensure they're not readable by child processes.
delete process.env.PASSWORD
delete process.env.HASHED_PASSWORD

// Filter duplicate proxy domains and remove any leading `*.`.
const proxyDomains = new Set((args["proxy-domain"] || []).map((d) => d.replace(/^\*\./, "")))
Expand All @@ -463,6 +482,7 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi
return {
...args,
usingEnvPassword,
usingEnvHashedPassword,
} as DefaultedArgs // TODO: Technically no guarantee this is fulfilled.
}

Expand Down
8 changes: 6 additions & 2 deletions src/node/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ const main = async (args: DefaultedArgs): Promise<void> => {
logger.info(`Using user-data-dir ${humanPath(args["user-data-dir"])}`)
logger.trace(`Using extensions-dir ${humanPath(args["extensions-dir"])}`)

if (args.auth === AuthType.Password && !args.password) {
throw new Error("Please pass in a password via the config file or $PASSWORD")
if (args.auth === AuthType.Password && !args.password && !args.hashedPassword) {
throw new Error(
"Please pass in a password via the config file or environment variable ($PASSWORD or $HASHED_PASSWORD)",
)
}

const [app, wsApp, server] = await createApp(args)
Expand All @@ -114,6 +116,8 @@ const main = async (args: DefaultedArgs): Promise<void> => {
logger.info(" - Authentication is enabled")
if (args.usingEnvPassword) {
logger.info(" - Using password from $PASSWORD")
} else if (args.usingEnvHashedPassword) {
logger.info(" - Using password from $HASHED_PASSWORD")
} else {
logger.info(` - Using password from ${humanPath(args.config)}`)
}
Expand Down
7 changes: 6 additions & 1 deletion src/node/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ export const authenticated = (req: express.Request): boolean => {
return true
case AuthType.Password:
// The password is stored in the cookie after being hashed.
return req.args.password && req.cookies.key && safeCompare(req.cookies.key, hash(req.args.password))
return !!(
req.cookies.key &&
(req.args.hashedPassword
? safeCompare(req.cookies.key, req.args.hashedPassword)
: req.args.password && safeCompare(req.cookies.key, hash(req.args.password)))
)
default:
throw new Error(`Unsupported auth type ${req.args.auth}`)
}
Expand Down
8 changes: 7 additions & 1 deletion src/node/routes/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const getRoot = async (req: Request, error?: Error): Promise<string> => {
let passwordMsg = `Check the config file at ${humanPath(req.args.config)} for the password.`
if (req.args.usingEnvPassword) {
passwordMsg = "Password was set from $PASSWORD."
} else if (req.args.usingEnvHashedPassword) {
passwordMsg = "Password was set from $HASHED_PASSWORD."
}
return replaceTemplates(
req,
Expand Down Expand Up @@ -65,7 +67,11 @@ router.post("/", async (req, res) => {
throw new Error("Missing password")
}

if (req.args.password && safeCompare(req.body.password, req.args.password)) {
if (
req.args.hashedPassword
? safeCompare(hash(req.body.password), req.args.hashedPassword)
: req.args.password && safeCompare(req.body.password, req.args.password)
) {
// The hash does not add any actual security but we do it for
// obfuscation purposes (and as a side effect it handles escaping).
res.cookie(Cookie.Key, hash(req.body.password), {
Expand Down
16 changes: 16 additions & 0 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe("parser", () => {
port: 8080,
"proxy-domain": [],
usingEnvPassword: false,
usingEnvHashedPassword: false,
"extensions-dir": path.join(paths.data, "extensions"),
"user-data-dir": paths.data,
}
Expand Down Expand Up @@ -290,6 +291,21 @@ describe("parser", () => {
})
})

it("should use env var hashed password", async () => {
process.env.HASHED_PASSWORD = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08" // test
const args = parse([])
assert.deepEqual(args, {
_: [],
})

assert.deepEqual(await setDefaults(args), {
...defaults,
_: [],
hashedPassword: "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08",
usingEnvHashedPassword: true,
})
})

it("should filter proxy domains", async () => {
const args = parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "coder.com", "--proxy-domain", "coder.org"])
assert.deepEqual(args, {
Expand Down