convertRelayPiiConfig.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 | null): Rule[] {
  9. const piiConfig = relayPiiConfig ? JSON.parse(relayPiiConfig) : {};
  10. const rules: Record<string, 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;
  44. if (method === MethodType.REPLACE) {
  45. if (type === RuleType.PATTERN) {
  46. convertedRules.push({
  47. id,
  48. method: MethodType.REPLACE,
  49. type: RuleType.PATTERN,
  50. source,
  51. placeholder: redaction?.text,
  52. pattern: resolvedRule.pattern,
  53. });
  54. } else {
  55. convertedRules.push({
  56. id,
  57. method: MethodType.REPLACE,
  58. type,
  59. source,
  60. placeholder: redaction?.text,
  61. });
  62. }
  63. } else if (type === RuleType.PATTERN) {
  64. convertedRules.push({
  65. id,
  66. method,
  67. type: RuleType.PATTERN,
  68. source,
  69. pattern: resolvedRule.pattern,
  70. });
  71. } else {
  72. convertedRules.push({id, method, type, source});
  73. }
  74. }
  75. }
  76. return convertedRules;
  77. }