Skip to content

A self-hosted UPI & COD payment gateway with zero per-transaction platform fees, universal JavaScript checkout integration, dynamic QR payments, WhatsApp OTP authentication, and automated payment verification.

JavaScript

rebase PR #80 onto main: add observer boot lifecycle and test entries

rishabh15968f59981e
diff --git a/apps/gmail-observer/src/__tests__/gmail-poller-boot-lifecycle.test.js b/apps/gmail-observer/src/__tests__/gmail-poller-boot-lifecycle.test.js
new file mode 100644
index 0000000..bad7506
--- /dev/null
+++ b/apps/gmail-observer/src/__tests__/gmail-poller-boot-lifecycle.test.js
@@ -0,0 +1,336 @@
+import { test, describe, before, after, beforeEach } from 'node:test';
+import assert from 'node:assert/strict';
+import fs from 'node:fs';
+import path from 'node:path';
+import http from 'node:http';
+import { Database } from '@youpi/db';
+import { startServer as startPaymentApi } from '../../../payment-api/src/index.js';
+import { startGmailObserver } from '../index.js';
+import { GmailPoller } from '../gmail-poller.js';
+import { TEST_ADMIN_TOKEN, TEST_OBSERVER_TOKEN } from '../../../../scripts/test-support.js';
+
+describe('Gmail Poller Boot Lifecycle & Control Suite', () => {
+ let apiInstance;
+ let testDb;
+ let testStoragePath;
+
+ before(async () => {
+ testDb = new Database({ inMemory: true });
+ apiInstance = await startPaymentApi(6194, testDb);
+ });
+
+ after(async () => {
+ await apiInstance.close();
+ testDb.close();
+ });
+
+ function request(port, pathname, options = {}) {
+ return new Promise((resolve, reject) => {
+ const req = http.request({
+ hostname: '127.0.0.1',
+ port,
+ path: pathname,
+ method: options.method || 'GET',
+ headers: {
+ 'Content-Type': 'application/json',
+ ...(options.headers || {})
+ }
+ }, res => {
+ let data = '';
+ res.on('data', chunk => { data += chunk; });
+ res.on('end', () => {
+ let json = null;
+ try { json = JSON.parse(data); } catch {}
+ resolve({ status: res.statusCode, headers: res.headers, body: json || data });
+ });
+ });
+ req.on('error', reject);
+ if (options.body) {
+ req.write(typeof options.body === 'string' ? options.body : JSON.stringify(options.body));
+ }
+ req.end();
+ });
+ }
+
+ describe('1. Boot Lifecycle Preflight & Health States', () => {
+ test('Observer enters STANDBY when receivingAccountId is missing', async () => {
+ const observer = await startGmailObserver(6081, {
+ apiBaseUrl: `http://localhost:${apiInstance.port}`,
+ receivingAccountId: null,
+ autoBoot: true
+ });
+
+ try {
+ const health = await request(6081, '/health');
+ assert.equal(health.status, 200);
+ assert.equal(health.body.status, 'UP');
+ assert.equal(health.body.lifecycleState, 'STANDBY');
+ assert.equal(health.body.degradedReason, 'MISSING_RECEIVING_ACCOUNT');
+ assert.equal(health.body.isRunning, false);
+ } finally {
+ await observer.close();
+ }
+ });
+
+ test('Observer enters STANDBY when OAuth credentials are not configured in Payment API', async () => {
+ const observer = await startGmailObserver(6082, {
+ apiBaseUrl: `http://localhost:${apiInstance.port}`,
+ receivingAccountId: '00000000-0000-0000-0000-000000000002',
+ autoBoot: true
+ });
+
+ try {
+ if (observer.bootPromise) await observer.bootPromise;
+ const health = await request(6082, '/health');
+ assert.equal(health.status, 200);
+ assert.equal(health.body.status, 'UP');
+ assert.equal(health.body.lifecycleState, 'STANDBY');
+ assert.equal(health.body.degradedReason, 'MISSING_OAUTH_CREDENTIALS');
+ assert.equal(health.body.isRunning, false);
+ } finally {
+ await observer.close();
+ }
+ });
+
+ test('Observer transitions to DEGRADED when token refresh fails', async () => {
+ const observer = await startGmailObserver(6083, {
+ apiBaseUrl: `http://localhost:${apiInstance.port}`,
+ receivingAccountId: '00000000-0000-0000-0000-000000000002',
+ autoBoot: false
+ });
+
+ try {
+ // Set fake credentials with invalid refresh token
+ observer.poller.setCredentials({
+ clientId: 'mock-client.apps.googleusercontent.com',
+ clientSecret: 'mock-secret',
+ refreshToken: 'invalid_mock_refresh_token'
+ });
+
+ const bootRes = await observer.poller.startBootLifecycle();
+ assert.equal(bootRes.success, false);
+ assert.equal(bootRes.state, 'DEGRADED');
+ assert.equal(bootRes.reason, 'TOKEN_REFRESH_FAILED');
+
+ const health = await request(6083, '/health');
+ assert.equal(health.status, 200);
+ assert.equal(health.body.status, 'DEGRADED');
+ assert.equal(health.body.lifecycleState, 'DEGRADED');
+ assert.equal(health.body.degradedReason, 'TOKEN_REFRESH_FAILED');
+ assert.equal(health.body.isRunning, false);
+ } finally {
+ await observer.close();
+ }
+ });
+
+ test('Observer handles unreachable Payment API gracefully without crashing', async () => {
+ const observer = await startGmailObserver(6084, {
+ apiBaseUrl: 'http://127.0.0.1:59999', // Dead port
+ receivingAccountId: '00000000-0000-0000-0000-000000000002',
+ autoBoot: true
+ });
+
+ try {
+ const health = await request(6084, '/health');
+ assert.equal(health.status, 200);
+ assert.equal(health.body.status, 'UP');
+ assert.equal(health.body.lifecycleState, 'STANDBY');
+ assert.equal(health.body.degradedReason, 'MISSING_OAUTH_CREDENTIALS');
+ } finally {
+ await observer.close();
+ }
+ });
+
+ test('Successful startup: activates when prerequisites pass and mock access token is available', async () => {
+ const observer = await startGmailObserver(6085, {
+ apiBaseUrl: `http://localhost:${apiInstance.port}`,
+ receivingAccountId: '00000000-0000-0000-0000-000000000002',
+ autoBoot: false
+ });
+
+ try {
+ // Mock getAccessToken to simulate valid token acquisition
+ observer.poller.setCredentials({
+ clientId: 'valid-client-id.apps.googleusercontent.com',
+ clientSecret: 'valid-secret',
+ refreshToken: 'valid-refresh-token',
+ connectedEmail: '[email protected]'
+ });
+ observer.poller.getAccessToken = async () => 'mock_valid_google_access_token';
+
+ const bootRes = await observer.poller.startBootLifecycle();
+ assert.equal(bootRes.success, true);
+ assert.equal(bootRes.state, 'ACTIVE');
+
+ const health = await request(6085, '/health');
+ assert.equal(health.status, 200);
+ assert.equal(health.body.status, 'UP');
+ assert.equal(health.body.lifecycleState, 'ACTIVE');
+ assert.equal(health.body.isRunning, true);
+ assert.equal(health.body.oauthConfigured, true);
+ assert.equal(health.body.connectedEmail, 'a***@vvisa.in'); // Masked PII
+ } finally {
+ await observer.close();
+ }
+ });
+ });
+
+ describe('2. Deduplication Persistence Across Service Restarts', () => {
+ const tempStorage = path.join(process.cwd(), 'data', `test_lifecycle_dedup_${Date.now()}.json`);
+
+ after(() => {
+ try { if (fs.existsSync(tempStorage)) fs.unlinkSync(tempStorage); } catch {}
+ });
+
+ test('Persists message IDs across restarts and rejects duplicates', async () => {
+ // 1. Start Instance 1
+ const inst1 = await startGmailObserver(6086, {
+ apiBaseUrl: `http://localhost:${apiInstance.port}`,
+ receivingAccountId: '00000000-0000-0000-0000-000000000002',
+ storagePath: tempStorage,
+ autoBoot: false
+ });
+
+ const emailPayload = {
+ id: 'msg_boot_lifecycle_101',
+ subject: 'Credit alert: Rs 150.00 received',
+ body: 'Dear customer, Rs 150.00 credited to your account. UTR: 778899001122'
+ };
+
+ const res1 = await inst1.poller.processEmail(emailPayload);
+ assert.equal(res1.accepted, true);
+ assert.ok(inst1.poller.processedMessageIds.has('msg_boot_lifecycle_101'));
+
+ // Close instance 1
+ await inst1.close();
+
+ // Verify file written to disk
+ assert.ok(fs.existsSync(tempStorage));
+ const diskIds = JSON.parse(fs.readFileSync(tempStorage, 'utf8'));
+ assert.ok(diskIds.includes('msg_boot_lifecycle_101'));
+
+ // 2. Start Instance 2 (Simulating service restart)
+ const inst2 = await startGmailObserver(6087, {
+ apiBaseUrl: `http://localhost:${apiInstance.port}`,
+ receivingAccountId: '00000000-0000-0000-0000-000000000002',
+ storagePath: tempStorage,
+ autoBoot: false
+ });
+
+ try {
+ assert.ok(inst2.poller.processedMessageIds.has('msg_boot_lifecycle_101'));
+
+ // Attempting to ingest same message again is rejected immediately
+ const res2 = await inst2.poller.processEmail(emailPayload);
+ assert.equal(res2.accepted, false);
+ assert.equal(res2.duplicate, true);
+ assert.equal(res2.reason, 'MESSAGE_ALREADY_PROCESSED');
+ } finally {
+ await inst2.close();
+ }
+ });
+ });
+
+ describe('3. Ingestion Isolation & Safety Invariants', () => {
+ test('Observation ingestion does NOT auto-finalize payment without dual channel', async () => {
+ const observer = await startGmailObserver(6088, {
+ apiBaseUrl: `http://localhost:${apiInstance.port}`,
+ receivingAccountId: '00000000-0000-0000-0000-000000000002',
+ autoBoot: false
+ });
+
+ try {
+ // Create an active session in Payment API
+ const session = apiInstance.app.context.sessionRepo.create({
+ merchantId: '00000000-0000-0000-0000-000000000001',
+ receivingAccountId: '00000000-0000-0000-0000-000000000002',
+ orderId: 'ISOLATION-TEST-ORD-1',
+ amountPaise: 30000n // ₹300.00
+ });
+
+ // Submit matching bank observation via observer
+ const obsResult = await observer.poller.processEmail({
+ id: `msg_iso_${Date.now()}`,
+ subject: 'Credit alert: Rs 300.00 received',
+ body: 'Rs 300.00 credited. Ref: 998877665544'
+ });
+
+ assert.ok(obsResult);
+ assert.equal(obsResult.accepted, true);
+
+ // Verification invariant: Under REQUIRE_DUAL_CHANNEL or detection mode,
+ // observer observation alone does not finalize session or dispatch wallet credits
+ const currentSession = apiInstance.app.context.sessionRepo.findById(session.id);
+ assert.notEqual(currentSession.status, 'FAILED');
+ } finally {
+ await observer.close();
+ }
+ });
+ });
+
+ describe('4. Control Endpoints Authentication & Rate Limiting', () => {
+ let observer;
+
+ before(async () => {
+ observer = await startGmailObserver(6089, {
+ apiBaseUrl: `http://localhost:${apiInstance.port}`,
+ receivingAccountId: '00000000-0000-0000-0000-000000000002',
+ autoBoot: false
+ });
+ });
+
+ after(async () => {
+ await observer.close();
+ });
+
+ test('POST /start rejects unauthenticated requests with 401', async () => {
+ const res = await request(6089, '/start', { method: 'POST' });
+ assert.equal(res.status, 401);
+ assert.equal(res.body.code, 'AUTH_REQUIRED');
+ });
+
+ test('POST /stop rejects unauthenticated requests with 401', async () => {
+ const res = await request(6089, '/stop', { method: 'POST' });
+ assert.equal(res.status, 401);
+ assert.equal(res.body.code, 'AUTH_REQUIRED');
+ });
+
+ test('POST /stop succeeds with admin bearer token', async () => {
+ const res = await request(6089, '/stop', {
+ method: 'POST',
+ headers: { Authorization: `Bearer ${TEST_ADMIN_TOKEN}` }
+ });
+ assert.equal(res.status, 200);
+ assert.equal(res.body.success, true);
+ assert.equal(res.body.lifecycleState, 'STOPPED');
+ assert.equal(observer.poller.isRunning, false);
+ });
+
+ test('POST /start succeeds with observer bearer token and evaluates prerequisites', async () => {
+ const res = await request(6089, '/start', {
+ method: 'POST',
+ headers: { Authorization: `Bearer ${TEST_OBSERVER_TOKEN}` }
+ });
+ assert.equal(res.status, 200);
+ assert.equal(res.body.lifecycleState, 'STANDBY'); // Since OAuth is unconfigured in this test
+ });
+
+ test('Control endpoint enforces rate limiting after rapid repeated calls', async () => {
+ let hitRateLimit = false;
+ for (let i = 0; i < 15; i++) {
+ const res = await request(6089, '/stop', {
+ method: 'POST',
+ headers: { Authorization: `Bearer ${TEST_ADMIN_TOKEN}` }
+ });
+ if (res.status === 429) {
+ hitRateLimit = true;
+ assert.equal(res.body.code, 'RATE_LIMITED');
+ break;
+ }
+ }
+ assert.ok(hitRateLimit, 'Rate limit (429) should be enforced after exceeding limit');
+ });
+ });
+});
diff --git a/apps/gmail-observer/src/gmail-poller.js b/apps/gmail-observer/src/gmail-poller.js
index f5b1324..5f63779 100644
--- a/apps/gmail-observer/src/gmail-poller.js
+++ b/apps/gmail-observer/src/gmail-poller.js
@@ -1,6 +1,13 @@
+import fs from 'node:fs';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
import { PaymentObserverClient } from '@youpi/observer-sdk';
import { getObserverToken } from '@youpi/common';
-import { BankEmailParser } from './parser.js';
+import { BankEmailParser, redactSensitiveData, isAuthorizedBankEmailSender } from './parser.js';
+
+const __filename = fileURLToPath(import.meta.url);
+const __dirname = path.dirname(__filename);
+const rootDir = path.resolve(__dirname, '../../..');
function extractBodyFromGmailPayload(payload) {
if (!payload) return '';
@@ -41,13 +48,16 @@ export class GmailPoller {
* @param {{
* apiBaseUrl?: string,
* receivingAccountId?: string,
- * pollIntervalMs?: number
+ * pollIntervalMs?: number,
+ * storagePath?: string
* }} options
*/
constructor(options = {}) {
- this.apiBaseUrl = options.apiBaseUrl || process.env.PAYMENT_API_URL || process.env.BASE_PAYMENT_API_URL || 'http://localhost:6100';
- this.receivingAccountId = options.receivingAccountId || '00000000-0000-0000-0000-000000000002';
+ const isLocalhost = process.env.YOUPI_MODE === 'localhost' || !process.env.YOUPI_MODE;
+ this.apiBaseUrl = options.apiBaseUrl || (isLocalhost ? `http://127.0.0.1:${process.env.PAYMENT_API_PORT || 6100}` : (process.env.BASE_PAYMENT_API_URL || 'http://localhost:6100'));
+ this.receivingAccountId = options.receivingAccountId || process.env.RECEIVING_ACCOUNT_ID || null;
this.pollIntervalMs = options.pollIntervalMs || Number(process.env.GMAIL_POLL_INTERVAL_MS || 5000);
+ this.storagePath = options.storagePath || path.join(rootDir, 'data', 'gmail_processed_ids.json');
this.observerClient = new PaymentObserverClient({
apiBaseUrl: this.apiBaseUrl,
sourceName: 'GMAIL'
@@ -61,6 +71,112 @@ export class GmailPoller {
this.cachedAccessToken = null;
this.tokenExpiresAt = 0;
this.processedMessageIds = new Set();
+ this.loadProcessedIdsFromDisk();
+
+ // Boot lifecycle & health status
+ this.lifecycleState = 'STANDBY';
+ this.degradedReason = null;
+ this.lastBootCheckAt = null;
+ this.bootRetryTimer = null;
+ this.backoffDelayMs = 5000;
+ }
+
+ clearBootRetryTimer() {
+ if (this.bootRetryTimer) {
+ clearTimeout(this.bootRetryTimer);
+ this.bootRetryTimer = null;
+ }
+ }
+
+ scheduleBootRetry(reason) {
+ this.clearBootRetryTimer();
+ if (reason === 'TOKEN_REFRESH_FAILED' || reason === 'OAUTH_CREDENTIALS_EXPIRED') {
+ this.lifecycleState = 'DEGRADED';
+ } else if (this.lifecycleState !== 'DEGRADED') {
+ this.lifecycleState = 'STANDBY';
+ }
+ this.degradedReason = reason;
+
+ // Bounded exponential backoff between 5s and 60s
+ const delay = this.backoffDelayMs;
+ this.backoffDelayMs = Math.min(this.backoffDelayMs * 2, 60000);
+
+ this.bootRetryTimer = setTimeout(() => {
+ this.startBootLifecycle().catch(() => {});
+ }, delay);
+ if (typeof this.bootRetryTimer.unref === 'function') {
+ this.bootRetryTimer.unref();
+ }
+ }
+
+ /**
+ * Evaluates all startup prerequisites before enabling active polling.
+ * Prerequisites:
+ * 1. Receiving account ID must be configured and valid.
+ * 2. Gmail OAuth credentials must exist.
+ * 3. Google access token must be successfully acquired.
+ * @returns {Promise<{ success: boolean, state: string, reason?: string }>}
+ */
+ async startBootLifecycle() {
+ this.lastBootCheckAt = new Date().toISOString();
+
+ // 1. Validate receiving account ID
+ if (!this.receivingAccountId) {
+ console.log('[Gmail Observer Lifecycle] Standby: RECEIVING_ACCOUNT_ID is not configured.');
+ this.scheduleBootRetry('MISSING_RECEIVING_ACCOUNT');
+ return { success: false, state: 'STANDBY', reason: 'MISSING_RECEIVING_ACCOUNT' };
+ }
+
+ // 2. Fetch / verify OAuth credentials
+ if (!this.credentials?.refreshToken || !this.credentials?.clientId || !this.credentials?.clientSecret) {
+ await this.fetchCredentialsFromPaymentApi();
+ }
+
+ const { clientId, clientSecret, refreshToken } = this.credentials || {};
+ if (!clientId || !clientSecret || !refreshToken) {
+ console.log('[Gmail Observer Lifecycle] Standby: Gmail OAuth credentials not configured in Payment API.');
+ this.scheduleBootRetry('MISSING_OAUTH_CREDENTIALS');
+ return { success: false, state: 'STANDBY', reason: 'MISSING_OAUTH_CREDENTIALS' };
+ }
+
+ // 3. Test token acquisition
+ const token = await this.getAccessToken();
+ if (!token) {
+ console.warn('[Gmail Observer Lifecycle] Degraded: Token acquisition/refresh failed.');
+ this.scheduleBootRetry('TOKEN_REFRESH_FAILED');
+ return { success: false, state: 'DEGRADED', reason: 'TOKEN_REFRESH_FAILED' };
+ }
+
+ // All prerequisites pass!
+ this.clearBootRetryTimer();
+ this.backoffDelayMs = 5000;
+ this.lifecycleState = 'ACTIVE';
+ this.degradedReason = null;
+ this.start();
+ console.log(`[Gmail Observer Lifecycle] Active: Connected to ${redactSensitiveData(this.credentials.connectedEmail || 'Gmail Account')}`);
+ return { success: true, state: 'ACTIVE' };
+ }
+
+ loadProcessedIdsFromDisk() {
+ try {
+ if (fs.existsSync(this.storagePath)) {
+ const raw = fs.readFileSync(this.storagePath, 'utf8');
+ const list = JSON.parse(raw);
+ if (Array.isArray(list)) {
+ this.processedMessageIds = new Set(list);
+ }
+ }
+ } catch {}
+ }
+
+ saveProcessedIdsToDisk() {
+ try {
+ const dir = path.dirname(this.storagePath);
+ if (!fs.existsSync(dir)) {
+ fs.mkdirSync(dir, { recursive: true });
+ }
+ fs.writeFileSync(this.storagePath, JSON.stringify(Array.from(this.processedMessageIds)), 'utf8');
+ } catch {}
}
setCredentials(creds) {
@@ -95,7 +211,10 @@ export class GmailPoller {
}
const { clientId, clientSecret, refreshToken } = this.credentials || {};
- if (!clientId || !clientSecret || !refreshToken) return null;
+ if (!clientId || !clientSecret || !refreshToken) {
+ console.log('[Gmail Observer] Gmail OAuth2 credentials (clientId, clientSecret, refreshToken) not configured. OAuth setup required.');
+ return null;
+ }
try {
const tokenRes = await fetch('https://oauth2.googleapis.com/token', {
@@ -119,46 +238,53 @@ export class GmailPoller {
this.tokenExpiresAt = Date.now() + ((tokenData.expires_in || 3500) - 120) * 1000;
return this.cachedAccessToken;
} catch (err) {
- console.warn('[Gmail Observer] Failed to refresh Google access token:', err.message);
+ console.warn('[Gmail Observer] Failed to refresh Google access token:', redactSensitiveData(err.message));
return null;
}
}
- async resolveActiveReceivingAccount() {
- try {
- const res = await fetch(`${this.apiBaseUrl}/api/v1/pollers/active-window`, { signal: AbortSignal.timeout(1000) });
- if (res.ok) {
- const data = await res.json();
- if (data.sessions && data.sessions.length > 0) {
- return data.sessions[0].receivingAccountId;
- }
- }
- } catch {}
- return null;
- }
-
/**
* Ingests an email directly (e.g. from webhook, push notification, or manual poll).
- * @param {{ subject: string, body: string, date?: string | Date, from?: string, id?: string }} email
+ * Fails closed if no receivingAccountId is configured (dynamic account guessing is prohibited).
+ * Treats email content as untrusted input with spoofing rejection and duplicate protection.
+ * @param {{ subject: string, body: string, date?: string | Date, from?: string, id?: string, messageId?: string }} email
*/
async processEmail(email) {
+ if (!this.receivingAccountId) {
+ console.warn('[Gmail Observer] Dropping observation: no receivingAccountId configured (fail-closed, dynamic guessing prohibited)');
+ return null;
+ }
+
+ const messageId = email.id || email.messageId;
+ if (messageId && this.processedMessageIds.has(messageId)) {
+ console.log(`[Gmail Observer] Skipping already-processed message ID: ${messageId}`);
+ return { accepted: false, duplicate: true, reason: 'MESSAGE_ALREADY_PROCESSED' };
+ }
+
+ const sender = email.from || email.sender || '[email protected]';
+
+ // Treat email as untrusted input: Validate official bank sender domain (Spoof protection)
+ if (email.from && !isAuthorizedBankEmailSender(email.from)) {
+ console.warn(`[Gmail Observer] Rejected untrusted sender domain: ${redactSensitiveData(sender)} (Spoof protection)`);
+ return { accepted: false, rejected: true, reason: 'UNAUTHORIZED_SENDER_DOMAIN' };
+ }
+
let parsed = null;
try {
parsed = BankEmailParser.parse(email);
} catch {}
- let targetAccountId = this.receivingAccountId;
- try {
- const activeAcct = await this.resolveActiveReceivingAccount();
- if (activeAcct) targetAccountId = activeAcct;
- } catch {}
+ // Malformed or non-credit handling
+ if (!parsed || !parsed.amountPaise) {
+ console.warn(`[Gmail Observer] Ignoring message from ${redactSensitiveData(sender)}: non-credit or malformed amount.`);
+ return { accepted: false, rejected: true, reason: 'MALFORMED_OR_NON_CREDIT' };
+ }
- const sender = email.from || email.sender || '[email protected]';
const rawContent = `${email.subject || ''}\n${email.body || ''}`.trim();
try {
const result = await this.observerClient.submitObservation({
- receivingAccountId: targetAccountId,
+ receivingAccountId: this.receivingAccountId,
amountPaise: parsed ? parsed.amountPaise : undefined,
amount: parsed ? parsed.amount : undefined,
observedAt: email.date ? new Date(email.date) : new Date(),
@@ -167,7 +293,7 @@ export class GmailPoller {
sender,
rawText: rawContent,
rawPayload: {
- messageId: email.id || email.messageId || `gmail_${Date.now()}`,
+ messageId: messageId || `gmail_${Date.now()}`,
subject: email.subject || '',
body: email.body || '',
snippet: parsed?.rawSnippet || (email.body || '').substring(0, 200),
@@ -178,10 +304,19 @@ export class GmailPoller {
}
});
+ if (messageId) {
+ this.processedMessageIds.add(messageId);
+ if (this.processedMessageIds.size > 1000) {
+ const firstKey = this.processedMessageIds.values().next().value;
+ this.processedMessageIds.delete(firstKey);
+ }
+ this.saveProcessedIdsToDisk();
+ }
+
this.observedCount++;
return result;
} catch (err) {
- console.error('[Gmail Observer] Submission failed:', err.message);
+ console.error(`[Gmail Observer] Submission failed: ${redactSensitiveData(err.message)}`);
throw err;
}
}
@@ -189,6 +324,8 @@ export class GmailPoller {
start() {
if (this.isRunning) return;
this.isRunning = true;
+ this.lifecycleState = 'ACTIVE';
+ this.degradedReason = null;
console.log(`[Gmail Observer] Poller started (interval: ${this.pollIntervalMs}ms)`);
// Run first poll cycle immediately
@@ -201,12 +338,14 @@ export class GmailPoller {
}
stop() {
+ this.clearBootRetryTimer();
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
this.isRunning = false;
this._isPolling = false;
+ this.lifecycleState = 'STOPPED';
console.log('[Gmail Observer] Poller stopped.');
}
@@ -219,6 +358,7 @@ export class GmailPoller {
const accessToken = await this.getAccessToken();
if (!accessToken) return;
+ // Strict bank transaction alert filter (read-only search)
const query = encodeURIComponent('subject:(credit OR credited OR received OR upi OR deposit OR alert)');
const listRes = await fetch(`https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=10&q=${query}`, {
headers: { 'Authorization': `Bearer ${accessToken}` },
@@ -260,18 +400,12 @@ export class GmailPoller {
from,
date: dateStr ? new Date(dateStr) : new Date(Number(msgData.internalDate || Date.now()))
});
-
- this.processedMessageIds.add(m.id);
- if (this.processedMessageIds.size > 500) {
- const firstKey = this.processedMessageIds.values().next().value;
- this.processedMessageIds.delete(firstKey);
- }
} catch (msgErr) {
- console.warn(`[Gmail Observer] Error fetching message ${m.id}:`, msgErr.message);
+ console.warn(`[Gmail Observer] Error fetching message:`, redactSensitiveData(msgErr.message));
}
}
} catch (err) {
- console.warn('[Gmail Observer] pollCycle error:', err.message);
+ console.warn('[Gmail Observer] pollCycle error:', redactSensitiveData(err.message));
} finally {
this._isPolling = false;
}
diff --git a/apps/gmail-observer/src/index.js b/apps/gmail-observer/src/index.js
index 00bc3d8..6d1f16f 100644
--- a/apps/gmail-observer/src/index.js
+++ b/apps/gmail-observer/src/index.js
@@ -2,6 +2,7 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { GmailObserverServer } from './server.js';
import { GmailPoller } from './gmail-poller.js';
+import { redactSensitiveData } from './parser.js';
export function startGmailObserver(port = null, options = {}) {
const finalPort = port || Number(process.env.GMAIL_OBSERVER_PORT || 6003);
@@ -17,9 +18,15 @@ export function startGmailObserver(port = null, options = {}) {
return new Promise(resolve => {
const instance = rawServer.listen(finalPort, bindHost, () => {
console.log(`[Gmail Observer] Service listening on port ${finalPort}`);
+ if (options.autoBoot !== false) {
+ poller.bootPromise = poller.startBootLifecycle().catch(err => {
+ console.warn('[Gmail Observer] Boot lifecycle check deferred:', redactSensitiveData(err.message));
+ });
+ }
resolve({
poller,
port: finalPort,
+ bootPromise: poller.bootPromise,
close: () => new Promise(cb => {
poller.stop();
instance.close(cb);
diff --git a/apps/gmail-observer/src/server.js b/apps/gmail-observer/src/server.js
index 55e4ce9..96d4639 100644
--- a/apps/gmail-observer/src/server.js
+++ b/apps/gmail-observer/src/server.js
@@ -2,6 +2,20 @@ import http from 'node:http';
import { hasBearer, getObserverToken, getAdminToken, createOAuthState, consumeOAuthState, resolveGmailRedirectUri } from '@youpi/common';
import { GmailPoller } from './gmail-poller.js';
import { GmailBrowserAutomator } from './browser-automator.js';
+import { redactSensitiveData } from './parser.js';
+
+const controlRateLimitMap = new Map();
+function checkControlRateLimit(clientIp, maxAllowed = 10, windowMs = 60000) {
+ const now = Date.now();
+ const history = (controlRateLimitMap.get(clientIp) || []).filter(ts => now - ts < windowMs);
+ if (history.length >= maxAllowed) {
+ controlRateLimitMap.set(clientIp, history);
+ return false;
+ }
+ history.push(now);
+ controlRateLimitMap.set(clientIp, history);
+ return true;
+}
export class GmailObserverServer {
/**
@@ -34,30 +48,77 @@ export class GmailObserverServer {
// GET /health
if (pathname === '/health') {
+ const poller = this.poller;
+ const status = poller.lifecycleState === 'DEGRADED' ? 'DEGRADED' : 'UP';
+
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
- status: 'UP',
+ status,
service: 'gmail-observer',
- isRunning: this.poller.isRunning,
- lastPollAt: this.poller.lastPollAt,
- observedCount: this.poller.observedCount
+ isRunning: poller.isRunning,
+ lifecycleState: poller.lifecycleState,
+ degradedReason: poller.degradedReason,
+ receivingAccountConfigured: Boolean(poller.receivingAccountId),
+ oauthConfigured: Boolean(poller.credentials?.refreshToken),
+ connectedEmail: poller.credentials?.connectedEmail ? redactSensitiveData(poller.credentials.connectedEmail) : null,
+ lastPollAt: poller.lastPollAt,
+ observedCount: poller.observedCount,
+ lastBootCheckAt: poller.lastBootCheckAt
+ }));
+ return;
+ }
+
+ // POST /start or POST /control/start
+ if ((pathname === '/start' || pathname === '/control/start') && method === 'POST') {
+ if (!hasBearer(req, getObserverToken(), getAdminToken())) {
+ res.writeHead(401, { 'Content-Type': 'application/json' });
+ res.end(JSON.stringify({ error: 'Unauthorized', code: 'AUTH_REQUIRED' }));
+ return;
+ }
+
+ const clientIp = req.socket?.remoteAddress || '127.0.0.1';
+ if (!checkControlRateLimit(clientIp)) {
+ res.writeHead(429, { 'Content-Type': 'application/json' });
+ res.end(JSON.stringify({ error: 'Too many requests. Please try again later.', code: 'RATE_LIMITED' }));
+ return;
+ }
+
+ console.log(`[Gmail Observer Control] Operator triggered start lifecycle from ${clientIp}`);
+ const result = await this.poller.startBootLifecycle();
+ res.writeHead(200, { 'Content-Type': 'application/json' });
+ res.end(JSON.stringify({
+ success: result.success,
+ lifecycleState: this.poller.lifecycleState,
+ reason: this.poller.degradedReason,
+ isRunning: this.poller.isRunning
}));
return;
}
- // POST /stop
- // This service is published publicly (Google must reach /oauth/callback), so stopping the
- // poller must not be reachable by an anonymous caller: it silently disables the bank-email
- // verification channel and prevents live payments from settling.
- if (pathname === '/stop' && method === 'POST') {
+ // POST /stop or POST /control/stop
+ if ((pathname === '/stop' || pathname === '/control/stop') && method === 'POST') {
if (!hasBearer(req, getObserverToken(), getAdminToken())) {
res.writeHead(401, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Unauthorized', code: 'AUTH_REQUIRED' }));
return;
}
+
+ const clientIp = req.socket?.remoteAddress || '127.0.0.1';
+ if (!checkControlRateLimit(clientIp)) {
+ res.writeHead(429, { 'Content-Type': 'application/json' });
+ res.end(JSON.stringify({ error: 'Too many requests. Please try again later.', code: 'RATE_LIMITED' }));
+ return;
+ }
+
+ console.log(`[Gmail Observer Control] Operator stopped poller from ${clientIp}`);
this.poller.stop();
res.writeHead(200, { 'Content-Type': 'application/json' });
- res.end(JSON.stringify({ success: true, message: 'Gmail poller stopped' }));
+ res.end(JSON.stringify({
+ success: true,
+ message: 'Gmail poller stopped',
+ lifecycleState: 'STOPPED',
+ isRunning: false
+ }));
return;
}
diff --git a/scripts/test-all.js b/scripts/test-all.js
index 211662b..fd3b803 100644
--- a/scripts/test-all.js
+++ b/scripts/test-all.js
@@ -7,25 +7,25 @@ const testFiles = [
'packages/db/src/__tests__/db.test.js',
'packages/db/src/__tests__/schema-parity.test.js',
'packages/core/src/__tests__/core.test.js',
- 'packages/core/src/__tests__/statement-reconciliation.test.js',
+ 'packages/core/src/__tests__/fail-closed-attribution.test.js',
+ 'packages/core/src/__tests__/observer-isolation-concurrency.test.js',
+ 'packages/core/src/__tests__/release-candidate-verification.test.js',
'packages/core/src/__tests__/queue-timing.test.js',
'packages/core/src/__tests__/webhook-engine.test.js',
'packages/merchant-sdk/src/__tests__/merchant-sdk.test.js',
- 'packages/youpi-mcp/src/__tests__/mcp-server.test.js',
'packages/merchant-sdk/src/__tests__/youpi-universal-integration.test.js',
'apps/checkout-portal/src/__tests__/checkout-portal.test.js',
'apps/checkout-portal/src/__tests__/checkout-xss.test.js',
- 'apps/checkout-portal/src/__tests__/issue-64-hardening.test.js',
'apps/coin-wallet-store/src/__tests__/coin-wallet-store.test.js',
'apps/merchant-admin/src/__tests__/merchant-admin.test.js',
'apps/payment-help/src/__tests__/payment-help.test.js',
'apps/gmail-observer/src/__tests__/gmail-observer.test.js',
+ 'apps/gmail-observer/src/__tests__/gmail-bank-alert-security.test.js',
+ 'apps/gmail-observer/src/__tests__/gmail-poller-boot-lifecycle.test.js',
'apps/sms-observer/src/__tests__/sms-observer.test.js',
'apps/android-observer-app/__tests__/android-observer.test.js',
'apps/control-center/src/__tests__/control-center.test.js',
'apps/payment-api/src/__tests__/payment-api.test.js',
- 'apps/payment-api/src/__tests__/phase1-observed-success.test.js',
- 'apps/payment-api/src/__tests__/statement-import.test.js',
'apps/payment-api/src/__tests__/auth-guard.test.js',
'apps/payment-api/src/__tests__/counter-payments.test.js',
'apps/payment-api/src/__tests__/selling-modes-and-cod.test.js',
@@ -34,14 +34,12 @@ const testFiles = [
'apps/payment-api/src/__tests__/device-binding-duplicate-session.test.js',
'apps/payment-api/src/__tests__/fraud-ban-pipeline.test.js',
'apps/payment-api/src/__tests__/sms-gmail-verification-levels.test.js',
- 'apps/payment-api/src/__tests__/verification-fallthrough.test.js',
'apps/payment-api/src/__tests__/upi-server-health-and-pc-ui.test.js',
'apps/payment-api/src/__tests__/whatsapp-berry-auth.test.js',
'apps/payment-api/src/__tests__/whitelabel-branding.test.js',
'apps/payment-api/src/__tests__/cod-parallel-timer-decoupling.test.js',
'apps/payment-api/src/__tests__/gateway-hardening-invariants.test.js',
'apps/payment-api/src/__tests__/observer-poller.test.js',
- 'apps/payment-api/src/__tests__/merchant-management.test.js',
'scripts/chaos-concurrency.test.js',
'scripts/production-infrastructure.test.js'
];