utils.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import {t} from 'sentry/locale';
  2. import type {Rule, SourceSuggestion} from './types';
  3. import {MethodType, RuleType, SourceSuggestionType} from './types';
  4. function getRuleLabel(type: RuleType) {
  5. switch (type) {
  6. case RuleType.ANYTHING:
  7. return t('Anything');
  8. case RuleType.IMEI:
  9. return t('IMEI numbers');
  10. case RuleType.MAC:
  11. return t('MAC addresses');
  12. case RuleType.EMAIL:
  13. return t('Email addresses');
  14. case RuleType.PEMKEY:
  15. return t('PEM keys');
  16. case RuleType.URLAUTH:
  17. return t('Auth in URLs');
  18. case RuleType.USSSN:
  19. return t('US social security numbers');
  20. case RuleType.USER_PATH:
  21. return t('Usernames in filepaths');
  22. case RuleType.UUID:
  23. return t('UUIDs');
  24. case RuleType.CREDITCARD:
  25. return t('Credit card numbers');
  26. case RuleType.PASSWORD:
  27. return t('Password fields');
  28. case RuleType.IP:
  29. return t('IP addresses');
  30. case RuleType.PATTERN:
  31. return t('Regex matches');
  32. default:
  33. return '';
  34. }
  35. }
  36. function getMethodLabel(type: MethodType) {
  37. switch (type) {
  38. case MethodType.MASK:
  39. return {
  40. label: t('Mask'),
  41. description: t('Replace with ****'),
  42. };
  43. case MethodType.HASH:
  44. return {
  45. label: t('Hash'),
  46. description: t('Replace with DEADBEEF'),
  47. };
  48. case MethodType.REMOVE:
  49. return {
  50. label: t('Remove'),
  51. description: t('Replace with null'),
  52. };
  53. case MethodType.REPLACE:
  54. return {
  55. label: t('Replace'),
  56. description: t('Replace with Placeholder'),
  57. };
  58. default:
  59. return {
  60. label: '',
  61. };
  62. }
  63. }
  64. const binarySuggestions: Array<SourceSuggestion> = [
  65. {
  66. type: SourceSuggestionType.BINARY,
  67. value: '&&',
  68. },
  69. {
  70. type: SourceSuggestionType.BINARY,
  71. value: '||',
  72. },
  73. ];
  74. const unarySuggestions: Array<SourceSuggestion> = [
  75. {
  76. type: SourceSuggestionType.UNARY,
  77. value: '!',
  78. },
  79. ];
  80. const valueSuggestions: Array<SourceSuggestion> = [
  81. {type: SourceSuggestionType.VALUE, value: '**', description: t('everywhere')},
  82. {
  83. type: SourceSuggestionType.VALUE,
  84. value: 'password',
  85. description: t('attributes named "password"'),
  86. },
  87. {
  88. type: SourceSuggestionType.VALUE,
  89. value: '$error.value',
  90. description: t('the exception value'),
  91. },
  92. {
  93. type: SourceSuggestionType.VALUE,
  94. value: '$message',
  95. description: t('the log message'),
  96. },
  97. {
  98. type: SourceSuggestionType.VALUE,
  99. value: 'extra.MyValue',
  100. description: t('the key "MyValue" in "Additional Data"'),
  101. },
  102. {
  103. type: SourceSuggestionType.VALUE,
  104. value: 'extra.**',
  105. description: t('everything in "Additional Data"'),
  106. },
  107. {
  108. type: SourceSuggestionType.VALUE,
  109. value: '$http.headers.x-custom-token',
  110. description: t('the X-Custom-Token HTTP header'),
  111. },
  112. {
  113. type: SourceSuggestionType.VALUE,
  114. value: '$user.ip_address',
  115. description: t('the user IP address'),
  116. },
  117. {
  118. type: SourceSuggestionType.VALUE,
  119. value: '$frame.vars.foo',
  120. description: t('the local variable "foo"'),
  121. },
  122. {
  123. type: SourceSuggestionType.VALUE,
  124. value: 'contexts.device.timezone',
  125. description: t('the timezone in the device context'),
  126. },
  127. {
  128. type: SourceSuggestionType.VALUE,
  129. value: 'tags.server_name',
  130. description: t('the tag "server_name"'),
  131. },
  132. {
  133. type: SourceSuggestionType.VALUE,
  134. value: '$attachments.**',
  135. description: t('all attachments'),
  136. },
  137. {
  138. type: SourceSuggestionType.VALUE,
  139. value: "$attachments.'logfile.txt'",
  140. description: t('all attachments named "logfile.txt"'),
  141. },
  142. {
  143. type: SourceSuggestionType.VALUE,
  144. value: '$minidump',
  145. description: t('the entire minidump of a native crash report'),
  146. },
  147. {
  148. type: SourceSuggestionType.VALUE,
  149. value: '$minidump.heap_memory',
  150. description: t('the heap memory region in a native crash report'),
  151. },
  152. {
  153. type: SourceSuggestionType.VALUE,
  154. value: 'code_file',
  155. description: t('the pathname of a code module in a native crash report'),
  156. },
  157. {
  158. type: SourceSuggestionType.VALUE,
  159. value: 'debug_file',
  160. description: t('the pathname of a debug module in a native crash report'),
  161. },
  162. ];
  163. export {
  164. binarySuggestions,
  165. getMethodLabel,
  166. getRuleLabel,
  167. unarySuggestions,
  168. valueSuggestions,
  169. };
  170. export function getRuleDescription(rule: Rule) {
  171. const {method, type, source} = rule;
  172. const methodLabel = getMethodLabel(method);
  173. const typeLabel = getRuleLabel(type);
  174. const descriptionDetails: Array<string> = [];
  175. descriptionDetails.push(`[${methodLabel.label}]`);
  176. descriptionDetails.push(
  177. rule.type === RuleType.PATTERN ? `[${rule.pattern}]` : `[${typeLabel}]`
  178. );
  179. if (rule.method === MethodType.REPLACE && rule.placeholder) {
  180. descriptionDetails.push(`with [${rule.placeholder}]`);
  181. }
  182. return `${descriptionDetails.join(' ')} ${t('from')} [${source}]`;
  183. }