convertRelayPiiConfig.tsx 2.6 KB

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