http.ts 9.6 KB
Newer Older
A
Asher 已提交
1
import { field, logger } from "@coder/logger"
A
Asher 已提交
2 3
import * as express from "express"
import * as expressCore from "express-serve-static-core"
4 5
import * as http from "http"
import * as net from "net"
6
import * as qs from "qs"
7
import { Disposable } from "../common/emitter"
T
Teffen 已提交
8
import { CookieKeys, HttpCode, HttpError } from "../common/http"
9
import { normalize } from "../common/util"
A
Asher 已提交
10
import { AuthType, DefaultedArgs } from "./cli"
11
import { version as codeServerVersion } from "./constants"
A
Asher 已提交
12
import { Heart } from "./heart"
13
import { getPasswordMethod, IsCookieValidArgs, isCookieValid, sanitizeString, escapeHtml, escapeJSON } from "./util"
A
Asher 已提交
14

15 16 17 18 19
/**
 * Base options included on every page.
 */
export interface ClientConfiguration {
  codeServerVersion: string
A
Asher 已提交
20
  /** Relative path from this page to the root.  No trailing slash. */
21
  base: string
A
Asher 已提交
22
  /** Relative path from this page to the static root.  No trailing slash. */
23 24 25
  csStaticBase: string
}

A
Asher 已提交
26 27 28 29 30 31 32 33 34 35
declare global {
  // eslint-disable-next-line @typescript-eslint/no-namespace
  namespace Express {
    export interface Request {
      args: DefaultedArgs
      heart: Heart
    }
  }
}

36
export const createClientConfiguration = (req: express.Request): ClientConfiguration => {
A
Asher 已提交
37
  const base = relativeRoot(req.originalUrl)
38 39 40

  return {
    base,
A
Asher 已提交
41
    csStaticBase: base + "/_static",
42 43 44 45
    codeServerVersion,
  }
}

A
Asher 已提交
46
/**
A
Asher 已提交
47
 * Replace common variable strings in HTML templates.
A
Asher 已提交
48
 */
A
Asher 已提交
49 50 51 52 53
export const replaceTemplates = <T extends object>(
  req: express.Request,
  content: string,
  extraOpts?: Omit<T, "base" | "csStaticBase" | "logLevel">,
): string => {
54
  const serverOptions: ClientConfiguration = {
55
    ...createClientConfiguration(req),
A
Asher 已提交
56 57
    ...extraOpts,
  }
58

A
Asher 已提交
59
  return content
60
    .replace(/{{TO}}/g, (typeof req.query.to === "string" && escapeHtml(req.query.to)) || "/")
61 62 63
    .replace(/{{BASE}}/g, serverOptions.base)
    .replace(/{{CS_STATIC_BASE}}/g, serverOptions.csStaticBase)
    .replace("{{OPTIONS}}", () => escapeJSON(serverOptions))
A
Asher 已提交
64 65 66
}

/**
A
Asher 已提交
67
 * Throw an error if not authorized. Call `next` if provided.
A
Asher 已提交
68
 */
69 70 71 72 73 74 75
export const ensureAuthenticated = async (
  req: express.Request,
  _?: express.Response,
  next?: express.NextFunction,
): Promise<void> => {
  const isAuthenticated = await authenticated(req)
  if (!isAuthenticated) {
A
Asher 已提交
76
    throw new HttpError("Unauthorized", HttpCode.Unauthorized)
77
  }
A
Asher 已提交
78 79 80
  if (next) {
    next()
  }
A
Asher 已提交
81 82
}

A
Asher 已提交
83 84 85
/**
 * Return true if authenticated via cookies.
 */
86
export const authenticated = async (req: express.Request): Promise<boolean> => {
A
Asher 已提交
87
  switch (req.args.auth) {
88
    case AuthType.None: {
A
Asher 已提交
89
      return true
90 91
    }
    case AuthType.Password: {
A
Asher 已提交
92
      // The password is stored in the cookie after being hashed.
93 94 95 96
      const hashedPasswordFromArgs = req.args["hashed-password"]
      const passwordMethod = getPasswordMethod(hashedPasswordFromArgs)
      const isCookieValidArgs: IsCookieValidArgs = {
        passwordMethod,
T
Teffen 已提交
97
        cookieKey: sanitizeString(req.cookies[CookieKeys.Session]),
98 99 100 101 102
        passwordFromArgs: req.args.password || "",
        hashedPasswordFromArgs: req.args["hashed-password"],
      }

      return await isCookieValid(isCookieValidArgs)
103 104
    }
    default: {
A
Asher 已提交
105
      throw new Error(`Unsupported auth type ${req.args.auth}`)
106
    }
A
Asher 已提交
107
  }
A
Asher 已提交
108 109
}

A
Asher 已提交
110 111
/**
 * Get the relative path that will get us to the root of the page. For each
A
Asher 已提交
112 113 114 115
 * slash we need to go up a directory.  Will not have a trailing slash.
 *
 * For example:
 *
A
Asher 已提交
116 117 118 119 120
 * / => .
 * /foo => .
 * /foo/ => ./..
 * /foo/bar => ./..
 * /foo/bar/ => ./../..
A
Asher 已提交
121 122 123 124 125 126 127 128 129 130
 *
 * All paths must be relative in order to work behind a reverse proxy since we
 * we do not know the base path.  Anything that needs to be absolute (for
 * example cookies) must get the base path from the frontend.
 *
 * All relative paths must be prefixed with the relative root to ensure they
 * work no matter the depth at which they happen to appear.
 *
 * For Express `req.originalUrl` should be used as they remove the base from the
 * standard `url` property making it impossible to get the true depth.
A
Asher 已提交
131
 */
A
Asher 已提交
132 133
export const relativeRoot = (originalUrl: string): string => {
  const depth = (originalUrl.split("?", 1)[0].match(/\//g) || []).length
A
Asher 已提交
134
  return normalize("./" + (depth > 1 ? "../".repeat(depth - 1) : ""))
A
Asher 已提交
135 136
}

A
Asher 已提交
137
/**
138 139
 * Redirect relatively to `/${to}`. Query variables on the current URI will be preserved.
 * `to` should be a simple path without any query parameters
A
Asher 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
 * `override` will merge with the existing query (use `undefined` to unset).
 */
export const redirect = (
  req: express.Request,
  res: express.Response,
  to: string,
  override: expressCore.Query = {},
): void => {
  const query = Object.assign({}, req.query, override)
  Object.keys(override).forEach((key) => {
    if (typeof override[key] === "undefined") {
      delete query[key]
    }
  })

A
Asher 已提交
155
  const relativePath = normalize(`${relativeRoot(req.originalUrl)}/${to}`, true)
A
Asher 已提交
156 157 158 159
  const queryString = qs.stringify(query)
  const redirectPath = `${relativePath}${queryString ? `?${queryString}` : ""}`
  logger.debug(`redirecting from ${req.originalUrl} to ${redirectPath}`)
  res.redirect(redirectPath)
160 161
}

A
Asher 已提交
162
/**
A
Asher 已提交
163
 * Get the value that should be used for setting a cookie domain. This will
164 165 166
 * allow the user to authenticate once no matter what sub-domain they use to log
 * in. This will use the highest level proxy domain (e.g. `coder.com` over
 * `test.coder.com` if both are specified).
A
Asher 已提交
167
 */
A
Asher 已提交
168 169 170
export const getCookieDomain = (host: string, proxyDomains: string[]): string | undefined => {
  const idx = host.lastIndexOf(":")
  host = idx !== -1 ? host.substring(0, idx) : host
171 172
  // If any of these are true we will still set cookies but without an explicit
  // `Domain` attribute on the cookie.
A
Asher 已提交
173
  if (
174
    // The host can be be blank or missing so there's nothing we can set.
A
Asher 已提交
175 176
    !host ||
    // IP addresses can't have subdomains so there's no value in setting the
177 178
    // domain for them. Assume that anything with a : is ipv6 (valid domain name
    // characters are alphanumeric or dashes)...
A
Asher 已提交
179
    host.includes(":") ||
180
    // ...and that anything entirely numbers and dots is ipv4 (currently tlds
A
Asher 已提交
181 182
    // cannot be entirely numbers).
    !/[^0-9.]/.test(host) ||
183 184
    // localhost subdomains don't seem to work at all (browser bug?). A cookie
    // set at dev.localhost cannot be read by 8080.dev.localhost.
A
Asher 已提交
185
    host.endsWith(".localhost") ||
186 187 188 189 190 191 192 193
    // Domains without at least one dot (technically two since domain.tld will
    // become .domain.tld) are considered invalid according to the spec so don't
    // set the domain for them. In my testing though localhost is the only
    // problem (the browser just doesn't store the cookie at all). localhost has
    // an additional problem which is that a reverse proxy might give
    // code-server localhost even though the domain is really domain.tld (by
    // default NGINX does this).
    !host.includes(".")
A
Asher 已提交
194 195 196 197 198 199 200 201 202 203 204 205
  ) {
    logger.debug("no valid cookie doman", field("host", host))
    return undefined
  }

  proxyDomains.forEach((domain) => {
    if (host.endsWith(domain) && domain.length < host.length) {
      host = domain
    }
  })

  logger.debug("got cookie doman", field("host", host))
A
Asher 已提交
206
  return host || undefined
A
Asher 已提交
207
}
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

/**
 * Return a function capable of fully disposing an HTTP server.
 */
export function disposer(server: http.Server): Disposable["dispose"] {
  const sockets = new Set<net.Socket>()
  let cleanupTimeout: undefined | NodeJS.Timeout

  server.on("connection", (socket) => {
    sockets.add(socket)

    socket.on("close", () => {
      sockets.delete(socket)

      if (cleanupTimeout && sockets.size === 0) {
        clearTimeout(cleanupTimeout)
        cleanupTimeout = undefined
      }
    })
  })

  return () => {
    return new Promise<void>((resolve, reject) => {
      // The whole reason we need this disposer is because close will not
      // actually close anything; it only prevents future connections then waits
      // until everything is closed.
      server.close((err) => {
        if (err) {
          return reject(err)
        }

        resolve()
      })

      // If there are sockets remaining we might need to force close them or
      // this promise might never resolve.
      if (sockets.size > 0) {
        // Give sockets a chance to close up shop.
        cleanupTimeout = setTimeout(() => {
          cleanupTimeout = undefined

          for (const socket of sockets.values()) {
            console.warn("a socket was left hanging")
            socket.destroy()
          }
        }, 1000)
      }
    })
  }
}
A
Asher 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

/**
 * Get the options for setting a cookie.  The options must be identical for
 * setting and unsetting cookies otherwise they are considered separate.
 */
export const getCookieOptions = (req: express.Request): express.CookieOptions => {
  // Normally we set paths relatively.  However browsers do not appear to allow
  // cookies to be set relatively which means we need an absolute path.  We
  // cannot be guaranteed we know the path since a reverse proxy might have
  // rewritten it.  That means we need to get the path from the frontend.

  // The reason we need to set the path (as opposed to defaulting to /) is to
  // avoid code-server instances on different sub-paths clobbering each other or
  // from accessing each other's tokens (and to prevent other services from
  // accessing code-server's tokens).

  // When logging in or out the request must include the href (the full current
  // URL of that page) and the relative path to the root as given to it by the
  // backend.  Using these two we can determine the true absolute root.
  const url = new URL(
    req.query.base || req.body.base || "/",
    req.query.href || req.body.href || "http://" + (req.headers.host || "localhost"),
  )
  return {
    domain: getCookieDomain(url.host, req.args["proxy-domain"]),
    path: normalize(url.pathname) || "/",
    sameSite: "lax",
  }
}