Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

1.7 KB
Raw Blame
const REDACTED = '[redacted]'

const SECRET_KEY_VALUE =
  /\b((?:x-)?auth[_-]?token|access[_-]?token|refresh[_-]?token|id[_-]?token|auth[_-]?token|token|password|passwd|secret|client[_-]?secret|api[_-]?key|apikey|session[_-]?id)\b(["']?\s*[:=]\s*)(["']?)([^\s"',;}\]&]+)\3/gi

const ENV_SECRET =
  /\b([A-Z][A-Z0-9_]*(?:TOKEN|SECRET|PASSWORD|PASSWD|API_KEY|CLIENT_SECRET|AUTHORIZATION))=([^\s]+)/g

const QUERY_SECRET =
  /([?&](?:token|access_token|refresh_token|id_token|code|key|secret|password)=)[^&\s]+/gi

/**
 * Remove tokens and credentials before an error message, log or report is
 * rendered or copied. Keep the surrounding text so the report stays useful.
 */
export function scrubSensitive(input: string): string {
  if (!input) return input

  return (
    input
      // Epic OAuth tokens are prefixed with eg1~
      .replace(/\beg1~[A-Za-z0-9._~+/=-]+/g, `eg1~${REDACTED}`)
      // JWT: header.payload.signature
      .replace(
        /\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b/g,
        REDACTED
      )
      // Authorization headers, with or without a scheme
      .replace(/(authorization\s*[:=]\s*)[^\n]+/gi, `$1${REDACTED}`)
      .replace(/\b(bearer\s+)[A-Za-z0-9._~+/=-]+/gi, `$1${REDACTED}`)
      // Cookie and Set-Cookie headers can carry session credentials
      .replace(/((?:set-)?cookie\s*:\s*)[^\n]+/gi, `$1${REDACTED}`)
      // key=value and key: value secrets, including GOG and Epic tokens
      .replace(
        SECRET_KEY_VALUE,
        (_match, key, separator, quote) =>
          `${key}${separator}${quote}${REDACTED}${quote}`
      )
      // Tokens passed in query strings
      .replace(QUERY_SECRET, `$1${REDACTED}`)
      // Env-style assignments
      .replace(ENV_SECRET, `$1=${REDACTED}`)
  )
}