complete api documentation for app hardening library
main library class providing app hardening functionality.
new ghard13(config = {})
parameters:
config (object, optional): configuration optionsdefault configuration:
{
build_time: true,
puzzle_enabled: true,
session_timeout: 3600000, // 1 hour
fallback_limits: {
timeout_limit: 2,
invalid_solution_limit: 3,
max_total_attempts: 5
},
obfuscator: {
hex_length: 8,
site_salt: 'ghard13_default',
pool_size: 100,
use_non_hex_chars: true
}
}
example:
const { ghard13 } = require('ghard13');
const hardener = new ghard13({
obfuscator: {
hex_length: 12,
site_salt: 'my_unique_salt'
}
});
hardens site content by obfuscating selectors.
parameters:
html_content (string): html content to hardencss_content (string): css content to hardenjs_content (string): javascript content to hardenreturns: object with hardened content and mappings
{
html: string, // hardened html
css: string, // hardened css
js: string, // hardened javascript
mappings: object // selector mappings
}
example:
const html = '<div id="main" class="container">content</div>';
const css = '.container { color: blue; }';
const js = 'document.getElementById("main")';
const result = hardener.harden_content(html, css, js);
console.log(result.mappings); // { main: 'a1b2c3d4', container: 'e5f6g7h8' }
generates behavioral puzzle for site entry.
parameters: none
returns: puzzle object
{
id: string, // unique puzzle identifier
type: string, // puzzle type ('behavioral')
instructions: string, // human-readable instructions
timeout: number, // timeout in milliseconds
components: {
wait_time: number, // required wait time (ms)
slider_target: number, // slider target percentage
input_word: string // required input word
}
}
example:
const puzzle = hardener.generate_puzzle();
console.log(puzzle.instructions); // 'wait 3.2 seconds, drag slider to 73%, type HARD'
validates puzzle solution with behavioral analysis.
parameters:
puzzle_id (string): puzzle identifier from generate_puzzle()solution (object): puzzle solution databehavioral_data (object): behavioral tracking datasolution format:
{
wait_time: number, // actual wait time (ms)
slider_value: number, // slider final position (%)
input_text: string // input field text
}
behavioral_data format:
{
mouse_movements: array, // mouse movement coordinates/timestamps
keystroke_timings: array, // keystroke timing data
total_time: number // total completion time (ms)
}
returns: boolean (true if valid, false if invalid)
example:
const solution = {
wait_time: 3150,
slider_value: 73,
input_text: 'HARD'
};
const behavioral_data = {
mouse_movements: [...],
keystroke_timings: [...],
total_time: 8500
};
const is_valid = hardener.validate_puzzle(puzzle.id, solution, behavioral_data);
handles hex value generation and selector mapping.
new obfuscator(config = {})
configuration:
{
hex_length: 8, // hex value length
site_salt: 'default_salt', // deterministic seeding
pool_size: 100, // pre-generated pool size
use_non_hex_chars: true // include _, - characters
}
generates hex mappings for selector array.
parameters:
selectors (array): array of selector stringsreturns: object mapping original selectors to hex values
example:
const selectors = ['main', 'container', 'header'];
const mappings = obfuscator.generate_mappings(selectors);
// { main: 'a1b2c3d4', container: 'e5f6g7h8', header: 'f9a8b7c6' }
extracts and tracks selectors across html/css/js files.
extracts all selectors from content files.
parameters:
html_content (string): html contentcss_content (string): css contentjs_content (string): javascript contentreturns: array of unique selector strings
example:
const html = '<div id="main" class="container">';
const css = '.container { } #main { }';
const js = 'getElementById("main")';
const selectors = oracle.extract_selectors(html, css, js);
// ['main', 'container']
transforms content using obfuscated mappings.
remaps html content using selector mappings.
parameters:
html_content (string): original html contentmappings (object): selector to hex mappingsreturns: string with remapped selectors
remaps css content using selector mappings.
parameters:
css_content (string): original css contentmappings (object): selector to hex mappingsreturns: string with remapped selectors
remaps javascript content using selector mappings.
parameters:
js_content (string): original javascript contentmappings (object): selector to hex mappingsreturns: string with remapped selectors
coordinates puzzle generation and validation.
generates new behavioral puzzle.
returns: puzzle object (see generate_puzzle() above)
validates puzzle solution.
parameters: see validate_puzzle() above returns: boolean validation result
creates randomized behavioral puzzles.
creates behavioral puzzle with random parameters.
returns: puzzle configuration object
validates puzzle solutions with behavioral analysis.
validates complete puzzle solution.
parameters:
puzzle (object): original puzzle configurationsolution (object): user solutionbehavioral_data (object): behavioral tracking datareturns: validation result object
{
valid: boolean,
reasons: array, // validation failure reasons
score: number // behavioral confidence score (0-1)
}
manages session lifecycle and validation.
new session_manager(timeout = 3600000)
creates new session.
returns: session object
{
id: string,
created_at: timestamp,
state: 'new',
puzzle_attempts: 0,
fallback_triggers: []
}
checks session validity.
parameters:
session_id (string): session identifierreturns: boolean
updates session data.
parameters:
session_id (string): session identifierupdates (object): session updatesreturns: boolean success
handles session persistence.
stores session data.
retrieves session data.
deletes session data.
manages puzzle failure scenarios.
determines if session should fallback.
parameters:
session (object): session datareturns: boolean
generates fallback content.
parameters:
original_content (object): original html/css/js contentmappings (object): selector mappingsreturns: obfuscated content object
{
build_time: boolean, // buildtime vs runtime processing
puzzle_enabled: boolean, // enable puzzle system
session_timeout: number, // session timeout (ms)
debug: boolean, // enable debug logging
log_level: string, // 'error', 'warn', 'info', 'debug'
fallback_limits: {
timeout_limit: number, // puzzle timeouts before fallback
invalid_solution_limit: number, // invalid solutions before fallback
max_total_attempts: number // total attempts before permanent fallback
},
obfuscator: {
hex_length: number, // hex value length (4-16)
site_salt: string, // deterministic seeding salt
pool_size: number, // pre-generated hex pool size
use_non_hex_chars: boolean // include _, - for realism
},
puzzle: {
timeout: number, // puzzle timeout (ms)
wait_time_range: [min, max], // wait time range (ms)
slider_range: [min, max], // slider target range (%)
word_set: array // available input words
}
}
GHARD13_SALT=your_site_salt
GHARD13_HEX_LENGTH=8
GHARD13_PUZZLE_ENABLED=true
GHARD13_SESSION_TIMEOUT=3600000
GHARD13_DEBUG=false
GHARD13_LOG_LEVEL=info
configuration validation errors.
try {
const hardener = new ghard13({ hex_length: 'invalid' });
} catch (error) {
if (error instanceof GhardConfigError) {
console.log('configuration error:', error.message);
}
}
content processing errors.
try {
const result = hardener.harden_content(invalid_html, css, js);
} catch (error) {
if (error instanceof GhardProcessingError) {
console.log('processing error:', error.message);
}
}
session management errors.
try {
const valid = hardener.session_manager.is_valid('invalid_id');
} catch (error) {
if (error instanceof GhardSessionError) {
console.log('session error:', error.message);
}
}
// comprehensive error handling
try {
const hardener = new ghard13(config);
const result = hardener.harden_content(html, css, js);
if (result.mappings.length === 0) {
console.warn('no selectors found to obfuscate');
}
} catch (error) {
console.error('ghard13 error:', error.message);
// fallback to original content
return { html, css, js, mappings: {} };
}
const fs = require('fs');
const { ghard13 } = require('ghard13');
function build_with_hardening() {
const hardener = new ghard13({ build_time: true });
// read source files
const files = ['index.html', 'styles.css', 'script.js']
.map(file => fs.readFileSync(`src/${file}`, 'utf8'));
// harden content
const result = hardener.harden_content(...files);
// write hardened files
fs.writeFileSync('dist/index.html', result.html);
fs.writeFileSync('dist/styles.css', result.css);
fs.writeFileSync('dist/script.js', result.js);
// save mappings for debugging
fs.writeFileSync('dist/mappings.json', JSON.stringify(result.mappings, null, 2));
}
const express = require('express');
const { ghard13 } = require('ghard13');
const app = express();
const hardener = new ghard13({ build_time: false });
app.get('/', (req, res) => {
// check session
if (!hardener.session_manager.is_valid(req.session.id)) {
const puzzle = hardener.generate_puzzle();
return res.render('puzzle', { puzzle });
}
// serve hardened content
const result = hardener.harden_content(html, css, js);
res.render('index', result);
});
app.post('/puzzle', (req, res) => {
const { puzzle_id, solution, behavioral_data } = req.body;
const valid = hardener.validate_puzzle(puzzle_id, solution, behavioral_data);
if (valid) {
hardener.session_manager.update_session(req.session.id, { state: 'puzzle_solved' });
res.json({ success: true });
} else {
res.json({ success: false, retry: true });
}
});
configuration changes:
// old format
const hardener = new ghard13({
hexLength: 8,
siteSalt: 'salt'
});
// new format
const hardener = new ghard13({
obfuscator: {
hex_length: 8,
site_salt: 'salt'
}
});
method changes:
// old method
hardener.obfuscateContent(html, css, js);
// new method
hardener.harden_content(html, css, js);