|
| 1 | +import type { AxiosError, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios' |
| 2 | +import * as enc from '../../encryption' |
| 3 | +import { PUBLIC_KEY_VERSION } from '../../constants' |
| 4 | +import crypto, { KeyObject } from 'crypto' |
| 5 | +import { DecryptionError } from '../cloud_request_errors' |
| 6 | +import axios from 'axios' |
| 7 | + |
| 8 | +let encryptionKey: KeyObject |
| 9 | + |
| 10 | +declare module 'axios' { |
| 11 | + interface AxiosRequestConfig { |
| 12 | + encrypt?: 'always' | 'signed' | boolean |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +const encryptRequest = async (req: InternalAxiosRequestConfig) => { |
| 17 | + if (!req.data) { |
| 18 | + throw new Error(`Cannot issue encrypted request to ${req.url} without request body`) |
| 19 | + } |
| 20 | + |
| 21 | + encryptionKey ??= crypto.createSecretKey(Uint8Array.from(crypto.randomBytes(32))) |
| 22 | + |
| 23 | + const { jwe } = await enc.encryptRequest({ body: req.data }, { secretKey: encryptionKey }) |
| 24 | + |
| 25 | + req.headers.set('x-cypress-encrypted', PUBLIC_KEY_VERSION) |
| 26 | + req.data = jwe |
| 27 | + |
| 28 | + return req |
| 29 | +} |
| 30 | + |
| 31 | +const signRequest = (req: InternalAxiosRequestConfig) => { |
| 32 | + req.headers.set('x-cypress-signature', PUBLIC_KEY_VERSION) |
| 33 | + |
| 34 | + return req |
| 35 | +} |
| 36 | + |
| 37 | +const maybeDecryptResponse = async (res: AxiosResponse) => { |
| 38 | + if (!res.config.encrypt) { |
| 39 | + return res |
| 40 | + } |
| 41 | + |
| 42 | + if (res.config.encrypt === 'always' || res.headers['x-cypress-encrypted']) { |
| 43 | + try { |
| 44 | + res.data = await enc.decryptResponse(res.data, encryptionKey) |
| 45 | + } catch (e) { |
| 46 | + throw new DecryptionError(e.message) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return res |
| 51 | +} |
| 52 | + |
| 53 | +const maybeDecryptErrorResponse = async (err: AxiosError<any> | Error & { error?: any, statusCode: number, isApiError?: boolean }) => { |
| 54 | + if (axios.isAxiosError(err) && err.response?.data) { |
| 55 | + if (err.config?.encrypt === 'always' || err.response?.headers['x-cypress-encrypted']) { |
| 56 | + try { |
| 57 | + if (err.response.data) { |
| 58 | + err.response.data = await enc.decryptResponse(err.response.data, encryptionKey) |
| 59 | + } |
| 60 | + } catch (e) { |
| 61 | + if (err.status && err.status >= 500 || err.status === 404) { |
| 62 | + throw err |
| 63 | + } |
| 64 | + |
| 65 | + throw new DecryptionError(e.message) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + throw err |
| 71 | +} |
| 72 | + |
| 73 | +const maybeVerifyResponseSignature = (res: AxiosResponse) => { |
| 74 | + if (res.config.encrypt === 'signed' && !res.headers['x-cypress-signature']) { |
| 75 | + throw new Error(`Expected signed response for ${res.config.url }`) |
| 76 | + } |
| 77 | + |
| 78 | + if (res.headers['x-cypress-signature']) { |
| 79 | + const dataString = typeof res.data === 'string' ? res.data : JSON.stringify(res.data) |
| 80 | + const verified = enc.verifySignature(dataString, res.headers['x-cypress-signature']) |
| 81 | + |
| 82 | + if (!verified) { |
| 83 | + throw new Error(`Unable to verify response signature for ${res.config.url}`) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return res |
| 88 | +} |
| 89 | + |
| 90 | +// Always = req & res MUST be encrypted |
| 91 | +// true = req MUST be encrypted, res MAY be encrypted, signified by header |
| 92 | +// signed = verify signature of the response body |
| 93 | +export const installEncryption = (axios: AxiosInstance) => { |
| 94 | + axios.interceptors.request.use(encryptRequest, undefined, { |
| 95 | + runWhen (config) { |
| 96 | + return config.encrypt === true || config.encrypt === 'always' |
| 97 | + }, |
| 98 | + }) |
| 99 | + |
| 100 | + axios.interceptors.request.use(signRequest, undefined, { |
| 101 | + runWhen (config) { |
| 102 | + return config.encrypt === 'signed' |
| 103 | + }, |
| 104 | + }) |
| 105 | + |
| 106 | + axios.interceptors.response.use(maybeDecryptResponse, maybeDecryptErrorResponse) |
| 107 | + axios.interceptors.response.use(maybeVerifyResponseSignature) |
| 108 | +} |
0 commit comments