Skip to content

Protect email addresses, phone numbers and other sensitive data from bots scraping the source code of your Astro app.

License

Notifications You must be signed in to change notification settings

andreas-brunner/astro-mail-obfuscation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Astro Mail Obfuscation

CodeFactor

Supported HTML tags: h1-h6, p, span, a, label, ul, ol, li, strong, b, em, i.

This Astro integration effectively prevents spam bots from extracting email addresses, phone numbers or other sensitive data from the source code of your website. You can manually select one of three methods or activate random mode to increase variation and make it harder for bots to extract data. The integration has been designed with a focus on stability, reliability, performance and seamless support for <ViewTransitions />.

Important: Server-side rendering is currently not supported.

Installation

Install (recommended via Astro CLI):

npx astro add astro-mail-obfuscation

Usage

Add the data-obfuscation attribute to any HTML tag containing sensitive content to obfuscate it during the build process. You can use the values "1", "2" or "3" to manually select the method for the respective tag. Alternatively, you can use "0" to specify that the integration automatically selects one of the three methods at random during the build, which increases variability and makes interpretation by simple bots even more difficult.

Examples:

<!-- Manually select obfuscation method: -->
<a href="mailto:info@..." data-obfuscation="1">info@...</a>
<a href="tel:0123..." data-obfuscation="2">0123...</a>
<p>...<span data-obfuscation="3">info@...</span>...</p>
<!-- Automatically select a random obfuscation method: -->
<ul data-obfuscation="0">
  <li>John Doe</li>
  <li>Jane Doe</li>
</ul>
<label data-obfuscation="0">info@...</label>
<!-- ... -->

Information: HTML tags that do not contain the data-obfuscation attribute are not obfuscated. This can be useful in certain situations (e.g. GDPR).

Methods

The integration provides three XOR-based methods for obfuscation:

  • Method "1": XOR with userKey, resulting in hexadecimal encoding.
  • Method "2": XOR with userKey, resulting in Base64 encoding (dynamically reversed).
  • Method "3": XOR with userKey and userSalt, resulting in Base62 encoding.

Each method uses XOR-based encoding with additional keys to vary how the content is represented, making it harder for simpler bots to detect patterns. For increased variability, data-obfuscation="0" enables automatic mode, which randomly selects a method during the build to further complicate bot interpretation.

Configuration

This integration works out of the box with no required setup.

Nevertheless, a few settings can be made to control the behavior of the integration:

// astro.config.mjs
import mailObfuscation from "astro-mail-obfuscation";
export default defineConfig({
  integrations: [
    mailObfuscation({
      fallbackText: "Please enable JavaScript!", // Default: "[PROTECTED!]"
      // userKey: "...", // Automatically generated
      // userSalt: "...", // Automatically generated (must be 8 characters if set manually)
      // concurrencyLimit: number, // Max concurrent tasks, default: 5 (p-limit)
      // allowedTags: ["button", "..."] // Add unsupported HTML tags to the whitelist
    }),
  ],
});

The fallbackText is displayed if the user has deactivated JavaScript in the browser. Whenever you run the build process, userKey and userSalt are automatically regenerated by default. If you still want to set static values within your Astro configuration, please note that the userSalt must be exactly 8 characters long. The length of the userKey, on the other hand, is not limited. The userKey and userSalt are set as a meta tag on all pages during the build process to make them accessible to the client-side JavaScript. Please change the concurrencyLimit with caution, as a value that is too high could affect stability. By default, 17 HTML tags are supported, but if you still need support for more tags, add them to the allowedTags array.

Recommendation: In most cases, only the fallbackText needs to be adjusted. The default settings are already optimized for stable use.

Limitations

While the majority of basic bots cannot execute JavaScript, this integration provides an effective and practical level of protection against simpler, more common bots. Astro Mail Obfuscation is a client-side solution that is particularly ideal for use on static sites.