|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Gitpod. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +import * as http from 'http'; |
| 6 | +import * as crypto from 'crypto'; |
| 7 | +import { args, ServerParsedArgs } from 'vs/server/node/args'; |
| 8 | +import { ILogService } from 'vs/platform/log/common/log'; |
| 9 | + |
| 10 | +export function sanitizeString(str: string): string { |
| 11 | + return typeof str === 'string' && str.trim().length > 0 ? str.trim() : ''; |
| 12 | +} |
| 13 | + |
| 14 | +export function parseCookies(request: http.IncomingMessage): Record<string, string> { |
| 15 | + const cookies: Record<string, string> = {}; |
| 16 | + const rc = request.headers.cookie; |
| 17 | + |
| 18 | + if (rc) { |
| 19 | + rc.split(';').forEach(cookie => { |
| 20 | + let parts = cookie.split('='); |
| 21 | + if (parts.length > 0) { |
| 22 | + const name = parts.shift()!.trim(); |
| 23 | + let value = decodeURI(parts.join('=')); |
| 24 | + cookies[name] = value; |
| 25 | + } |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + return cookies; |
| 30 | +} |
| 31 | + |
| 32 | +export async function authenticated(args: ServerParsedArgs, req: http.IncomingMessage): Promise<boolean> { |
| 33 | + if (!args.password) { |
| 34 | + return true; |
| 35 | + } |
| 36 | + const cookies = parseCookies(req); |
| 37 | + return isHashMatch(args.password || '', sanitizeString(cookies.key)); |
| 38 | +}; |
| 39 | + |
| 40 | +interface PasswordValidation { |
| 41 | + valid: boolean |
| 42 | + hashed: string |
| 43 | +} |
| 44 | + |
| 45 | +interface HandlePasswordValidationArgs { |
| 46 | + reqPassword: string | undefined |
| 47 | + argsPassword: string | undefined |
| 48 | +} |
| 49 | + |
| 50 | +function safeCompare(a: string, b: string): boolean { |
| 51 | + return a.length === b.length && crypto.timingSafeEqual(Buffer.from(a), Buffer.from(b)); |
| 52 | +} |
| 53 | + |
| 54 | +export async function generateAndSetPassword(logService: ILogService, length = 24): Promise<void> { |
| 55 | + if (args.password || !length) { |
| 56 | + return; |
| 57 | + } |
| 58 | + const password = await generatePassword(length); |
| 59 | + args.password = password; |
| 60 | + logService.info(`Automatically generated password\r\n ${password}`); |
| 61 | +} |
| 62 | + |
| 63 | +export async function generatePassword(length = 24): Promise<string> { |
| 64 | + const buffer = Buffer.alloc(Math.ceil(length / 2)); |
| 65 | + await new Promise(resolve => { |
| 66 | + crypto.randomFill(buffer, (_, buf) => resolve(buf)); |
| 67 | + }); |
| 68 | + return buffer.toString('hex').substring(0, length); |
| 69 | +} |
| 70 | + |
| 71 | +export function hash(str: string): string { |
| 72 | + return crypto.createHash('sha256').update(str).digest('hex'); |
| 73 | +} |
| 74 | + |
| 75 | +export function isHashMatch(password: string, hashPassword: string): boolean { |
| 76 | + const hashed = hash(password); |
| 77 | + return safeCompare(hashed, hashPassword); |
| 78 | +} |
| 79 | + |
| 80 | +export async function handlePasswordValidation({ argsPassword: passwordFromArgs, reqPassword: passwordFromRequestBody }: HandlePasswordValidationArgs): Promise<PasswordValidation> { |
| 81 | + if (passwordFromRequestBody) { |
| 82 | + const valid = passwordFromArgs ? safeCompare(passwordFromRequestBody, passwordFromArgs) : false; |
| 83 | + const hashed = hash(passwordFromRequestBody); |
| 84 | + return { |
| 85 | + valid, |
| 86 | + hashed |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + return { |
| 91 | + valid: false, |
| 92 | + hashed: '' |
| 93 | + } |
| 94 | +} |
0 commit comments