convertRelayPiiConfig.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import type {Applications, PiiConfig, Rule, RuleDefault} from './types';
  2. import {MethodType, RuleType} from './types';
  3. // Remap PII config format to something that is more usable in React. Ideally
  4. // we would stop doing this at some point and make some updates to how we
  5. // store this configuration on the server.
  6. //
  7. // For the time being the PII config format is documented at
  8. // https://getsentry.github.io/relay/pii-config/
  9. export function convertRelayPiiConfig(relayPiiConfig?: string | null): Rule[] {
  10. const piiConfig = relayPiiConfig ? JSON.parse(relayPiiConfig) : {};
  11. const rules: Record<string, PiiConfig> = piiConfig.rules || {};
  12. const applications: Applications = piiConfig.applications || {};
  13. const convertedRules: Array<Rule> = [];
  14. for (const application in applications) {
  15. for (const rule of applications[application]) {
  16. const resolvedRule = rules[rule];
  17. const id = convertedRules.length;
  18. const source = application;
  19. if (!resolvedRule) {
  20. // Convert a "built-in" rule like "@anything:remove" to an object {
  21. // type: "anything",
  22. // method: "remove"
  23. // }
  24. if (rule[0] === '@') {
  25. const typeAndMethod = rule.slice(1).split(':');
  26. let [type] = typeAndMethod;
  27. const [, method] = typeAndMethod;
  28. if (type === 'urlauth') {
  29. type = 'url_auth';
  30. }
  31. if (type === 'usssn') {
  32. type = 'us_ssn';
  33. }
  34. convertedRules.push({
  35. id,
  36. method: method as RuleDefault['method'],
  37. type: type as RuleDefault['type'],
  38. source,
  39. });
  40. }
  41. continue;
  42. }
  43. const {type, redaction} = resolvedRule;
  44. const method = redaction.method;
  45. if (method === MethodType.REPLACE) {
  46. if (type === RuleType.PATTERN) {
  47. convertedRules.push({
  48. id,
  49. method: MethodType.REPLACE,
  50. type: RuleType.PATTERN,
  51. source,
  52. placeholder: redaction?.text,
  53. pattern: resolvedRule.pattern,
  54. });
  55. } else {
  56. convertedRules.push({
  57. id,
  58. method: MethodType.REPLACE,
  59. type,
  60. source,
  61. placeholder: redaction?.text,
  62. });
  63. }
  64. } else if (type === RuleType.PATTERN) {
  65. convertedRules.push({
  66. id,
  67. method,
  68. type: RuleType.PATTERN,
  69. source,
  70. pattern: resolvedRule.pattern,
  71. });
  72. } else {
  73. convertedRules.push({id, method, type, source});
  74. }
  75. }
  76. }
  77. return convertedRules;
  78. }