Skip to content

Conversation

@teppokoivula
Copy link
Contributor

@Roope, sorry! I didn't test previous PR well enough. Found an issue with nested emails.

Things looked fine at first glance, but actually...

<a href="https://example.com/example@example.com">https://example.com/example@example.com</a>

... resulted in something like this:

<a href="https://example.com/example@example.com">https://example.com/<noscript data-emo=\"PGEgaHJlZj0ibWFpbHRvOnVzZXJAZG9tYWluLmNvbSI_dXNlckBkb21haW4uY29tPC9hPgAA\">Enable JavaScript to view protected
  content.</noscript></a>

This should now be fixed in this PR.

Also, since this is actually a tiny bit complicated, I thought it might make sense to bundle in some basic tests. testObfuscation($html) can be used for one-off tests, and testObfuscationWithCases() runs some predefined test cases. Feel free to discard this part if you don't like the idea, but it helped me a lot while testing different situations :)

Test results are an array like this:

  {
      "plain email": {
          "pass": true,
          "input": "Contact us at test@example.com for help.",
          "output": "Contact us at <noscript data-emo=\"PGEgaHJlZj0ibWFpbHRvOnRlc3RAZXhhbXBsZS5jb20iPnRlc3RAZXhhbXBsZS5jb208L2E_\">Enable JavaScript to view protected content.</noscript> for help.",
          "expected": "Contact us at <noscript data-emo=\"PGEgaHJlZj0ibWFpbHRvOnRlc3RAZXhhbXBsZS5jb20iPnRlc3RAZXhhbXBsZS5jb208L2E_\">Enable JavaScript to view protected content.</noscript> for help."
      },
      "mailto link": {
          "pass": true,
          "input": "<a href=\"mailto:test@example.com\">test@example.com</a>",
          "output": "<noscript data-emo=\"PGEgaHJlZj0ibWFpbHRvOnRlc3RAZXhhbXBsZS5jb20iPnRlc3RAZXhhbXBsZS5jb208L2E_\">Enable JavaScript to view protected content.</noscript>",
          "expected": "<noscript data-emo=\"PGEgaHJlZj0ibWFpbHRvOnRlc3RAZXhhbXBsZS5jb20iPnRlc3RAZXhhbXBsZS5jb208L2E_\">Enable JavaScript to view protected content.</noscript>"
      },
      "url with email-like path": {
          "pass": true,
          "input": "<a href=\"https://example.com/user@domain.com\">https://example.com/user@domain.com</a>",
          "output": "<a href=\"https://example.com/user@domain.com\">https://example.com/user@domain.com</a>",
          "expected": "<a href=\"https://example.com/user@domain.com\">https://example.com/user@domain.com</a>"
      },
      "email query parameter": {
          "pass": true,
          "input": "<a href=\"https://example.com/?email=user@example.com\">https://example.com/?email=user@example.com</a>",
          "output": "<a href=\"https://example.com/?email=user@example.com\">https://example.com/?email=user@example.com</a>",
          "expected": "<a href=\"https://example.com/?email=user@example.com\">https://example.com/?email=user@example.com</a>"
      },
      "multiple emails": {
          "pass": true,
          "input": "Contact alice@example.com or bob@example.com for help.",
          "output": "Contact <noscript data-emo=\"PGEgaHJlZj0ibWFpbHRvOmFsaWNlQGV4YW1wbGUuY29tIj5hbGljZUBleGFtcGxlLmNvbTwvYT4A\">Enable JavaScript to view protected content.</noscript> or <noscript
  data-emo=\"PGEgaHJlZj0ibWFpbHRvOmJvYkBleGFtcGxlLmNvbSI_Ym9iQGV4YW1wbGUuY29tPC9hPgAA\">Enable JavaScript to view protected content.</noscript> for help.",
          "expected": "Contact <noscript data-emo=\"PGEgaHJlZj0ibWFpbHRvOmFsaWNlQGV4YW1wbGUuY29tIj5hbGljZUBleGFtcGxlLmNvbTwvYT4A\">Enable JavaScript to view protected content.</noscript> or <noscript
  data-emo=\"PGEgaHJlZj0ibWFpbHRvOmJvYkBleGFtcGxlLmNvbSI_Ym9iQGV4YW1wbGUuY29tPC9hPgAA\">Enable JavaScript to view protected content.</noscript> for help."
      }
  }

And without this fix, two of the tests would fail:

      "url with email-like path": {
          "pass": false,
          "input": "<a href=\"https://example.com/user@domain.com\">https://example.com/user@domain.com</a>",
          "output": "<a href=\"https://example.com/user@domain.com\">https://example.com/<noscript data-emo=\"PGEgaHJlZj0ibWFpbHRvOnVzZXJAZG9tYWluLmNvbSI_dXNlckBkb21haW4uY29tPC9hPgAA\">Enable JavaScript to view protected
  content.</noscript></a>",
          "expected": "<a href=\"https://example.com/user@domain.com\">https://example.com/user@domain.com</a>"
      },
      "email query parameter": {
          "pass": false,
          "input": "<a href=\"https://example.com/?email=user@example.com\">https://example.com/?email=user@example.com</a>",
          "output": "<a href=\"https://example.com/?email=user@example.com\">https://example.com/<noscript data-emo=\"PGEgaHJlZj0ibWFpbHRvOj9lbWFpbD11c2VyQGV4YW1wbGUuY29tIj4.ZW1haWw9dXNlckBleGFtcGxlLmNvbTwvYT4A\">Enable JavaScript to view
   protected content.</noscript></a>",
          "expected": "<a href=\"https://example.com/?email=user@example.com\">https://example.com/?email=user@example.com</a>"
      },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant