engagelab-captcha-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/captcha-sdk.esm.js +1 -0
  2. package/dist/captcha-sdk.umd.js +1 -0
  3. package/dist/types/api.d.ts +12 -0
  4. package/dist/types/challenges/dragsort.d.ts +24 -0
  5. package/dist/types/challenges/grid.d.ts +20 -0
  6. package/dist/types/challenges/icons.d.ts +18 -0
  7. package/dist/types/challenges/invisible.d.ts +8 -0
  8. package/dist/types/challenges/physics.d.ts +40 -0
  9. package/dist/types/challenges/rotate.d.ts +14 -0
  10. package/dist/types/challenges/slider.d.ts +17 -0
  11. package/dist/types/challenges/spatial.d.ts +12 -0
  12. package/dist/types/compat/hcaptcha.d.ts +1 -0
  13. package/dist/types/compat/recaptcha.d.ts +1 -0
  14. package/dist/types/crypto.d.ts +11 -0
  15. package/dist/types/deep_probes.d.ts +13 -0
  16. package/dist/types/degradation.d.ts +27 -0
  17. package/dist/types/device_attestation.d.ts +19 -0
  18. package/dist/types/fingerprint.d.ts +2 -0
  19. package/dist/types/headless.d.ts +2 -0
  20. package/dist/types/i18n.d.ts +7 -0
  21. package/dist/types/index.d.ts +36 -0
  22. package/dist/types/integrity.d.ts +3 -0
  23. package/dist/types/logger.d.ts +49 -0
  24. package/dist/types/native_monitor.d.ts +21 -0
  25. package/dist/types/passive_signals.d.ts +119 -0
  26. package/dist/types/pow.d.ts +2 -0
  27. package/dist/types/reporter.d.ts +8 -0
  28. package/dist/types/session_persist.d.ts +18 -0
  29. package/dist/types/theme.d.ts +11 -0
  30. package/dist/types/timing.d.ts +2 -0
  31. package/dist/types/trajectory.d.ts +18 -0
  32. package/dist/types/types.d.ts +182 -0
  33. package/dist/types/ui/container.d.ts +20 -0
  34. package/dist/types/ui/styles.d.ts +1 -0
  35. package/dist/types/wasm_core.d.ts +44 -0
  36. package/dist/types/web-component.d.ts +11 -0
  37. package/package.json +22 -0
  38. package/rollup.config.mjs +62 -0
  39. package/src/api.ts +180 -0
  40. package/src/challenges/dragsort.ts +168 -0
  41. package/src/challenges/grid.ts +147 -0
  42. package/src/challenges/icons.ts +106 -0
  43. package/src/challenges/invisible.ts +57 -0
  44. package/src/challenges/physics.ts +437 -0
  45. package/src/challenges/rotate.ts +81 -0
  46. package/src/challenges/slider.ts +168 -0
  47. package/src/challenges/spatial.ts +91 -0
  48. package/src/compat/hcaptcha.ts +112 -0
  49. package/src/compat/recaptcha.ts +108 -0
  50. package/src/crypto.ts +69 -0
  51. package/src/deep_probes.ts +690 -0
  52. package/src/degradation.ts +113 -0
  53. package/src/device_attestation.ts +109 -0
  54. package/src/fingerprint.ts +247 -0
  55. package/src/headless.ts +233 -0
  56. package/src/i18n.ts +455 -0
  57. package/src/index.ts +527 -0
  58. package/src/integrity.ts +100 -0
  59. package/src/logger.ts +170 -0
  60. package/src/native_monitor.ts +100 -0
  61. package/src/passive_signals.ts +544 -0
  62. package/src/pow.ts +120 -0
  63. package/src/reporter.ts +75 -0
  64. package/src/session_persist.ts +90 -0
  65. package/src/theme.ts +41 -0
  66. package/src/timing.ts +79 -0
  67. package/src/trajectory.ts +110 -0
  68. package/src/types.ts +199 -0
  69. package/src/ui/container.ts +161 -0
  70. package/src/ui/styles.ts +153 -0
  71. package/src/wasm_core.ts +189 -0
  72. package/src/web-component.ts +103 -0
  73. package/tsconfig.json +18 -0
package/src/logger.ts ADDED
@@ -0,0 +1,170 @@
1
+ /**
2
+ * EngageLab CAPTCHA SDK Logger
3
+ *
4
+ * Provides structured logging with levels and optional console output.
5
+ * Can be enabled via config or localStorage for debugging.
6
+ */
7
+
8
+ export enum LogLevel {
9
+ NONE = 0,
10
+ ERROR = 1,
11
+ WARN = 2,
12
+ INFO = 3,
13
+ DEBUG = 4,
14
+ }
15
+
16
+ interface LogEntry {
17
+ timestamp: number;
18
+ level: string;
19
+ module: string;
20
+ message: string;
21
+ data?: any;
22
+ }
23
+
24
+ class SDKLogger {
25
+ private level: LogLevel = LogLevel.NONE;
26
+ private logs: LogEntry[] = [];
27
+ private maxLogs = 500;
28
+ private prefix = '[EngageLab-CAPTCHA]';
29
+ private apiBase = '';
30
+ private siteKey = '';
31
+ private sessionId = '';
32
+ private flushTimer: ReturnType<typeof setInterval> | null = null;
33
+ private isFlushing = false;
34
+
35
+ setLevel(level: LogLevel): void {
36
+ this.level = level;
37
+ }
38
+
39
+ enableDebug(): void {
40
+ this.level = LogLevel.DEBUG;
41
+ }
42
+
43
+ disable(): void {
44
+ this.level = LogLevel.NONE;
45
+ }
46
+
47
+ error(module: string, message: string, data?: any): void {
48
+ this.log(LogLevel.ERROR, module, message, data);
49
+ }
50
+
51
+ warn(module: string, message: string, data?: any): void {
52
+ this.log(LogLevel.WARN, module, message, data);
53
+ }
54
+
55
+ info(module: string, message: string, data?: any): void {
56
+ this.log(LogLevel.INFO, module, message, data);
57
+ }
58
+
59
+ debug(module: string, message: string, data?: any): void {
60
+ this.log(LogLevel.DEBUG, module, message, data);
61
+ }
62
+
63
+ getLogs(): LogEntry[] {
64
+ return [...this.logs];
65
+ }
66
+
67
+ clearLogs(): void {
68
+ this.logs = [];
69
+ }
70
+
71
+ private log(level: LogLevel, module: string, message: string, data?: any): void {
72
+ if (level > this.level) return;
73
+
74
+ const entry: LogEntry = {
75
+ timestamp: Date.now(),
76
+ level: LogLevel[level],
77
+ module,
78
+ message,
79
+ data,
80
+ };
81
+
82
+ this.logs.push(entry);
83
+ if (this.logs.length > this.maxLogs) {
84
+ this.logs.splice(0, this.logs.length - this.maxLogs);
85
+ }
86
+
87
+ const levelNames = ['', 'ERROR', 'WARN', 'INFO', 'DEBUG'];
88
+ const tag = `${this.prefix}[${levelNames[level]}][${module}]`;
89
+
90
+ if (level === LogLevel.ERROR) {
91
+ console.error(tag, message, data !== undefined ? data : '');
92
+ } else if (level === LogLevel.WARN) {
93
+ console.warn(tag, message, data !== undefined ? data : '');
94
+ } else if (level === LogLevel.INFO) {
95
+ console.info(tag, message, data !== undefined ? data : '');
96
+ } else {
97
+ console.log(tag, message, data !== undefined ? data : '');
98
+ }
99
+ }
100
+
101
+ configure(apiBase: string, siteKey: string): void {
102
+ this.apiBase = apiBase.replace(/\/$/, '');
103
+ this.siteKey = siteKey;
104
+ }
105
+
106
+ setSessionId(id: string): void {
107
+ this.sessionId = id;
108
+ }
109
+
110
+ startAutoFlush(intervalMs: number = 30000): void {
111
+ this.stopAutoFlush();
112
+ this.flushTimer = setInterval(() => this.flush(), intervalMs);
113
+ }
114
+
115
+ stopAutoFlush(): void {
116
+ if (this.flushTimer) {
117
+ clearInterval(this.flushTimer);
118
+ this.flushTimer = null;
119
+ }
120
+ }
121
+
122
+ async flush(): Promise<void> {
123
+ if (this.isFlushing || this.logs.length === 0 || !this.apiBase) return;
124
+ this.isFlushing = true;
125
+
126
+ const logsToSend = this.logs.splice(0, 50);
127
+ try {
128
+ await fetch(`${this.apiBase}/v1/logs/report`, {
129
+ method: 'POST',
130
+ headers: {
131
+ 'Content-Type': 'application/json',
132
+ 'X-Site-Key': this.siteKey,
133
+ },
134
+ body: JSON.stringify({
135
+ siteKey: this.siteKey,
136
+ sessionId: this.sessionId,
137
+ sdkVersion: '2.0.0',
138
+ platform: 'web',
139
+ logs: logsToSend,
140
+ }),
141
+ });
142
+ } catch {
143
+ // Put logs back on failure
144
+ this.logs.unshift(...logsToSend);
145
+ } finally {
146
+ this.isFlushing = false;
147
+ }
148
+ }
149
+
150
+ destroy(): void {
151
+ this.stopAutoFlush();
152
+ void this.flush();
153
+ }
154
+ }
155
+
156
+ // Check localStorage for debug flag
157
+ function shouldEnableDebug(): boolean {
158
+ try {
159
+ return localStorage.getItem('engagelab_captcha_debug') === 'true';
160
+ } catch {
161
+ return false;
162
+ }
163
+ }
164
+
165
+ export const sdkLogger = new SDKLogger();
166
+
167
+ // Auto-enable if debug flag is set
168
+ if (typeof window !== 'undefined' && shouldEnableDebug()) {
169
+ sdkLogger.enableDebug();
170
+ }
@@ -0,0 +1,100 @@
1
+ /**
2
+ * NativeMonitor — wrap sensitive global functions to detect automation tool injection.
3
+ *
4
+ * Records stack traces from calls to eval, Function constructor, and setTimeout(string).
5
+ * Provides disable() and a config flag (disableEvalPatching) for environments
6
+ * where wrapping interferes with HMR or dev tooling.
7
+ */
8
+ import type { NativeTrace } from './types';
9
+
10
+ const MAX_TRACES = 100;
11
+
12
+ export class NativeMonitor {
13
+ private traces: NativeTrace[] = [];
14
+ private originals: Record<string, any> = {};
15
+ private active = false;
16
+
17
+ start(): void {
18
+ if (this.active) return;
19
+ this.active = true;
20
+
21
+ this.wrapEval();
22
+ this.wrapFunctionConstructor();
23
+ this.wrapSetTimeoutString();
24
+ }
25
+
26
+ disable(): void {
27
+ this.active = false;
28
+ this.restore();
29
+ }
30
+
31
+ getTraces(): NativeTrace[] {
32
+ return this.traces.slice();
33
+ }
34
+
35
+ private record(fn: string): void {
36
+ if (!this.active) return;
37
+ const err = new Error();
38
+ const stack = (err.stack || '').split('\n').slice(2, 8).join('\n');
39
+ this.traces.push({ fn, stack, timestamp: Date.now() });
40
+ if (this.traces.length > MAX_TRACES) {
41
+ this.traces.splice(0, this.traces.length - MAX_TRACES);
42
+ }
43
+ }
44
+
45
+ private wrapEval(): void {
46
+ if (typeof window === 'undefined') return;
47
+ const origEval = window.eval;
48
+ this.originals['eval'] = origEval;
49
+ const self = this;
50
+
51
+ (window as any).eval = function (code: string) {
52
+ self.record('eval');
53
+ return origEval.call(this, code);
54
+ };
55
+ }
56
+
57
+ private wrapFunctionConstructor(): void {
58
+ const origCtor = Function.prototype.constructor;
59
+ this.originals['FunctionConstructor'] = origCtor;
60
+ const self = this;
61
+
62
+ try {
63
+ Function.prototype.constructor = function (this: any, ...args: any[]) {
64
+ self.record('Function.constructor');
65
+ return origCtor.apply(this, args);
66
+ } as any;
67
+ } catch {
68
+ // Some environments don't allow overriding Function.prototype.constructor
69
+ }
70
+ }
71
+
72
+ private wrapSetTimeoutString(): void {
73
+ if (typeof window === 'undefined') return;
74
+ const origSetTimeout = window.setTimeout;
75
+ this.originals['setTimeout'] = origSetTimeout;
76
+ const self = this;
77
+
78
+ (window as any).setTimeout = function (handler: any, timeout?: number, ...args: any[]) {
79
+ if (typeof handler === 'string') {
80
+ self.record('setTimeout(string)');
81
+ }
82
+ return origSetTimeout.call(this, handler, timeout, ...args);
83
+ };
84
+ }
85
+
86
+ private restore(): void {
87
+ if (this.originals['eval'] && typeof window !== 'undefined') {
88
+ (window as any).eval = this.originals['eval'];
89
+ }
90
+ if (this.originals['FunctionConstructor']) {
91
+ try {
92
+ Function.prototype.constructor = this.originals['FunctionConstructor'];
93
+ } catch { /* ignore */ }
94
+ }
95
+ if (this.originals['setTimeout'] && typeof window !== 'undefined') {
96
+ (window as any).setTimeout = this.originals['setTimeout'];
97
+ }
98
+ this.originals = {};
99
+ }
100
+ }