test_datascrubbing.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import copy
  2. import pytest
  3. from sentry.datascrubbing import scrub_data
  4. from sentry.testutils.pytest.fixtures import django_db_all
  5. def merge_pii_configs(prefixes_and_configs):
  6. from sentry.datascrubbing import _merge_pii_configs as f
  7. prefixes_and_configs_bak = copy.deepcopy(prefixes_and_configs)
  8. rv = f(prefixes_and_configs)
  9. # No accidental mutation
  10. assert prefixes_and_configs == prefixes_and_configs_bak
  11. return rv
  12. @django_db_all
  13. @pytest.mark.parametrize("field", ["ooo", "oöö", "o o", "o\no", "o'o"])
  14. def test_scrub_data(field, default_project):
  15. project = default_project
  16. organization = project.organization
  17. organization.update_option(
  18. "sentry:relay_pii_config",
  19. """
  20. {
  21. "applications": {
  22. "debug_meta.images.*.code_file": ["@userpath:replace"],
  23. "debug_meta.images.*.debug_file": ["@userpath:replace"]
  24. }
  25. }
  26. """,
  27. )
  28. organization.update_option("sentry:safe_fields", [])
  29. organization.update_option("sentry:sensitive_fields", ["o"])
  30. organization.update_option("sentry:scrub_ip_address", False)
  31. organization.update_option("sentry:require_scrub_data", True)
  32. event = {
  33. "extra": {field: "pls remove"},
  34. "debug_meta": {
  35. "images": [
  36. {"type": "symbolic", "debug_file": "/Users/foo/bar", "code_file": "/Users/foo/bar"}
  37. ]
  38. },
  39. }
  40. new_event = scrub_data(project, event)
  41. assert new_event == (
  42. {
  43. "_meta": {
  44. "debug_meta": {
  45. "images": {
  46. "0": {
  47. "code_file": {
  48. "": {"len": 10, "rem": [["@userpath:replace", "s", 7, 13]]}
  49. },
  50. "debug_file": {
  51. "": {"len": 10, "rem": [["@userpath:replace", "s", 7, 13]]}
  52. },
  53. }
  54. }
  55. },
  56. "extra": {field: {"": {"len": 10, "rem": [["strip-fields", "s", 0, 10]]}}},
  57. },
  58. "debug_meta": {
  59. "images": [
  60. {
  61. "code_file": "/Users/[user]/bar",
  62. "debug_file": "/Users/[user]/bar",
  63. "type": "symbolic",
  64. }
  65. ]
  66. },
  67. "extra": {field: "[Filtered]"},
  68. }
  69. )
  70. def test_merge_pii_configs_simple():
  71. assert merge_pii_configs([("p:", {}), ("o:", {})]) == {}
  72. assert merge_pii_configs(
  73. [("p:", {"applications": {"$string": ["@ip:remove"]}}), ("o:", {})]
  74. ) == {"applications": {"$string": ["@ip:remove"]}}
  75. def test_merge_pii_configs_rule_references():
  76. my_rules = {
  77. "remove_ips_alias": {
  78. "type": "alias",
  79. "rule": "@ip",
  80. "hide_rule": False,
  81. "redaction": {"method": "remove"},
  82. },
  83. "remove_ips_and_macs": {
  84. "type": "multiple",
  85. "rules": ["remove_ips_alias", "@mac"],
  86. "hide_rule": False,
  87. "redaction": {"method": "remove"},
  88. },
  89. }
  90. assert merge_pii_configs(
  91. [
  92. ("o:", {"rules": my_rules, "applications": {"$string": ["remove_ips_and_macs"]}}),
  93. ("p:", {"rules": my_rules, "applications": {"$string": ["remove_ips_alias"]}}),
  94. ]
  95. ) == {
  96. "applications": {"$string": ["o:remove_ips_and_macs", "p:remove_ips_alias"]},
  97. "rules": {
  98. "o:remove_ips_and_macs": {
  99. "hide_rule": False,
  100. "redaction": {"method": "remove"},
  101. "rules": ["o:remove_ips_alias", "@mac"],
  102. "type": "multiple",
  103. },
  104. "o:remove_ips_alias": {
  105. "hide_rule": False,
  106. "redaction": {"method": "remove"},
  107. "rule": "@ip",
  108. "type": "alias",
  109. },
  110. "p:remove_ips_and_macs": {
  111. "hide_rule": False,
  112. "redaction": {"method": "remove"},
  113. "rules": ["p:remove_ips_alias", "@mac"],
  114. "type": "multiple",
  115. },
  116. "p:remove_ips_alias": {
  117. "hide_rule": False,
  118. "redaction": {"method": "remove"},
  119. "rule": "@ip",
  120. "type": "alias",
  121. },
  122. },
  123. }