Files
altcha/README.md
Daniel Regeci bddbad588f 0.8.0
2024-08-09 19:05:28 -03:00

10 KiB

ALTCHA

ALTCHA uses a proof-of-work mechanism to protect your website, APIs, and online services from spam and abuse. Unlike other solutions, ALTCHA is self-hosted, does not use cookies nor fingerprinting, does not track users, and is fully compliant with GDPR.

Visit ALTCHA for more information.

Features

  • Frictionless CAPTCHA Alternative - Employs proof-of-work (PoW) instead of visual puzzles.
  • Data Obfuscation - Safeguards your email address from scraping.
  • Cookie-less - Designed to be GDPR compliant by default.
  • Self-hosted - Operates independently without relying on external providers.
  • SaaS Available - Get started with the SaaS API at altcha.org/docs/api.

Examples

Server Integrations

Plugins

More anti-spam solutions

  • Spam Filter - stop sophisticated attacks and human-generated spam by classifying data.

Usage

ALTCHA widget is distributed as a "Web Component" and supports all modern browsers.

1. Install ALTCHA

npm install altcha

import altcha in your main file:

import 'altcha';

or insert <script> tag to your website:

<script async defer src="/altcha.js" type="module"></script>

CDN: https://cdn.jsdelivr.net/gh/altcha-org/altcha@main/dist/altcha.min.js

2. Use <altcha-widget> tag in your forms

<form>
  <altcha-widget
    challengeurl="https://..."
  ></altcha-widget>  
</form>

See the configuration below or visit the website integration documentation.

3. Integrate ALTCHA with your server

See server documentation for more details.

Content Security Policy (CSP)

The default distribution bundle of the WebComponent includes styles and the worker in a single file. This might cause issues with strict CSP rules. If you require strict CSP compliance, consider using the scripts located in the /dist_external directory. For more details, please refer to the documentation.

Configuration

Required options (at least one is required):

  • challengeurl - URL of your server to fetch the challenge from. Refer to server integration.
  • challengejson - JSON-encoded challenge data. If avoiding an HTTP request to challengeurl, provide the data here.

Additional options:

  • analytics - Enable analytics with ALTCHA Forms. See HTML submissions documentation.
  • auto - Automatically verify without user interaction (possible values: onfocus, onload, onsubmit).
  • beaconurl - URL to which analytics data will be sent using a beacon POST if the form is abandoned. This option is only used when analytics is enabled.
  • blockspam - Only used in conjunction with the spamfilter option. If enabled, it will block form submission and fail verification if the Spam Filter returns a negative classification. This effectively prevents submission of the form.
  • delay - The artificial delay in milliseconds to apply before the verification (defaults to 0).
  • expire - The challenge expiration (duration in milliseconds).
  • floating - Enable floating UI (possible values: auto, top, bottom).
  • floatinganchor - The CSS selector of the "anchor" to which the floating UI will be attached to (defaults to the button[type="submit"] in the related form).
  • floatingoffset - The Y offset from the anchor element for the floating UI expressed in pixels (defaults to 12).
  • hidefooter - Hide the footer (ALTCHA link).
  • hidelogo - Hide the ALTCHA logo.
  • maxnumber - The max. number to iterate to (defaults to 1,000,000).
  • name - The name of the hidden field containing the payload (defaults to "altcha").
  • obfuscated - The obfuscated data provided as a base64-encoded string. Use only without challengeurl/challengejson.
  • spamfilter - Enable Spam Filter.
  • strings - JSON-encoded translation strings. Refer to customization.
  • refetchonexpire - Automatically re-fetch and re-validate when the challenge expires (defaults to true).
  • verifyurl - URL for server-side verification requests. This option is automatically configured when the spamfilter option is used. Override this setting only if you are using a custom server implementation.
  • workers - The number of workers to utilize for PoW (defaults to navigator.hardwareConcurrency || 8, max. value 16).
  • workerurl - The URL of the Worker script (defaults to ./worker.js, only works with external build).

Development / testing options:

  • debug - Print log messages in the console.
  • mockerror - Causes the verification to always fail with a "mock" error.
  • test - Generates a "mock" challenge within the widget, bypassing the request to challengeurl.

Programmatic Configuration

To configure the widget programmatically, use the configure() method:

document.querySelector('#altcha').configure({
  challenge: {
    algorithm: 'SHA-256',
    challenge: '...',
    salt: '...',
    signature: '...',
  },
  strings: {
    label: 'Verify',
  },
});

Available configuration options:

export interface Configure {
  analytics?: boolean | string;
  auto?: 'onfocus' | 'onload' | 'onsubmit';
  beaconurl?: string;
  challenge?: {
    algorithm: string;
    challenge: string;
    maxnumber?: number;
    salt: string;
    signature: string;
  };
  challengeurl?: string;
  debug?: boolean;
  delay?: number;
  expire?: number;
  floating?: 'auto' | 'top' | 'bottom';
  floatinganchor?: string;
  floatingoffset?: number;
  autorenew?: boolean;
  hidefooter?: boolean;
  hidelogo?: boolean;
  maxnumber?: number;
  mockerror?: boolean;
  name?: string;
  obfuscated?: string;
  refetchonexpire?: boolean;
  spamfilter?: boolean | 'ipAddress' | SpamFilter;
  strings?: {
    error: string;
    expired: string;
    footer: string;
    label: string;
    verified: string;
    verifying: string;
    waitAlert: string;
  }
  test?: boolean | number | 'delay';
  verifyurl?: string;
  workers?: number;
  workerurl?: string;
}

Events

  • serververification - Triggers upon a server verification (only in conjunction with spamfilter).
  • statechange - Triggers whenever an internal state changes.
  • verified - Triggers when the challenge is verified.
enum State {
  ERROR = 'error',
  VERIFIED = 'verified',
  VERIFYING = 'verifying',
  UNVERIFIED = 'unverified',
  EXPIRED = 'expired',
};

Using events:

document.querySelector('#altcha').addEventListener('statechange', (ev) => {
  // See enum State above
  console.log('state:', ev.detail.state);
});

Important

Both programmatic configuration and event listeners have to called/attached after the ALTCHA script loads, such as within window.addEventListener('load', ...).

Spam Filter

The widget integrates with ALTCHA's Anti-Spam solution to allow checking submitted form data for potential spam.

The Spam Filter API analyzes various signals in the submitted data to determine if it exhibits characteristics of spam. This non-invasive filtering helps reduce spam submissions without frustrating legitimate users.

Spam Filter Configuration

The Spam Filter can be enabled with default configuration by setting the spamfilter option to true, or ipAddress to verify only the IP address and the time zone, or it can be customized using the following configuration schema:

interface SpamFilter {
  blockedCountries?: string[];
  classifier?: string;
  disableRules?: string[];
  email?: string | false;
  expectedCountries?: string[];
  expectedLanguages?: string[];
  fields?: string[] | false;
  ipAddress?: string | false;
  text?: string | string[];
  timeZone?: string | false;
}

SpamFilter configuration options:

  • blockedCountries - An array of country codes (ISO 3166 alpha-2) that you want to block.
  • classifier - Enforce a specific classifier.
  • disableRules - An array of rules to disable.
  • email - The name of the input field for the user's email. Disable email checking with false.
  • expectedCountries - An array of expected countries as 2-letter codes (ISO 3166-1 alpha-2).
  • expectedLanguages - An array of expected languages as 2-letter codes (ISO 639 alpha-2).
  • fields - An array of input names to send to the spam filter.
  • ipAddress - The user's IP is detected automatically but can be overridden or disabled with false.
  • text - The text to classify. An array of strings can also be submitted.
  • timeZone - The user's timezone is detected automatically but can be overridden or disabled with false.

To include the email field into fields (for easier server-side verification), configure the list of input names using the spamfilter.fields: string[] option.

Exclude Inputs from Spam Checking

By default, all text inputs and textareas within the parent form are spam-checked. To exclude a specific input, add the data-no-spamfilter attribute. Alternatively, explicitly list the checked fields using the fields config option.

Contributing

See Contributing Guide and please follow our Code of Conduct.

License

MIT