|
| 1 | +import { GraphQLRequestContext } from 'apollo-server-plugin-base'; |
| 2 | +import { GraphQLError } from 'graphql'; |
| 3 | +import { ResolverContextBase } from '../types/graphql'; |
| 4 | +import { truncateText } from './slowOperationAlert'; |
| 5 | + |
| 6 | +const MAX_ALERT_ERRORS = 10; |
| 7 | +const MAX_ALERT_ERRORS_LENGTH = 1200; |
| 8 | + |
| 9 | +const SENSITIVE_VARIABLE_KEYS = new Set([ |
| 10 | + 'password', |
| 11 | + 'token', |
| 12 | + 'accesstoken', |
| 13 | + 'refreshtoken', |
| 14 | + 'secret', |
| 15 | + 'authorization', |
| 16 | +]); |
| 17 | + |
| 18 | +const HIGHLIGHTED_VARIABLE_KEYS = new Set([ |
| 19 | + 'projectid', |
| 20 | + 'workspaceid', |
| 21 | + 'eventid', |
| 22 | + 'originaleventid', |
| 23 | + 'release', |
| 24 | + 'search', |
| 25 | + 'assignee', |
| 26 | + 'cursor', |
| 27 | +]); |
| 28 | + |
| 29 | +/** |
| 30 | + * Redact sensitive GraphQL variables before sending alerts. |
| 31 | + * |
| 32 | + * @param value - variable value |
| 33 | + * @param key - variable key |
| 34 | + * @returns sanitized value |
| 35 | + */ |
| 36 | +function sanitizeVariableValue(value: unknown, key: string): unknown { |
| 37 | + if (SENSITIVE_VARIABLE_KEYS.has(key.toLowerCase())) { |
| 38 | + return '[redacted]'; |
| 39 | + } |
| 40 | + |
| 41 | + if (Array.isArray(value)) { |
| 42 | + return value.map((item, index) => sanitizeVariableValue(item, `${key}[${index}]`)); |
| 43 | + } |
| 44 | + |
| 45 | + if (value && typeof value === 'object') { |
| 46 | + // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 47 | + return sanitizeVariables(value as Record<string, unknown>); |
| 48 | + } |
| 49 | + |
| 50 | + return value; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Redact sensitive GraphQL variables before sending alerts. |
| 55 | + * |
| 56 | + * @param variables - GraphQL request variables |
| 57 | + * @returns sanitized variables |
| 58 | + */ |
| 59 | +function sanitizeVariables( |
| 60 | + variables: Record<string, unknown> | null | undefined |
| 61 | +): Record<string, unknown> { |
| 62 | + /** |
| 63 | + * Null / non-object values are treated as empty — many clients send |
| 64 | + * `variables: null` for operations without variables, and arrays are not a |
| 65 | + * valid GraphQL variables map. |
| 66 | + */ |
| 67 | + if (variables == null || typeof variables !== 'object' || Array.isArray(variables)) { |
| 68 | + return {}; |
| 69 | + } |
| 70 | + |
| 71 | + return Object.fromEntries( |
| 72 | + Object.entries(variables).map(([key, value]) => [key, sanitizeVariableValue(value, key)]) |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Extract useful identifiers from nested GraphQL variables. |
| 78 | + * |
| 79 | + * @param value - variable value |
| 80 | + * @param prefix - nested path prefix |
| 81 | + * @param result - accumulator for extracted ids |
| 82 | + * @returns extracted identifiers |
| 83 | + */ |
| 84 | +function collectHighlightedIds( |
| 85 | + value: unknown, |
| 86 | + prefix = '', |
| 87 | + result: Record<string, string | number | boolean> = {} |
| 88 | +): Record<string, string | number | boolean> { |
| 89 | + if (!value || typeof value !== 'object') { |
| 90 | + return result; |
| 91 | + } |
| 92 | + |
| 93 | + for (const [key, nestedValue] of Object.entries(value as Record<string, unknown>)) { |
| 94 | + const path = prefix ? `${prefix}.${key}` : key; |
| 95 | + |
| 96 | + if ( |
| 97 | + HIGHLIGHTED_VARIABLE_KEYS.has(key.toLowerCase()) && |
| 98 | + (typeof nestedValue === 'string' || typeof nestedValue === 'number' || typeof nestedValue === 'boolean') |
| 99 | + ) { |
| 100 | + result[path] = nestedValue; |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + if (nestedValue && typeof nestedValue === 'object' && !Array.isArray(nestedValue)) { |
| 105 | + collectHighlightedIds(nestedValue, path, result); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return result; |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Flatten GraphQL errors into a capped string for Hawk alert context. |
| 114 | + * sanitizeContext() only truncates top-level strings, not values nested in arrays. |
| 115 | + * Reserves space for an omitted-count suffix and truncateText()'s ellipsis. |
| 116 | + * |
| 117 | + * @param errors - GraphQL errors from the request |
| 118 | + * @returns flattened and truncated error messages |
| 119 | + */ |
| 120 | +export function formatGraphqlErrorsForAlert(errors: readonly GraphQLError[]): string { |
| 121 | + const messages = errors.slice(0, MAX_ALERT_ERRORS).map((error) => error.message); |
| 122 | + const omittedCount = errors.length - messages.length; |
| 123 | + const omittedSuffix = omittedCount > 0 ? `; …(+${omittedCount} more)` : ''; |
| 124 | + const maxMessagesLength = Math.max(0, MAX_ALERT_ERRORS_LENGTH - omittedSuffix.length - 1); |
| 125 | + |
| 126 | + return `${truncateText(messages.join('; '), maxMessagesLength)}${omittedSuffix}`; |
| 127 | +} |
| 128 | + |
| 129 | +/** |
| 130 | + * Build request context for slow GraphQL operation alerts. |
| 131 | + * |
| 132 | + * @param ctx - GraphQL request context |
| 133 | + * @returns alert context |
| 134 | + */ |
| 135 | +export function buildGraphqlRequestContext(ctx: GraphQLRequestContext): Record<string, unknown> { |
| 136 | + const context = ctx.context as ResolverContextBase | undefined; |
| 137 | + const variables = sanitizeVariables( |
| 138 | + ctx.request.variables as Record<string, unknown> | null | undefined |
| 139 | + ); |
| 140 | + const highlightedIds = collectHighlightedIds(variables); |
| 141 | + const alertContext: Record<string, unknown> = {}; |
| 142 | + |
| 143 | + if (context?.user?.id) { |
| 144 | + alertContext.userId = context.user.id; |
| 145 | + } |
| 146 | + |
| 147 | + if (Object.keys(highlightedIds).length > 0) { |
| 148 | + alertContext.ids = highlightedIds; |
| 149 | + } |
| 150 | + |
| 151 | + const variablesJson = JSON.stringify(variables); |
| 152 | + |
| 153 | + if (variablesJson && variablesJson !== '{}') { |
| 154 | + alertContext.variables = truncateText(variablesJson, 1200); |
| 155 | + } |
| 156 | + |
| 157 | + return alertContext; |
| 158 | +} |
0 commit comments