diff --git a/.github/workflows/security-hardening.yml b/.github/workflows/security-hardening.yml new file mode 100644 index 00000000..035e8752 --- /dev/null +++ b/.github/workflows/security-hardening.yml @@ -0,0 +1,34 @@ +name: Security hardening regression + +on: + pull_request: + branches: [main] + paths: + - 'backend/**' + - '.github/workflows/security-hardening.yml' + +permissions: + contents: read + +jobs: + backend-regression: + runs-on: ubuntu-latest + defaults: + run: + working-directory: backend + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - uses: actions/setup-node@v4 + with: + node-version: '22' + cache: npm + cache-dependency-path: backend/package-lock.json + - name: Install exact lockfile dependencies + run: npm ci + - name: Run complete backend test suite + run: npm test + - name: Check whitespace errors across the PR + working-directory: . + run: git diff --check origin/main...HEAD diff --git a/backend/.env.example b/backend/.env.example index 11edf7df..a22365bb 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -22,3 +22,17 @@ STRIPE_KEY= UI_STRING=Fruitful_app NODE_ID=backend-node-01 NODE_TYPE=backend + +# Comma-separated exact browser origins allowed to call this API. +CORS_ALLOWED_ORIGINS=https://www.ntari.org + +# Federation peer authentication (use random secrets of at least 32 characters). +# FEDERATION_LOCAL_NODE_ID=backend-node-01 +# FEDERATION_OUTBOUND_PEERS_JSON={"peer-node-01":"replace-with-peer-specific-32-byte-secret"} +# FEDERATION_PEERS_JSON={"remote-node-01":"replace-with-that-peer's-32-byte-secret"} + +# Shared store for distributed rate limits (required for multiple server instances). +# REDIS_URL=redis://localhost:6379 +# Set true in multi-instance deployments to fail startup if Redis is missing. +# The same Redis instance backs request counters and concurrent stream reservations. +RATE_LIMIT_REDIS_REQUIRED=false diff --git a/backend/__tests__/message_retries.test.js b/backend/__tests__/message_retries.test.js index 58a1ca07..be67bc7c 100644 --- a/backend/__tests__/message_retries.test.js +++ b/backend/__tests__/message_retries.test.js @@ -148,30 +148,35 @@ test('invalid keys are rejected before persistence or AI work', async () => { assert.equal(aiCalls, 0); }); -for (const apiCaller of [false, true]) { - test(`parallel ${apiCaller ? 'API-key' : 'JWT'} retries cannot acquire a second send while the first AI response is running`, async () => { - const auth = apiCaller ? { apiKey: process.env.API_KEY } : {}; - let started, finish; - const running = new Promise(resolve => { started = resolve; }); - const release = new Promise(resolve => { finish = resolve; }); - setClient({ chat: { completions: { create: async () => { - aiCalls++; - started(); - await release; - return (async function* () { yield { choices: [{ delta: { content: 'Ready' } }] }; })(); - } } } }); - const first = send(auth).then(response => response); - await running; - const racing = await Promise.all(Array.from({ length: 8 }, () => send(auth))); - finish(); - const done = await first; - assert.ok(racing.every(response => response.status === 409 && response.body.code === 'message_send_pending')); - assert.deepEqual((await send(auth)).body, done.body); - assert.equal(messages().length, 2); - assert.equal(aiCalls, 1); - assert.equal(emitted.length, 2); - }); -} +test('parallel JWT retries cannot acquire a second send while the first AI response is running', async () => { + let started, finish; + const running = new Promise(resolve => { started = resolve; }); + const release = new Promise(resolve => { finish = resolve; }); + setClient({ chat: { completions: { create: async () => { + aiCalls++; + started(); + await release; + return (async function* () { yield { choices: [{ delta: { content: 'Ready' } }] }; })(); + } } } }); + const first = send().then(response => response); + await running; + const racing = await Promise.all(Array.from({ length: 8 }, () => send())); + finish(); + const done = await first; + assert.ok(racing.every(response => response.status === 409 && response.body.code === 'message_send_pending')); + assert.deepEqual((await send()).body, done.body); + assert.equal(messages().length, 2); + assert.equal(aiCalls, 1); + assert.equal(emitted.length, 2); +}); + +test('shared API-key-only callers are rejected before message or AI work', async () => { + const response = await send({ apiKey: process.env.API_KEY }); + assert.equal(response.status, 401); + assert.equal(response.body.error, 'Account bearer token required'); + assert.equal(messages().length, 0); + assert.equal(aiCalls, 0); +}); test('completed receipts survive reloading the request handler', async () => { const first = await send(); @@ -203,59 +208,6 @@ test('changing sender label, recipient, type or attachment conflicts, without re assert.equal(aiCalls, 1); }); -test('API-key callers replay within their credential scope and do not share JWT receipts', async () => { - const first = await send({ apiKey: process.env.API_KEY }); - assert.deepEqual((await send({ apiKey: process.env.API_KEY })).body, first.body); - const user = await send(); - assert.equal(user.status, 201); - assert.notEqual(user.body.message.id, first.body.message.id); - assert.equal(aiCalls, 2); -}); - -test('API-key receipts use the stable scrypt scope instead of a fast credential hash', async () => { - const first = await send({ apiKey: process.env.API_KEY }); - assert.equal(first.status, 201); - const [receipt] = db.tables.get('MessageSends').values(); - // Independent Python hashlib.scrypt fixture: password=test-only-api-key, - // salt=SHA256(JSON([message-send-api-key-v2, conversation-1, send-1])).hex, - // N=16384, r=8, p=5, dklen=32. Locks the durable replay namespace. - assert.equal(receipt.id, 'e54be044e9e970d309de6ebe2b3763f1cbb49c546d04c7379102ba52709867e6'); - assert.ok(!JSON.stringify(receipt).includes(process.env.API_KEY)); - delete require.cache[require.resolve('../services/retrySafeMessage')]; - delete require.cache[require.resolve('../controllers/communication_controller')]; - delete require.cache[require.resolve('../routes/communicationRoutes')]; - app = makeApp(); - assert.deepEqual((await send({ apiKey: process.env.API_KEY })).body, first.body); - assert.equal(aiCalls, 1); -}); - -test('API-key rotation, conversation and send keys have separate retry scopes', async () => { - const first = await send({ apiKey: process.env.API_KEY }); - const otherConversation = await send({ apiKey: process.env.API_KEY, conversation: 'conversation-2' }); - const otherSend = await send({ apiKey: process.env.API_KEY, key: 'send-2' }); - process.env.API_KEY = 'rotated-test-api-key'; - const rotated = await send({ apiKey: process.env.API_KEY }); - for (const response of [first, otherConversation, otherSend, rotated]) assert.equal(response.status, 201); - assert.equal(new Set([first, otherConversation, otherSend, rotated].map(response => response.body.message.id)).size, 4); - assert.deepEqual((await send({ apiKey: process.env.API_KEY })).body, rotated.body); - assert.equal(aiCalls, 4); -}); - -test('WIX API-key retries work without a JWT secret', async () => { - const priorWixKey = process.env.WIX_API_KEY; - delete process.env.API_KEY; - delete process.env.JWT_SECRET; - process.env.WIX_API_KEY = 'test-only-wix-api-key'; - try { - const first = await send({ apiKey: process.env.WIX_API_KEY }); - assert.equal(first.status, 201); - assert.deepEqual((await send({ apiKey: process.env.WIX_API_KEY })).body, first.body); - assert.equal(aiCalls, 1); - } finally { - if (priorWixKey === undefined) delete process.env.WIX_API_KEY; else process.env.WIX_API_KEY = priorWixKey; - } -}); - test('an invalid API-key header cannot alter a verified JWT retry scope', async () => { const first = await send(); const retry = await request(app).post('/messages/conversation-1') @@ -268,11 +220,11 @@ test('an invalid API-key header cannot alter a verified JWT retry scope', async }); test('authentication failures and JWTs without a stable user ID create no receipts', async () => { - assert.equal((await request(app).post('/messages/conversation-1').set('Idempotency-Key', 'key').send(payload)).status, 403); + assert.equal((await request(app).post('/messages/conversation-1').set('Idempotency-Key', 'key').send(payload)).status, 401); const noIdentity = await request(app).post('/messages/conversation-1') .set('Authorization', `Bearer ${jwt.sign({ role: 'member' }, process.env.JWT_SECRET)}`) .set('Idempotency-Key', 'key').send(payload); - assert.equal(noIdentity.status, 400); + assert.equal(noIdentity.status, 401); assert.equal(db.tables.size, 0); }); diff --git a/backend/__tests__/routes/admin.test.js b/backend/__tests__/routes/admin.test.js index 47fd5997..f19f40b9 100644 --- a/backend/__tests__/routes/admin.test.js +++ b/backend/__tests__/routes/admin.test.js @@ -5,20 +5,21 @@ jest.mock('../../lib/dynamodbClient', () => ({ scan: jest.fn() })); const docClient = require('../../lib/dynamodbClient'); const adminRouter = require('../../routes/admin'); const app = express(); +app.use((req, _res, next) => { req.user = { userId: 'test-admin', role: 'admin' }; next(); }); app.use('/', adminRouter); describe.each([ - { route: 'users', table: 'Users', item: { id: 'u1', username: 'Alice' } }, - { route: 'keys', table: 'Keys', item: { id: 'k1', key: 'abc' } }, -])('admin GET /$route', ({ route, table, item }) => { + { route: 'users', table: 'Users', item: { id: 'u1', username: 'Alice', email: 'alice@example.test', role: 'consumer' }, expected: { id: 'u1', username: 'Alice', email: 'alice@example.test', role: 'consumer' } }, + { route: 'keys', table: 'Keys', item: { id: 'k1', userId: 'u1', key: 'secret', usageCount: 2 }, expected: { id: 'k1', userId: 'u1', usageCount: 2 } }, +])('admin GET /$route', ({ route, table, item, expected }) => { beforeEach(() => docClient.scan.mockReset()); - test('returns stored records from the expected table', async () => { + test('returns sanitized records from the expected table', async () => { docClient.scan.mockReturnValue({ promise: async () => ({ Items: [item] }) }); const response = await request(app).get('/' + route); expect(response.status).toBe(200); - expect(response.body).toEqual([item]); - expect(docClient.scan).toHaveBeenCalledWith({ TableName: table }); + expect(response.body).toEqual([expected]); + expect(docClient.scan).toHaveBeenCalledWith(expect.objectContaining({ TableName: table, ProjectionExpression: expect.any(String) })); }); test('returns 500 on a storage error', async () => { diff --git a/backend/docs/security-hardening.md b/backend/docs/security-hardening.md new file mode 100644 index 00000000..9e6669ec --- /dev/null +++ b/backend/docs/security-hardening.md @@ -0,0 +1,63 @@ +# Agrinet account, federation and request-limit hardening + +This branch closes the account-overwrite and unprotected federation paths and prepares the chat PRs to use the same distributed limit store. + +## Account and user-response boundaries + +- POST /users is administrative provisioning only. It requires a verified JWT principal whose role is admin; the browser/API-key path does not create req.user and cannot pass this check. +- IDs are generated by the server. A supplied id is rejected. DynamoDB writes use attribute_not_exists(id) so a collision cannot replace a prior account. +- Public /users responses are a narrow directory DTO. /api/admin/users requires an administrator and returns an administrative DTO, not a raw DynamoDB row. +- Login responses use the authenticated-account DTO. Password hashes, verification codes and exact coordinates are never included. +- Public signup remains /api/auth/register and uses a conditional create. + +## Federation peer authentication + +Federation endpoints are mounted before the general browser/API-key middleware and require HMAC peer authentication themselves. They do not accept the shared browser API key as peer identity. + +Each peer pair needs a distinct random secret of at least 32 characters: + +- FEDERATION_LOCAL_NODE_ID (or existing NODE_ID) is this node's stable identity. +- FEDERATION_OUTBOUND_PEERS_JSON maps each remote NodeRegistry.nodeId to the secret this node uses when contacting that peer. +- FEDERATION_PEERS_JSON maps authorized inbound sender node IDs to their matching shared secrets. + +For A to synchronize with B, A's outbound entry for B must match B's inbound entry for A. The reverse direction can use a different secret. Never put these values in browser code or commit a real .env file. Keep node clocks synchronized; signed requests and responses have a five-minute clock-skew window. + +Requests sign the timestamp, HTTP method, path and SHA-256 digest of the exact request body. Export responses are also signed and checked against the node ID in the local NodeRegistry record. Production peers must use HTTPS; HTTP is accepted only for loopback development. + +Federation user imports accept an explicit user-field allowlist, reject existing IDs atomically, strip passwords, contact fields, roles and verification state, and record sourceNodeId from the authenticated peer plus remoteUserId. Claimed origin identifiers are retained as provenance metadata from the signed peer payload; they never determine the authenticated sender. + +Exports use explicit field allowlists. The location feature's exact owner coordinates remain in its separate local Locations table and are not part of the user export. + +## Distributed request and connection limits + +express-rate-limit is used for API, user writes, location, chat, stream and federation traffic. Chat routes require an account JWT (`chatAuth`); the shared browser/API key is explicitly rejected for private chat. Authenticated requests are keyed by req.user.userId; authenticated peers by req.federationPeer.nodeId; application-key traffic without a user identity falls back to source IP. + +When REDIS_URL is configured, request counters and concurrent-stream reservations use Redis and are shared across instances. In a multi-instance deployment, set RATE_LIMIT_REDIS_REQUIRED=true so the service refuses to start without the shared store. Without Redis, counters and stream slots are process-local and suitable only for a documented single-instance deployment. + +backend/middleware/chatRateLimit.js is intended to be mounted after chatAuth. backend/middleware/streamConcurrency.js provides a five-stream-per-account reservation. The authenticated-stream PR should wire the stream route in this order: + +1. chatAuth +2. chatRateLimit(60) +3. createConcurrentStreamLimit({ maximum: 5 }) +4. conversationStreams.open + +The concurrency middleware releases a reservation on response close/finish and Redis reservations expire to recover from process crashes. + +## Temporary fail-closed behavior and merge order + +Until the owner-checked chat changes are deployed: + +- /events and /stream/:conversationId return 410 instead of exposing the legacy unauthenticated feed. +- /uploads returns 410 instead of serving files to any caller holding the shared browser key. + +After #191 is rebased and approved, restore attachment serving only behind chatAuth, the shared chat limiter, and attachmentOwner. After #192 is rebased and approved, replace the temporary stream 410 with the ordered authenticated handler above. Do not restore the old handlers before those protections are present. + +No upload-creation endpoint exists in the current main tree; when one is introduced, place upload throttling on that actual write operation, not only on static download paths. + +## CORS configuration + +Set CORS_ALLOWED_ORIGINS to the exact deployed chat UI/application origins (comma-separated). Do not use * with credentials. The default remains the existing NTARI web origin. Authorization and the required integration/federation headers are explicitly allowed for preflight. + +## Verification + +The branch adds tests for user/admin response allowlists, non-admin denial, caller-supplied ID rejection, conditional writes, imported-user sanitization and provenance, peer request/response signatures, request limits, and concurrent-stream release. CI should run npm ci, npm test, CodeQL and the location-storage regression workflow on the PR. No local test run is claimed here. diff --git a/backend/federation/__tests__/federationRoutes.test.js b/backend/federation/__tests__/federationRoutes.test.js new file mode 100644 index 00000000..80ac72d5 --- /dev/null +++ b/backend/federation/__tests__/federationRoutes.test.js @@ -0,0 +1,98 @@ +const express = require('express'); +const request = require('supertest'); + +jest.mock('../../lib/dynamodbClient', () => ({ + scan: jest.fn(), + put: jest.fn() +})); + +const docClient = require('../../lib/dynamodbClient'); +const federationRoutes = require('../federationRoutes'); +const { createPeerHeaders, verifyPeerResponse } = require('../../middleware/federationPeerAuth'); + +describe('federation route security', () => { + const oldEnv = { + peers: process.env.FEDERATION_PEERS_JSON, + node: process.env.NODE_ID, + localNode: process.env.FEDERATION_LOCAL_NODE_ID + }; + const secret = '0123456789abcdef0123456789abcdef'; + let app; + + beforeEach(() => { + process.env.FEDERATION_PEERS_JSON = JSON.stringify({ 'peer-1': secret }); + process.env.NODE_ID = 'local-node'; + process.env.FEDERATION_LOCAL_NODE_ID = 'local-node'; + app = express(); + app.use(express.json({ verify(req, _res, buffer) { req.rawBody = Buffer.from(buffer); } })); + app.use('/federation', federationRoutes); + docClient.scan.mockReset(); + docClient.put.mockReset(); + }); + + afterAll(() => { + if (oldEnv.peers === undefined) delete process.env.FEDERATION_PEERS_JSON; + else process.env.FEDERATION_PEERS_JSON = oldEnv.peers; + if (oldEnv.node === undefined) delete process.env.NODE_ID; + else process.env.NODE_ID = oldEnv.node; + if (oldEnv.localNode === undefined) delete process.env.FEDERATION_LOCAL_NODE_ID; + else process.env.FEDERATION_LOCAL_NODE_ID = oldEnv.localNode; + }); + + test('does not export unless the caller proves a configured peer identity', async () => { + const response = await request(app).get('/federation/export'); + expect(response.status).toBe(401); + expect(docClient.scan).not.toHaveBeenCalled(); + }); + + test('exports only allowlisted user, listing and transaction fields and signs response', async () => { + docClient.scan + .mockReturnValueOnce({ promise: async () => ({ Items: [{ id: 'l1', title: 'Apples', password: 'secret', internal: 'x' }] }) }) + .mockReturnValueOnce({ promise: async () => ({ Items: [{ id: 't1', status: 'pending', cardToken: 'secret' }] }) }) + .mockReturnValueOnce({ promise: async () => ({ Items: [{ + id: 'u1', username: 'alice', email: 'a@example.test', phone: '555-0100', + password: 'hash', verificationCode: 'secret', role: 'admin', verified: true, + coordinates: { lat: 1, long: 2 }, reputationScore: 3 + }] }) }); + + const headers = createPeerHeaders({ + nodeId: 'peer-1', secret, method: 'GET', path: '/federation/export' + }); + const response = await request(app).get('/federation/export').set(headers); + expect(response.status).toBe(200); + expect(response.body.users).toEqual([{ + id: 'u1', username: 'alice', reputationScore: 3, + originNodeId: 'local-node', originUserId: 'u1' + }]); + expect(response.body.listings).toEqual([{ id: 'l1', title: 'Apples' }]); + expect(response.body.transactions).toEqual([{ id: 't1', status: 'pending' }]); + expect(verifyPeerResponse({ + headers: response.headers, + expectedNodeId: 'local-node', + secret, + method: 'GET', + path: '/federation/export', + body: JSON.stringify(response.body) + })).toBe(true); + }); + + test('import uses the authenticated peer identity, not a body-supplied source id', async () => { + docClient.put.mockReturnValue({ promise: async () => ({}) }); + const body = JSON.stringify({ + sourceNodeId: 'forged-node', + users: [{ id: 'remote-u1', username: 'bob', role: 'admin', password: 'hash' }] + }); + const headers = createPeerHeaders({ + nodeId: 'peer-1', secret, method: 'POST', path: '/federation/import', body + }); + const response = await request(app).post('/federation/import') + .set(headers).set('Content-Type', 'application/json').send(body); + expect(response.status).toBe(200); + const written = docClient.put.mock.calls[0][0]; + expect(written.Item.sourceNodeId).toBe('peer-1'); + expect(written.Item.remoteUserId).toBe('remote-u1'); + expect(written.Item.role).toBeUndefined(); + expect(written.Item.password).toBeUndefined(); + expect(written.ConditionExpression).toBe('attribute_not_exists(#id)'); + }); +}); diff --git a/backend/federation/__tests__/federationSyncJob.test.js b/backend/federation/__tests__/federationSyncJob.test.js index 5b821fb1..57999c6c 100644 --- a/backend/federation/__tests__/federationSyncJob.test.js +++ b/backend/federation/__tests__/federationSyncJob.test.js @@ -7,46 +7,89 @@ jest.mock('../../lib/dynamodbClient', () => ({ const axios = require('axios'); const dynamodbClient = require('../../lib/dynamodbClient'); +const { createPeerResponseHeaders } = require('../../middleware/federationPeerAuth'); const runFederationSync = require('../federationSyncJob'); describe('runFederationSync', () => { + const originalEnv = { + nodeId: process.env.NODE_ID, + localNodeId: process.env.FEDERATION_LOCAL_NODE_ID, + outbound: process.env.FEDERATION_OUTBOUND_PEERS_JSON + }; + const peerSecret = '0123456789abcdef0123456789abcdef'; + beforeEach(() => { jest.clearAllMocks(); + process.env.NODE_ID = 'local-node'; + process.env.FEDERATION_LOCAL_NODE_ID = 'local-node'; + process.env.FEDERATION_OUTBOUND_PEERS_JSON = JSON.stringify({ 'peer-1': peerSecret }); + }); + + afterAll(() => { + if (originalEnv.nodeId === undefined) delete process.env.NODE_ID; + else process.env.NODE_ID = originalEnv.nodeId; + if (originalEnv.localNodeId === undefined) delete process.env.FEDERATION_LOCAL_NODE_ID; + else process.env.FEDERATION_LOCAL_NODE_ID = originalEnv.localNodeId; + if (originalEnv.outbound === undefined) delete process.env.FEDERATION_OUTBOUND_PEERS_JSON; + else process.env.FEDERATION_OUTBOUND_PEERS_JSON = originalEnv.outbound; }); - it('updates lastSyncAt on successful sync', async () => { + it('verifies peer response signatures before importing and updates lastSyncAt', async () => { dynamodbClient.scan.mockReturnValue({ - promise: () => Promise.resolve({ Items: [{ url: 'http://node1.com' }] }) + promise: async () => ({ Items: [{ nodeId: 'peer-1', url: 'https://node1.example' }] }) }); - dynamodbClient.update.mockReturnValue({ - promise: () => Promise.resolve() + dynamodbClient.update.mockReturnValue({ promise: async () => ({}) }); + const data = { users: [] }; + const body = JSON.stringify(data); + axios.get.mockResolvedValue({ + data, + headers: createPeerResponseHeaders({ + nodeId: 'peer-1', secret: peerSecret, method: 'GET', path: '/federation/export', body + }) }); - axios.get.mockResolvedValue({ data: { foo: 'bar' } }); - axios.post.mockResolvedValue({}); await runFederationSync(); + expect(axios.get).toHaveBeenCalledWith('https://node1.example/federation/export', expect.objectContaining({ + headers: expect.objectContaining({ + 'x-federation-node-id': 'local-node', + 'x-federation-signature': expect.any(String) + }), + timeout: 15000 + })); expect(dynamodbClient.update).toHaveBeenCalledTimes(1); const params = dynamodbClient.update.mock.calls[0][0]; expect(params).toMatchObject({ - Key: { url: 'http://node1.com' }, + Key: { url: 'https://node1.example' }, UpdateExpression: 'set lastSyncAt = :time', - ExpressionAttributeValues: { - ':time': expect.any(String) - } + ExpressionAttributeValues: { ':time': expect.any(String) } }); const timestamp = params.ExpressionAttributeValues[':time']; expect(new Date(timestamp).toISOString()).toBe(timestamp); }); - it('does not update lastSyncAt on failed sync', async () => { + it('does not import or update metadata when the peer response signature is invalid', async () => { dynamodbClient.scan.mockReturnValue({ - promise: () => Promise.resolve({ Items: [{ url: 'http://node1.com' }] }) + promise: async () => ({ Items: [{ nodeId: 'peer-1', url: 'https://node1.example' }] }) + }); + axios.get.mockResolvedValue({ + data: { users: [] }, + headers: { + 'x-federation-response-node-id': 'attacker', + 'x-federation-response-timestamp': String(Math.floor(Date.now() / 1000)), + 'x-federation-response-signature': '0'.repeat(64) + } }); - axios.get.mockRejectedValue(new Error('fail')); - await runFederationSync(); + expect(dynamodbClient.update).not.toHaveBeenCalled(); + }); + it('does not send credentials to non-HTTPS production peers', async () => { + dynamodbClient.scan.mockReturnValue({ + promise: async () => ({ Items: [{ nodeId: 'peer-1', url: 'http://node1.example' }] }) + }); + await runFederationSync(); + expect(axios.get).not.toHaveBeenCalled(); expect(dynamodbClient.update).not.toHaveBeenCalled(); }); }); diff --git a/backend/federation/federationRoutes.js b/backend/federation/federationRoutes.js index 48506004..2afe333e 100644 --- a/backend/federation/federationRoutes.js +++ b/backend/federation/federationRoutes.js @@ -5,31 +5,73 @@ const { LISTING_TABLE_NAME } = require('../models/listing'); const { TRANSACTION_TABLE_NAME } = require('../models/transaction'); const { USER_TABLE_NAME } = require('../models/user'); const { importFederatedData } = require('../sync/importHelper'); +const { requireFederationPeer, createPeerResponseHeaders } = require('../middleware/federationPeerAuth'); +const { federationAttemptRateLimit, federationRateLimit } = require('../middleware/rateLimit'); -// Export route for peer sync -router.get('/export', async (req, res) => { +const pick = (item, fields) => Object.fromEntries( + fields.filter(field => item[field] !== undefined).map(field => [field, item[field]]) +); + +// Browser API keys and ordinary member tokens are never federation credentials. +router.use(federationAttemptRateLimit, requireFederationPeer, federationRateLimit); + +router.get('/export', async (_req, res) => { try { - const listingsRes = await docClient.scan({ TableName: LISTING_TABLE_NAME }).promise(); - const transactionsRes = await docClient.scan({ TableName: TRANSACTION_TABLE_NAME }).promise(); - const usersRes = await docClient.scan({ TableName: USER_TABLE_NAME }).promise(); - res.json({ - listings: listingsRes.Items || [], - transactions: transactionsRes.Items || [], - users: usersRes.Items || [] - }); - } catch (err) { - res.status(500).json({ error: 'Export failed' }); + const [listingsRes, transactionsRes, usersRes] = await Promise.all([ + docClient.scan({ TableName: LISTING_TABLE_NAME }).promise(), + docClient.scan({ TableName: TRANSACTION_TABLE_NAME }).promise(), + docClient.scan({ + TableName: USER_TABLE_NAME, + ProjectionExpression: '#id, username, reputationScore, reputationWeight, originNodeId, originUserId', + ExpressionAttributeNames: { '#id': 'id' } + }).promise() + ]); + + const configuredNodeId = process.env.FEDERATION_LOCAL_NODE_ID || process.env.NODE_ID; + const users = (usersRes.Items || []).map(user => ({ + ...pick(user, ['id', 'username', 'reputationScore', 'reputationWeight']), + originNodeId: typeof user.originNodeId === 'string' ? user.originNodeId : configuredNodeId, + originUserId: typeof user.originUserId === 'string' ? user.originUserId : user.id + })); + + // Export only fields required for federation; never serialize full DynamoDB rows. + const listings = (listingsRes.Items || []).map(item => pick(item, [ + 'id', 'userId', 'type', 'title', 'description', 'price', 'location', 'createdAt' + ])); + const transactions = (transactionsRes.Items || []).map(item => pick(item, [ + 'id', 'contractId', 'consumerId', 'producerId', 'rating', 'status' + ])); + + const payload = JSON.stringify({ listings, transactions, users }); + const peers = JSON.parse(process.env.FEDERATION_PEERS_JSON || '{}'); + const secret = peers[_req.federationPeer.nodeId]; + const responseNodeId = process.env.FEDERATION_LOCAL_NODE_ID || process.env.NODE_ID; + if (!responseNodeId || typeof secret !== 'string' || secret.length < 32) { + return res.status(503).json({ error: 'Federation response signing is not configured' }); + } + res.set(createPeerResponseHeaders({ + nodeId: responseNodeId, secret, method: 'GET', path: '/federation/export', body: payload + })); + return res.type('application/json').send(payload); + } catch { + return res.status(500).json({ error: 'Export failed' }); } }); -// Import route for federation sync router.post('/import', async (req, res) => { try { - await importFederatedData(req.body); - - res.json({ message: 'Import successful' }); - } catch (err) { - res.status(500).json({ error: 'Import failed' }); + await importFederatedData(req.body, { sourceNodeId: req.federationPeer.nodeId }); + return res.json({ message: 'Import successful' }); + } catch (error) { + if (error.code === 'ConditionalCheckFailedException') { + return res.status(409).json({ error: 'Import refused: user id already exists' }); + } + if (error.message === 'Federated user must have a remote user id' || + error.message === 'Federated user must have a username' || + error.message === 'Federation payload must be an object') { + return res.status(400).json({ error: error.message }); + } + return res.status(500).json({ error: 'Import failed' }); } }); diff --git a/backend/federation/federationSyncJob.js b/backend/federation/federationSyncJob.js index 1ffca6c5..2863cf83 100644 --- a/backend/federation/federationSyncJob.js +++ b/backend/federation/federationSyncJob.js @@ -1,50 +1,78 @@ -const axios = require("axios"); -const dynamodbClient = require("../lib/dynamodbClient"); -const { NODE_REGISTRY_TABLE_NAME } = require("./models/nodeRegistry"); - -const BACKEND_URL = process.env.BACKEND_URL || "http://localhost:5000"; +const axios = require('axios'); +const dynamodbClient = require('../lib/dynamodbClient'); +const { NODE_REGISTRY_TABLE_NAME } = require('./models/nodeRegistry'); +const { createPeerHeaders, verifyPeerResponse } = require('../middleware/federationPeerAuth'); +const { importFederatedData } = require('../sync/importHelper'); const runFederationSync = async () => { - // Scan all nodes from DynamoDB table - const { Items: nodes } = await dynamodbClient + const localNodeId = process.env.FEDERATION_LOCAL_NODE_ID || process.env.NODE_ID; + if (!localNodeId) { + console.error('Federation sync skipped: FEDERATION_LOCAL_NODE_ID or NODE_ID is not configured'); + return; + } + let outboundSecrets; + try { + outboundSecrets = JSON.parse(process.env.FEDERATION_OUTBOUND_PEERS_JSON || '{}'); + } catch { + console.error('Federation sync skipped: FEDERATION_OUTBOUND_PEERS_JSON is invalid'); + return; + } + + const { Items: nodes = [] } = await dynamodbClient .scan({ TableName: NODE_REGISTRY_TABLE_NAME }) .promise(); -/* -const { getAllNodes, saveNode } = require("./models/nodeRegistry"); -const runFederationSync = async () => { - const nodes = await getAllNodes(); -*/ for (const node of nodes) { try { - // Fetch federation/export data from the node - const { data } = await axios.get(`${node.url}/federation/export`); - - // Update lastSyncAt in DynamoDB for this node - await axios.post(`${BACKEND_URL}/import`, data); - - await dynamodbClient - .update({ - TableName: NODE_REGISTRY_TABLE_NAME, - Key: { url: node.url }, - UpdateExpression: "set lastSyncAt = :time", - ExpressionAttributeValues: { - ":time": new Date().toISOString(), - }, - }) - .promise(); - - //node.lastSyncAt = new Date().toISOString(); - //await saveNode(node); - - console.log(`✅ Synced with ${node.url}`); - } catch (err) { - console.error(`❌ Failed to sync with ${node.url}`); + // A peer must be present in the operator's authenticated peer registry. + const peerSecret = node.nodeId ? outboundSecrets[node.nodeId] : null; + if (!node.nodeId || typeof peerSecret !== 'string' || peerSecret.length < 32) { + console.error('Federation sync skipped for an unconfigured peer'); + continue; + } + + const exportPath = '/federation/export'; + const headers = createPeerHeaders({ + nodeId: localNodeId, secret: peerSecret, method: 'GET', path: exportPath + }); + const peerUrl = new URL(node.url); + if (peerUrl.protocol !== 'https:' && !['localhost', '127.0.0.1', '[::1]'].includes(peerUrl.hostname)) { + throw new Error('Federation peers must use HTTPS'); + } + const response = await axios.get(new URL(exportPath, peerUrl).toString(), { + headers, timeout: 15000 + }); + const responseBody = JSON.stringify(response.data); + if (!verifyPeerResponse({ + headers: response.headers, + expectedNodeId: node.nodeId, + secret: peerSecret, + method: 'GET', + path: exportPath, + body: responseBody + })) { + throw new Error('Federation peer response signature is invalid'); + } + const data = response.data; + + // Source identity comes from the locally managed NodeRegistry record whose + // peer credential is configured above, never from fields in the response body. + await importFederatedData(data, { sourceNodeId: node.nodeId }); + + await dynamodbClient.update({ + TableName: NODE_REGISTRY_TABLE_NAME, + Key: { url: node.url }, + UpdateExpression: 'set lastSyncAt = :time', + ExpressionAttributeValues: { ':time': new Date().toISOString() } + }).promise(); + console.log('Federation sync completed with a registered peer'); + } catch (error) { + console.error('Federation sync failed for a registered peer:', error.message); } } }; -// Run every 6 hours -setInterval(runFederationSync, 6 * 60 * 60 * 1000); // 6 hours +const timer = setInterval(runFederationSync, 6 * 60 * 60 * 1000); +if (typeof timer.unref === 'function') timer.unref(); module.exports = runFederationSync; diff --git a/backend/middleware/__tests__/chatAuth.test.js b/backend/middleware/__tests__/chatAuth.test.js new file mode 100644 index 00000000..8b3472b9 --- /dev/null +++ b/backend/middleware/__tests__/chatAuth.test.js @@ -0,0 +1,37 @@ +const express = require('express'); +const request = require('supertest'); +const jwt = require('jsonwebtoken'); +const chatAuth = require('../chatAuth'); +const { rateLimit, ipKeyGenerator } = require('express-rate-limit'); + +describe('private chat authentication', () => { + const prior = process.env.JWT_SECRET; + beforeAll(() => { process.env.JWT_SECRET = 'chat-auth-test-secret'; }); + afterAll(() => { + if (prior === undefined) delete process.env.JWT_SECRET; + else process.env.JWT_SECRET = prior; + }); + + function app() { + const server = express(); + server.get('/private', rateLimit({ windowMs: 60000, limit: 300, keyGenerator: req => ipKeyGenerator(req.ip) }), chatAuth, rateLimit({ windowMs: 60000, limit: 60, keyGenerator: req => req.user?.userId || ipKeyGenerator(req.ip) }), (req, res) => res.json({ userId: req.user.userId })); + return server; + } + + test('rejects shared API-key-only requests', async () => { + const response = await request(app()).get('/private').set('x-api-key', 'browser-key'); + expect(response.status).toBe(401); + }); + + test('rejects missing and invalid bearer tokens', async () => { + expect((await request(app()).get('/private')).status).toBe(401); + expect((await request(app()).get('/private').set('Authorization', 'Bearer invalid')).status).toBe(401); + }); + + test('accepts a signed account token and sets its principal', async () => { + const token = jwt.sign({ userId: 'member-1', role: 'consumer' }, process.env.JWT_SECRET, { expiresIn: '5m' }); + const response = await request(app()).get('/private').set('Authorization', 'Bearer ' + token); + expect(response.status).toBe(200); + expect(response.body).toEqual({ userId: 'member-1' }); + }); +}); diff --git a/backend/middleware/__tests__/federationPeerAuth.test.js b/backend/middleware/__tests__/federationPeerAuth.test.js new file mode 100644 index 00000000..6ac38907 --- /dev/null +++ b/backend/middleware/__tests__/federationPeerAuth.test.js @@ -0,0 +1,49 @@ +const express = require('express'); +const request = require('supertest'); +const { requireFederationPeer, createPeerHeaders } = require('../federationPeerAuth'); + +describe('federation peer authentication', () => { + const saved = process.env.FEDERATION_PEERS_JSON; + let app; + beforeEach(() => { + process.env.FEDERATION_PEERS_JSON = JSON.stringify({ 'peer-1': '0123456789abcdef0123456789abcdef' }); + app = express(); + app.use(express.json({ verify(req, _res, buffer) { req.rawBody = Buffer.from(buffer); } })); + app.post('/federation/import', requireFederationPeer, (req, res) => { + res.json({ authenticatedPeer: req.federationPeer.nodeId }); + }); + }); + afterAll(() => { + if (saved === undefined) delete process.env.FEDERATION_PEERS_JSON; + else process.env.FEDERATION_PEERS_JSON = saved; + }); + + test('accepts a valid signature and sets peer identity from the credential', async () => { + const body = JSON.stringify({ sourceNodeId: 'forged-body-identity', users: [] }); + const headers = createPeerHeaders({ + nodeId: 'peer-1', secret: '0123456789abcdef0123456789abcdef', + method: 'POST', path: '/federation/import', body + }); + const response = await request(app).post('/federation/import') + .set(headers).set('Content-Type', 'application/json').send(body); + expect(response.status).toBe(200); + expect(response.body.authenticatedPeer).toBe('peer-1'); + }); + + test('fails closed when peer credentials are missing', async () => { + const response = await request(app).post('/federation/import').send({ users: [] }); + expect(response.status).toBe(401); + }); + + test('rejects an invalid signature', async () => { + const body = JSON.stringify({ users: [] }); + const headers = createPeerHeaders({ + nodeId: 'peer-1', secret: '0123456789abcdef0123456789abcdef', + method: 'POST', path: '/federation/import', body + }); + headers['x-federation-signature'] = '0'.repeat(64); + const response = await request(app).post('/federation/import') + .set(headers).set('Content-Type', 'application/json').send(body); + expect(response.status).toBe(401); + }); +}); diff --git a/backend/middleware/__tests__/rateLimit.test.js b/backend/middleware/__tests__/rateLimit.test.js new file mode 100644 index 00000000..55155b29 --- /dev/null +++ b/backend/middleware/__tests__/rateLimit.test.js @@ -0,0 +1,20 @@ +const express = require('express'); +const request = require('supertest'); +const { createRequestLimiter } = require('../rateLimit'); + +describe('request rate limiting', () => { + test('limits by authenticated account identity', async () => { + const app = express(); + app.use((req, _res, next) => { req.user = { userId: 'account-a' }; next(); }); + app.get('/limited', createRequestLimiter({ prefix: 'test-account', limit: 1 }), (_req, res) => res.sendStatus(204)); + expect((await request(app).get('/limited')).status).toBe(204); + expect((await request(app).get('/limited')).status).toBe(429); + }); + + test('uses IP identity for shared-key callers without req.user', async () => { + const app = express(); + app.get('/limited', createRequestLimiter({ prefix: 'test-ip', limit: 1 }), (_req, res) => res.sendStatus(204)); + expect((await request(app).get('/limited')).status).toBe(204); + expect((await request(app).get('/limited')).status).toBe(429); + }); +}); diff --git a/backend/middleware/__tests__/streamConcurrency.test.js b/backend/middleware/__tests__/streamConcurrency.test.js new file mode 100644 index 00000000..59fbf203 --- /dev/null +++ b/backend/middleware/__tests__/streamConcurrency.test.js @@ -0,0 +1,43 @@ +const { EventEmitter } = require('node:events'); +const { createConcurrentStreamLimit } = require('../streamConcurrency'); + +function response() { + const res = new EventEmitter(); + res.statusCode = 200; + res.status = code => { res.statusCode = code; return res; }; + res.json = body => { res.body = body; return res; }; + return res; +} + +describe('concurrent stream limits', () => { + test('caps open streams per account and releases a slot on disconnect', async () => { + const middleware = createConcurrentStreamLimit({ maximum: 1 }); + const req = { user: { userId: 'stream-user', exp: Math.ceil(Date.now() / 1000) + 60 } }; + const first = response(); + let firstNext = false; + await middleware(req, first, () => { firstNext = true; }); + expect(firstNext).toBe(true); + + const second = response(); + let secondNext = false; + await middleware(req, second, () => { secondNext = true; }); + expect(secondNext).toBe(false); + expect(second.statusCode).toBe(429); + + first.emit('close'); + await new Promise(resolve => setImmediate(resolve)); + + const third = response(); + let thirdNext = false; + await middleware(req, third, () => { thirdNext = true; }); + expect(thirdNext).toBe(true); + third.emit('close'); + }); + + test('fails closed without a valid account and unexpired token', async () => { + const middleware = createConcurrentStreamLimit(); + const res = response(); + await middleware({ user: { userId: 'missing-exp' } }, res, () => { throw new Error('next must not run'); }); + expect(res.statusCode).toBe(401); + }); +}); diff --git a/backend/middleware/chatAuth.js b/backend/middleware/chatAuth.js new file mode 100644 index 00000000..a6ad99c2 --- /dev/null +++ b/backend/middleware/chatAuth.js @@ -0,0 +1,20 @@ +const jwt = require('jsonwebtoken'); + +// A shared integration key does not identify an individual chat participant. +module.exports = (req, res, next) => { + const header = req.headers.authorization; + if (!header || !header.startsWith('Bearer ')) { + return res.status(401).json({ error: 'Account bearer token required' }); + } + try { + const payload = jwt.verify(header.slice(7), process.env.JWT_SECRET, { algorithms: ['HS256'] }); + if (typeof payload.userId !== 'string' || !payload.userId.trim()) { + throw new Error('Missing account identity'); + } + req.user = payload; + res.set('Cache-Control', 'private, no-store'); + return next(); + } catch { + return res.status(401).json({ error: 'Unauthorized: Invalid Token' }); + } +}; diff --git a/backend/middleware/chatRateLimit.js b/backend/middleware/chatRateLimit.js new file mode 100644 index 00000000..300c227e --- /dev/null +++ b/backend/middleware/chatRateLimit.js @@ -0,0 +1,11 @@ +const { createRequestLimiter } = require('./rateLimit'); + +// Mount after chatAuth so req.user.userId is a verified account identity. +module.exports = function chatRateLimit(limit = 60) { + return createRequestLimiter({ + prefix: 'agrinet:chat', + windowMs: 60 * 1000, + limit, + message: 'Too many chat requests, please try again later' + }); +}; diff --git a/backend/middleware/federationPeerAuth.js b/backend/middleware/federationPeerAuth.js new file mode 100644 index 00000000..4890ba53 --- /dev/null +++ b/backend/middleware/federationPeerAuth.js @@ -0,0 +1,117 @@ +const crypto = require('node:crypto'); + +const MAX_CLOCK_SKEW_SECONDS = 300; + +function loadPeers() { + let peers; + try { + peers = JSON.parse(process.env.FEDERATION_PEERS_JSON || '{}'); + } catch { + throw new Error('FEDERATION_PEERS_JSON must be valid JSON'); + } + if (!peers || Array.isArray(peers) || typeof peers !== 'object') { + throw new Error('FEDERATION_PEERS_JSON must be an object'); + } + return peers; +} + +function requestDigest(req) { + const body = Buffer.isBuffer(req.rawBody) ? req.rawBody : Buffer.alloc(0); + return crypto.createHash('sha256').update(body).digest('hex'); +} + +function signaturePayload(req, timestamp) { + const path = (req.originalUrl || req.url).split('?')[0]; + return [timestamp, req.method.toUpperCase(), path, requestDigest(req)].join('.'); +} + +function safeEqualHex(actual, expected) { + if (typeof actual !== 'string' || !/^[a-f0-9]{64}$/i.test(actual)) return false; + const actualBuffer = Buffer.from(actual, 'hex'); + const expectedBuffer = Buffer.from(expected, 'hex'); + return actualBuffer.length === expectedBuffer.length && + crypto.timingSafeEqual(actualBuffer, expectedBuffer); +} + +function createPeerHeaders({ nodeId, secret, method, path, body = '' }) { + if (!nodeId || !secret) throw new Error('Federation peer credentials are required'); + const timestamp = String(Math.floor(Date.now() / 1000)); + const digest = crypto.createHash('sha256').update(body).digest('hex'); + const payload = [timestamp, method.toUpperCase(), path.split('?')[0], digest].join('.'); + const signature = crypto.createHmac('sha256', secret).update(payload).digest('hex'); + return { + 'x-federation-node-id': nodeId, + 'x-federation-timestamp': timestamp, + 'x-federation-signature': signature + }; +} + +function requireFederationPeer(req, res, next) { + try { + const nodeId = req.get('x-federation-node-id'); + const timestamp = req.get('x-federation-timestamp'); + const signature = req.get('x-federation-signature'); + + if (!nodeId || !/^[A-Za-z0-9._:-]{1,128}$/.test(nodeId) || + !timestamp || !/^\d{10}$/.test(timestamp)) { + return res.status(401).json({ error: 'Authenticated federation peer required' }); + } + + const seconds = Math.floor(Date.now() / 1000); + if (Math.abs(seconds - Number(timestamp)) > MAX_CLOCK_SKEW_SECONDS) { + return res.status(401).json({ error: 'Federation request timestamp expired' }); + } + + const peers = loadPeers(); + const secret = peers[nodeId]; + if (typeof secret !== 'string' || secret.length < 32) { + return res.status(401).json({ error: 'Unknown federation peer' }); + } + + const expected = crypto.createHmac('sha256', secret) + .update(signaturePayload(req, timestamp)) + .digest('hex'); + + if (!safeEqualHex(signature, expected)) { + return res.status(401).json({ error: 'Invalid federation signature' }); + } + + // Provenance is assigned only after authenticating the peer credential. + req.federationPeer = Object.freeze({ nodeId }); + return next(); + } catch (error) { + return res.status(503).json({ error: 'Federation authentication is not configured' }); + } +} + +function createPeerResponseHeaders({ nodeId, secret, method, path, body }) { + const timestamp = String(Math.floor(Date.now() / 1000)); + const digest = crypto.createHash('sha256').update(body).digest('hex'); + const payload = [timestamp, 'RESPONSE:' + method.toUpperCase(), path.split('?')[0], digest].join('.'); + const signature = crypto.createHmac('sha256', secret).update(payload).digest('hex'); + return { + 'x-federation-response-node-id': nodeId, + 'x-federation-response-timestamp': timestamp, + 'x-federation-response-signature': signature + }; +} + +function verifyPeerResponse({ headers, expectedNodeId, secret, method, path, body }) { + const nodeId = headers['x-federation-response-node-id']; + const timestamp = headers['x-federation-response-timestamp']; + const signature = headers['x-federation-response-signature']; + const seconds = Math.floor(Date.now() / 1000); + if (nodeId !== expectedNodeId || !timestamp || !/^[0-9]{10}$/.test(timestamp) || + Math.abs(seconds - Number(timestamp)) > MAX_CLOCK_SKEW_SECONDS) return false; + const digest = crypto.createHash('sha256').update(body).digest('hex'); + const payload = [timestamp, 'RESPONSE:' + method.toUpperCase(), path.split('?')[0], digest].join('.'); + const expected = crypto.createHmac('sha256', secret).update(payload).digest('hex'); + return safeEqualHex(signature, expected); +} + +module.exports = { + requireFederationPeer, + createPeerHeaders, + createPeerResponseHeaders, + verifyPeerResponse +}; diff --git a/backend/middleware/rateLimit.js b/backend/middleware/rateLimit.js new file mode 100644 index 00000000..9fdf2315 --- /dev/null +++ b/backend/middleware/rateLimit.js @@ -0,0 +1,66 @@ +const { rateLimit, ipKeyGenerator } = require('express-rate-limit'); + +let redis = null; +if (process.env.REDIS_URL) { + const Redis = require('ioredis'); + redis = new Redis(process.env.REDIS_URL, { + maxRetriesPerRequest: 1, + enableOfflineQueue: false + }); +} else if (process.env.RATE_LIMIT_REDIS_REQUIRED === 'true') { + throw new Error('REDIS_URL is required when RATE_LIMIT_REDIS_REQUIRED=true'); +} + +class RedisRateLimitStore { + constructor(client, prefix) { + this.client = client; + this.prefix = prefix; + this.windowMs = 60000; + } + init(options) { this.windowMs = options.windowMs; } + async increment(key) { + const redisKey = this.prefix + ':' + key; + const totalHits = await this.client.incr(redisKey); + if (totalHits === 1) await this.client.pexpire(redisKey, this.windowMs); + let ttl = await this.client.pttl(redisKey); + if (ttl < 0) { + await this.client.pexpire(redisKey, this.windowMs); + ttl = this.windowMs; + } + return { totalHits, resetTime: new Date(Date.now() + ttl) }; + } + async decrement(key) { await this.client.decr(this.prefix + ':' + key); } + async resetKey(key) { await this.client.del(this.prefix + ':' + key); } + async shutdown() {} +} + +function createRequestLimiter({ prefix, windowMs = 60000, limit = 60, message = 'Too many requests' }) { + return rateLimit({ + windowMs, + limit, + standardHeaders: 'draft-8', + legacyHeaders: false, + ...(redis ? { store: new RedisRateLimitStore(redis, prefix) } : {}), + keyGenerator: req => { + const peerId = req.federationPeer?.nodeId; + if (typeof peerId === 'string' && peerId) return 'peer:' + peerId; + const userId = req.user?.userId; + return typeof userId === 'string' && userId + ? 'account:' + userId + : 'ip:' + ipKeyGenerator(req.ip); + }, + handler: (_req, res) => res.status(429).json({ error: message }) + }); +} + +module.exports = { + createRequestLimiter, + getRedisClient: () => redis, + apiRateLimit: createRequestLimiter({ prefix: 'agrinet:api', limit: 120 }), + userWriteRateLimit: createRequestLimiter({ prefix: 'agrinet:user-write', limit: 20, message: 'Too many user-management requests' }), + locationRateLimit: createRequestLimiter({ prefix: 'agrinet:location', limit: 30, message: 'Too many location requests' }), + chatRateLimit: createRequestLimiter({ prefix: 'agrinet:chat', limit: 60, message: 'Too many chat requests' }), + streamRateLimit: createRequestLimiter({ prefix: 'agrinet:stream', limit: 20, message: 'Too many stream requests' }), + federationAttemptRateLimit: createRequestLimiter({ prefix: 'agrinet:federation-attempt', limit: 20, message: 'Too many federation authentication attempts' }), + federationRateLimit: createRequestLimiter({ prefix: 'agrinet:federation', limit: 30, message: 'Too many federation requests' }) +}; diff --git a/backend/middleware/requireAdmin.js b/backend/middleware/requireAdmin.js new file mode 100644 index 00000000..f1ddda53 --- /dev/null +++ b/backend/middleware/requireAdmin.js @@ -0,0 +1,8 @@ +// Admin authorization must be based on a verified account token, never the shared API key. +module.exports = function requireAdmin(req, res, next) { + const user = req.user; + if (!user || typeof user.userId !== 'string' || !user.userId || user.role !== 'admin') { + return res.status(403).json({ error: 'Admin access required' }); + } + return next(); +}; diff --git a/backend/middleware/streamConcurrency.js b/backend/middleware/streamConcurrency.js new file mode 100644 index 00000000..24895813 --- /dev/null +++ b/backend/middleware/streamConcurrency.js @@ -0,0 +1,96 @@ +const { randomUUID } = require('node:crypto'); +const { getRedisClient } = require('./rateLimit'); + +const localReservations = new Map(); +const RESERVATION_TTL_MS = 8 * 24 * 60 * 60 * 1000; + +const ACQUIRE_SCRIPT = [ + "redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', ARGV[1])", + "local count = redis.call('ZCARD', KEYS[1])", + "if count >= tonumber(ARGV[2]) then return 0 end", + "redis.call('ZADD', KEYS[1], ARGV[3], ARGV[4])", + "redis.call('PEXPIRE', KEYS[1], ARGV[5])", + "return 1" +].join('\n'); + +const RELEASE_SCRIPT = [ + "redis.call('ZREM', KEYS[1], ARGV[1])", + "if redis.call('ZCARD', KEYS[1]) == 0 then redis.call('DEL', KEYS[1]) end", + "return 1" +].join('\n'); + +async function acquire(userId, reservationId, expiresAt, maximum) { + const redis = getRedisClient(); + if (redis) { + const key = 'agrinet:active-streams:' + userId; + const result = await redis.eval( + ACQUIRE_SCRIPT, 1, key, Date.now(), maximum, expiresAt, + reservationId, RESERVATION_TTL_MS + ); + return Number(result) === 1; + } + + // Process-local fallback is appropriate only for a single-instance deployment. + const now = Date.now(); + const reservations = localReservations.get(userId) || new Map(); + for (const [id, expiry] of reservations) { + if (expiry <= now) reservations.delete(id); + } + if (reservations.size >= maximum) { + localReservations.set(userId, reservations); + return false; + } + reservations.set(reservationId, expiresAt); + localReservations.set(userId, reservations); + return true; +} + +async function release(userId, reservationId) { + const redis = getRedisClient(); + if (redis) { + await redis.eval( + RELEASE_SCRIPT, 1, 'agrinet:active-streams:' + userId, reservationId + ); + return; + } + const reservations = localReservations.get(userId); + if (!reservations) return; + reservations.delete(reservationId); + if (reservations.size === 0) localReservations.delete(userId); +} + +function createConcurrentStreamLimit({ maximum = 5 } = {}) { + return async function concurrentStreamLimit(req, res, next) { + const userId = req.user?.userId; + const expiresAt = Number(req.user?.exp) * 1000; + if (typeof userId !== 'string' || !userId || !Number.isFinite(expiresAt) || expiresAt <= Date.now()) { + return res.status(401).json({ error: 'Valid account token required for streaming' }); + } + + const reservationId = randomUUID(); + let admitted; + try { + admitted = await acquire(userId, reservationId, expiresAt, maximum); + } catch { + return res.status(503).json({ error: 'Stream capacity service unavailable' }); + } + if (!admitted) { + return res.status(429).json({ error: 'Too many open streams for this account' }); + } + + let released = false; + const cleanup = () => { + if (released) return; + released = true; + release(userId, reservationId).catch(() => { + // Expiring Redis reservations provide crash recovery if release cannot complete. + }); + }; + res.once('close', cleanup); + res.once('finish', cleanup); + res.once('error', cleanup); + return next(); + }; +} + +module.exports = { createConcurrentStreamLimit }; diff --git a/backend/models/user.js b/backend/models/user.js index 9f451408..f203c14d 100644 --- a/backend/models/user.js +++ b/backend/models/user.js @@ -1,47 +1,53 @@ -// DynamoDB does not require a schema or model definition like Mongoose. -// Instead, you can use this file to define helper functions for user objects, -// or simply export attribute names for consistency across your codebase. - -const USER_TABLE_NAME = "Users"; +// DynamoDB user record helpers and explicit response contracts. +const USER_TABLE_NAME = 'Users'; function createUserItem({ - id, // You should generate a unique id for each user (e.g., uuid) - username, - email, - phone, - password, - verified = false, - verificationCode, - location, - coordinates, // { lat, long } - locationType, // 'farm' | 'production' | 'delivery' - locationPrivacy = false, - role = "consumer", - reputationScore = 0, + id, username, email, phone, password, verified = false, + verificationCode, location, coordinates, locationType, + locationPrivacy = false, role = 'consumer', reputationScore = 0, reputationWeight = 0 }) { const item = { - id, // Partition key for DynamoDB table - username, - email, - phone, - password, - verified, - location, - locationPrivacy, - role, - reputationScore, - reputationWeight + id, username, email, phone, password, verified, location, + locationPrivacy, role, reputationScore, reputationWeight }; - if (verificationCode !== undefined) item.verificationCode = verificationCode; if (coordinates !== undefined) item.coordinates = coordinates; if (locationType !== undefined) item.locationType = locationType; - return item; } +// Public directory DTO: no contact details, roles, precise location, or auth state. +function sanitizeUserForPublic(user = {}) { + const safe = {}; + for (const field of ['id', 'username', 'locationType', 'reputationScore', 'reputationWeight']) { + if (user[field] !== undefined) safe[field] = user[field]; + } + return safe; +} + +// Authenticated account DTO. Only returned to the account owner at login. +function sanitizeUserForAuth(user = {}) { + const safe = {}; + for (const field of ['id', 'username', 'email', 'phone', 'role', 'verified', 'reputationScore', 'reputationWeight']) { + if (user[field] !== undefined) safe[field] = user[field]; + } + return safe; +} + +// Administrator DTO intentionally excludes credentials, verification codes and exact coordinates. +function sanitizeUserForAdmin(user = {}) { + const safe = {}; + for (const field of ['id', 'username', 'email', 'phone', 'role', 'verified', 'locationType', 'reputationScore', 'reputationWeight', 'createdAt']) { + if (user[field] !== undefined) safe[field] = user[field]; + } + return safe; +} + module.exports = { USER_TABLE_NAME, - createUserItem + createUserItem, + sanitizeUserForPublic, + sanitizeUserForAuth, + sanitizeUserForAdmin }; diff --git a/backend/package-lock.json b/backend/package-lock.json index f066c735..8586abd8 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -17,11 +17,13 @@ "cors": "^2.8.5", "dotenv": "^16.4.7", "express": "^4.21.2", + "express-rate-limit": "^8.7.2", "franc": "^6.1.0", "jsonwebtoken": "^9.0.2", "openai": "^4.76.0", "stripe": "^18.5.0", - "twilio": "^4.21.0" + "twilio": "^4.21.0", + "ioredis": "^5.11.1" }, "devDependencies": { "jest": "^30.5.2", @@ -3662,6 +3664,48 @@ "url": "https://opencollective.com/express" } }, + "node_modules/express-rate-limit": { + "version": "8.7.2", + "resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-8.7.2.tgz", + "integrity": "sha512-U1ScocxDD9WI09LaBVypvNkOtNR5yWAmZLkuKJdAzIchA4YKFYF6GT5IKER7epG58oXtjvGlV0/6ii0F83EyZg==", + "license": "MIT", + "dependencies": { + "debug": "^4.4.3", + "ip-address": "^10.2.0" + }, + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://github.com/sponsors/express-rate-limit" + }, + "peerDependencies": { + "express": ">= 4.11" + } + }, + "node_modules/express-rate-limit/node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/express-rate-limit/node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "license": "MIT" + }, "node_modules/fast-json-stable-stringify": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", @@ -4211,6 +4255,15 @@ "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", "license": "MIT" }, + "node_modules/ip-address": { + "version": "10.7.5", + "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.7.5.tgz", + "integrity": "sha512-t2v0rkILYv9WXmSL7TpAaU3HXgaFYOPUcH3QHgwQ++gpJuH3bqyMy4Fvt/DJCTKwzH3Cf+bAp5W/mFL39KPLzA==", + "license": "MIT", + "engines": { + "node": ">= 12" + } + }, "node_modules/ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", diff --git a/backend/package.json b/backend/package.json index 0e1551e8..abacf17a 100644 --- a/backend/package.json +++ b/backend/package.json @@ -16,11 +16,13 @@ "cors": "^2.8.5", "dotenv": "^16.4.7", "express": "^4.21.2", + "express-rate-limit": "^8.7.2", "franc": "^6.1.0", "jsonwebtoken": "^9.0.2", "openai": "^4.76.0", "stripe": "^18.5.0", - "twilio": "^4.21.0" + "twilio": "^4.21.0", + "ioredis": "^5.11.1" }, "devDependencies": { "jest": "^30.5.2", diff --git a/backend/routes/__tests__/admin.test.js b/backend/routes/__tests__/admin.test.js index c782c0c9..518f6886 100644 --- a/backend/routes/__tests__/admin.test.js +++ b/backend/routes/__tests__/admin.test.js @@ -9,57 +9,44 @@ const { USER_TABLE_NAME } = require('../../models/user'); const adminRouter = require('../admin'); const app = express(); +app.use((req, _res, next) => { req.user = { userId: 'test-admin', role: 'admin' }; next(); }); app.use('/', adminRouter); -beforeEach(() => { - mockScan.mockReset(); -}); +beforeEach(() => mockScan.mockReset()); describe('GET /users', () => { - it('uses scan and omits password', async () => { + it('uses a minimal projection and omits private account fields', async () => { mockScan.mockReturnValue({ promise: jest.fn().mockResolvedValue({ - Items: [ - { id: '1', username: 'alice', password: 'secret' } - ] + Items: [{ id: '1', username: 'alice', email: 'a@example.test', role: 'consumer', password: 'secret', verificationCode: 'secret' }] }) }); - const res = await request(app).get('/users'); - - expect(mockScan).toHaveBeenCalledWith({ TableName: USER_TABLE_NAME }); + expect(mockScan).toHaveBeenCalledWith(expect.objectContaining({ + TableName: USER_TABLE_NAME, + ProjectionExpression: expect.stringContaining('email') + })); expect(res.status).toBe(200); - expect(res.body).toEqual([{ id: '1', username: 'alice' }]); + expect(res.body).toEqual([{ id: '1', username: 'alice', email: 'a@example.test', role: 'consumer' }]); expect(res.body[0].password).toBeUndefined(); + expect(res.body[0].verificationCode).toBeUndefined(); }); it('returns 500 when scan rejects', async () => { - mockScan.mockReturnValue({ - promise: jest.fn().mockRejectedValue(new Error('fail')) - }); - + mockScan.mockReturnValue({ promise: jest.fn().mockRejectedValue(new Error('fail')) }); const res = await request(app).get('/users'); - expect(mockScan).toHaveBeenCalled(); expect(res.status).toBe(500); }); }); describe('GET /keys', () => { - it('returns stored keys', async () => { - const sampleKeys = [ - { - id: 'k1', - key: 'abc', - userId: 'u1' - } - ]; + it('returns only non-secret key metadata', async () => { + const sampleKeys = [{ id: 'k1', userId: 'u1', issuedAt: '2026-01-01', usageCount: 1, expiresAt: '2027-01-01', key: 'secret-value' }]; mockScan.mockReturnValue({ promise: async () => ({ Items: sampleKeys }) }); - const res = await request(app).get('/keys'); - - expect(mockScan).toHaveBeenCalledWith({ TableName: 'Keys' }); + expect(mockScan).toHaveBeenCalledWith(expect.objectContaining({ TableName: 'Keys', ProjectionExpression: expect.any(String) })); expect(res.status).toBe(200); - expect(res.body).toEqual(sampleKeys); + expect(res.body).toEqual([{ id: 'k1', userId: 'u1', issuedAt: '2026-01-01', usageCount: 1, expiresAt: '2027-01-01' }]); }); }); diff --git a/backend/routes/__tests__/adminRoutes.test.js b/backend/routes/__tests__/adminRoutes.test.js new file mode 100644 index 00000000..7a9328c3 --- /dev/null +++ b/backend/routes/__tests__/adminRoutes.test.js @@ -0,0 +1,52 @@ +const request = require('supertest'); +const express = require('express'); + +jest.mock('../../lib/dynamodbClient', () => ({ + scan: jest.fn() +})); + +const docClient = require('../../lib/dynamodbClient'); +const adminRoutes = require('../admin'); + +describe('admin routes access and response contracts', () => { + let app; + beforeEach(() => { + app = express(); + app.use(express.json()); + app.use((req, _res, next) => { + req.user = { userId: 'admin-1', role: 'admin' }; + next(); + }); + app.use('/api/admin', adminRoutes); + docClient.scan.mockReset(); + }); + + test('denies shared-key callers without a verified admin identity', async () => { + app = express(); + app.use('/api/admin', adminRoutes); + const response = await request(app).get('/api/admin/users'); + expect(response.status).toBe(403); + }); + + test('returns only administrator-approved user fields', async () => { + docClient.scan.mockReturnValue({ + promise: async () => ({ + Items: [{ + id: 'u1', username: 'alice', email: 'alice@example.test', phone: '555-0100', + role: 'consumer', verified: true, password: 'bcrypt-hash', + verificationCode: 'secret', coordinates: { lat: 1, long: 2 }, + locationType: 'farm', reputationScore: 3 + }] + }) + }); + const response = await request(app).get('/api/admin/users'); + expect(response.status).toBe(200); + expect(response.body).toEqual([{ + id: 'u1', username: 'alice', email: 'alice@example.test', phone: '555-0100', + role: 'consumer', verified: true, locationType: 'farm', reputationScore: 3 + }]); + expect(docClient.scan).toHaveBeenCalledWith(expect.objectContaining({ + ProjectionExpression: expect.stringContaining('email') + })); + }); +}); diff --git a/backend/routes/__tests__/userRoutes.test.js b/backend/routes/__tests__/userRoutes.test.js index 8e0886fc..c1de31aa 100644 --- a/backend/routes/__tests__/userRoutes.test.js +++ b/backend/routes/__tests__/userRoutes.test.js @@ -10,81 +10,101 @@ jest.mock('../../lib/dynamodbClient', () => ({ const docClient = require('../../lib/dynamodbClient'); const userRoutes = require('../userRoutes'); -describe('userRoutes', () => { +describe('userRoutes security', () => { let app; beforeEach(() => { app = express(); app.use(express.json()); + app.use((req, _res, next) => { + req.user = { userId: 'admin-1', role: 'admin' }; + next(); + }); app.use('/users', userRoutes); docClient.scan.mockReset(); docClient.put.mockReset(); }); - describe('GET /users', () => { - it('returns users without passwords', async () => { - docClient.scan.mockReturnValue({ - promise: () => Promise.resolve({ - Items: [ - { id: '1', username: 'john', password: 'hash', role: 'consumer', location: 'NY' } - ] - }) - }); - - const res = await request(app).get('/users'); - expect(res.status).toBe(200); - expect(res.body).toEqual([ - { id: '1', username: 'john', role: 'consumer', location: 'NY' } - ]); - expect(res.body[0].password).toBeUndefined(); + test('public directory returns only an explicit public DTO', async () => { + docClient.scan.mockReturnValue({ + promise: async () => ({ + Items: [{ + id: 'u1', username: 'alice', role: 'admin', email: 'alice@example.test', + phone: '555-0100', password: 'hash', verificationCode: 'secret', + verified: true, location: 'private address', coordinates: { lat: 1, long: 2 }, + locationType: 'farm', reputationScore: 5, reputationWeight: 2 + }] + }) }); - it('applies role and location filters', async () => { - docClient.scan.mockReturnValue({ promise: () => Promise.resolve({ Items: [] }) }); + const response = await request(app).get('/users'); + expect(response.status).toBe(200); + expect(response.body).toEqual([{ + id: 'u1', username: 'alice', locationType: 'farm', + reputationScore: 5, reputationWeight: 2 + }]); + expect(docClient.scan).toHaveBeenCalledWith(expect.objectContaining({ + ProjectionExpression: expect.stringContaining('username') + })); + }); - await request(app).get('/users?role=farmer&location=Texas'); + test('rejects caller-supplied IDs before writing', async () => { + const response = await request(app).post('/users').send({ + id: 'victim-id', username: 'attacker', email: 'a@example.test', password: 'long-password' + }); + expect(response.status).toBe(400); + expect(docClient.put).not.toHaveBeenCalled(); + }); - expect(docClient.scan).toHaveBeenCalledWith( - expect.objectContaining({ - FilterExpression: 'role = :role AND location = :location', - ExpressionAttributeValues: { ':role': 'farmer', ':location': 'Texas' } - }) - ); + test('rejects shared API-key-style callers without a verified admin identity', async () => { + app = express(); + app.use(express.json()); + app.use('/users', userRoutes); + const response = await request(app).post('/users').send({ + username: 'attacker', email: 'a@example.test', password: 'long-password' }); + expect(response.status).toBe(403); + expect(docClient.put).not.toHaveBeenCalled(); }); - describe('POST /users', () => { - it('creates user with hashed password and defaults', async () => { - docClient.put.mockReturnValue({ promise: () => Promise.resolve({}) }); + test('rejects authenticated non-admin callers', async () => { + app = express(); + app.use(express.json()); + app.use((req, _res, next) => { + req.user = { userId: 'member-1', role: 'consumer' }; + next(); + }); + app.use('/users', userRoutes); + const response = await request(app).post('/users').send({ + username: 'member', email: 'm@example.test', password: 'long-password' + }); + expect(response.status).toBe(403); + expect(docClient.put).not.toHaveBeenCalled(); + }); - const res = await request(app) - .post('/users') - .send({ - id: '1', - username: 'alice', - email: 'a@b.com', - password: 'secret', - role: 'producer', - location: 'CA' - }); + test('creates a server-ID account with a hash and atomic no-overwrite condition', async () => { + docClient.put.mockReturnValue({ promise: async () => ({}) }); + const response = await request(app).post('/users').send({ + username: 'alice', email: 'a@b.com', password: 'secretsecret', + role: 'producer', locationType: 'farm' + }); - expect(res.status).toBe(201); - expect(res.body.user).toMatchObject({ - id: '1', - username: 'alice', - email: 'a@b.com', - role: 'producer', - location: 'CA', - verified: false, - reputationScore: 0 - }); - expect(res.body.user.password).toBeUndefined(); + expect(response.status).toBe(201); + expect(response.body.user).toMatchObject({ username: 'alice', locationType: 'farm' }); + expect(response.body.user.id).toBeTruthy(); + expect(response.body.user.role).toBeUndefined(); + const params = docClient.put.mock.calls[0][0]; + expect(params.ConditionExpression).toBe('attribute_not_exists(#id)'); + expect(params.Item.id).not.toBe('undefined'); + expect(await bcrypt.compare('secretsecret', params.Item.password)).toBe(true); + }); - const params = docClient.put.mock.calls[0][0]; - const stored = params.Item; - expect(stored.verified).toBe(false); - expect(stored.reputationScore).toBe(0); - expect(await bcrypt.compare('secret', stored.password)).toBe(true); + test('reports conditional ID collision without overwriting', async () => { + docClient.put.mockReturnValue({ + promise: async () => { throw Object.assign(new Error('collision'), { code: 'ConditionalCheckFailedException' }); } }); + const response = await request(app).post('/users').send({ + username: 'alice', email: 'a@b.com', password: 'secretsecret' + }); + expect(response.status).toBe(409); }); }); - diff --git a/backend/routes/admin.js b/backend/routes/admin.js index c64e30c2..29985707 100644 --- a/backend/routes/admin.js +++ b/backend/routes/admin.js @@ -1,30 +1,47 @@ -const express = require("express"); -const docClient = require("../lib/dynamodbClient"); -const { USER_TABLE_NAME } = require("../models/user"); -const { KEY_TABLE_NAME } = require("../models/key"); +const express = require('express'); +const docClient = require('../lib/dynamodbClient'); +const { USER_TABLE_NAME, sanitizeUserForAdmin } = require('../models/user'); +const { KEY_TABLE_NAME } = require('../models/key'); +const requireAdmin = require('../middleware/requireAdmin'); +const { userWriteRateLimit } = require('../middleware/rateLimit'); const router = express.Router(); -// Get all users -router.get("/users", async (req, res) => { - try { - const data = await docClient - .scan({ TableName: USER_TABLE_NAME }) - .promise(); - const users = data.Items.map(({ password, ...rest }) => rest); - res.json(users); - } catch (err) { - res.status(500).json({ error: "Failed to fetch users" }); - } +function sanitizeKeyForAdmin(key = {}) { + const safe = {}; + for (const field of ['id', 'userId', 'issuedAt', 'usageCount', 'expiresAt']) { + if (key[field] !== undefined) safe[field] = key[field]; + } + return safe; +} + +// Every route in this module requires a verified administrator account token. +router.use(requireAdmin); +router.use(userWriteRateLimit); + +router.get('/users', async (_req, res) => { + try { + const data = await docClient.scan({ + TableName: USER_TABLE_NAME, + ProjectionExpression: '#id, username, email, phone, #role, verified, locationType, reputationScore, reputationWeight, createdAt', + ExpressionAttributeNames: { '#id': 'id', '#role': 'role' } + }).promise(); + res.json((data.Items || []).map(sanitizeUserForAdmin)); + } catch { + res.status(500).json({ error: 'Failed to fetch users' }); + } }); -// Get key usage logs -router.get("/keys", async (req, res) => { - try { - const { Items: keys = [] } = await docClient.scan({ TableName: KEY_TABLE_NAME }).promise(); - res.json(keys); - } catch (err) { - res.status(500).json({ error: "Failed to fetch keys" }); - } +router.get('/keys', async (_req, res) => { + try { + const { Items: keys = [] } = await docClient.scan({ + TableName: KEY_TABLE_NAME, + ProjectionExpression: '#id, userId, issuedAt, usageCount, expiresAt', + ExpressionAttributeNames: { '#id': 'id' } + }).promise(); + res.json(keys.map(sanitizeKeyForAdmin)); + } catch { + res.status(500).json({ error: 'Failed to fetch keys' }); + } }); module.exports = router; diff --git a/backend/routes/authRoutes.js b/backend/routes/authRoutes.js index 9d4411cc..876b2b39 100644 --- a/backend/routes/authRoutes.js +++ b/backend/routes/authRoutes.js @@ -1,54 +1,71 @@ -const express = require("express"); -const bcrypt = require("../utils/bcrypt"); -const jwt = require("../utils/jwt"); -const { USER_TABLE_NAME, createUserItem } = require("../models/user"); +const express = require('express'); +const bcrypt = require('../utils/bcrypt'); +const jwt = require('../utils/jwt'); +const { USER_TABLE_NAME, createUserItem, sanitizeUserForAuth } = require('../models/user'); const docClient = require('../lib/dynamodbClient'); -const { randomUUID } = require('crypto'); +const { randomUUID } = require('node:crypto'); const router = express.Router(); -try { - require("dotenv").config(); -} catch { /* Environment may be supplied by the process. */ } +const { rateLimit, ipKeyGenerator } = require('express-rate-limit'); -// Test Route to Verify authRoutes -router.get("/test", (req, res) => { - console.log("Test route hit"); - res.status(200).json({ message: "Auth routes are working!" }); +const authRateLimit = rateLimit({ + windowMs: 15 * 60 * 1000, + limit: 20, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => ipKeyGenerator(req.ip), + message: { error: 'Too many authentication attempts. Try again later.' } }); -// Register User -router.post("/register", async (req, res) => { - try { - const { username, email, phone, password } = req.body; - const hashedPassword = await bcrypt.hash(password, 10); - const user = createUserItem({ id: randomUUID(), username, email, phone, password: hashedPassword }); - await docClient.put({ TableName: USER_TABLE_NAME, Item: user }).promise(); - res.status(201).json({ message: "User registered. Please verify your email/phone." }); - } catch (error) { - res.status(500).json({ error: "Error registering user." }); - } -}); +try { require('dotenv').config(); } catch { /* Environment may be supplied by the process. */ } -// Login User -router.post("/login", async (req, res) => { - try { - const { email, password } = req.body; - const { Items = [] } = await docClient.scan({ - TableName: USER_TABLE_NAME, - FilterExpression: 'email = :email', - ExpressionAttributeValues: { ':email': email }, - }).promise(); - const user = Items[0]; +router.get('/test', (_req, res) => res.status(200).json({ message: 'Auth routes are working!' })); - if (!user || !(await bcrypt.compare(password, user.password))) { - return res.status(401).json({ error: "Invalid credentials" }); - } +router.post('/register', authRateLimit, async (req, res) => { + try { + const { username, email, phone, password } = req.body || {}; + if (typeof username !== 'string' || !username.trim() || + typeof email !== 'string' || !email.trim() || + typeof password !== 'string' || password.length < 8) { + return res.status(400).json({ error: 'Username, email, and a password of at least 8 characters are required' }); + } + const user = createUserItem({ + id: randomUUID(), username: username.trim(), email: email.trim().toLowerCase(), + ...(typeof phone === 'string' ? { phone } : {}), + password: await bcrypt.hash(password, 10) + }); + await docClient.put({ + TableName: USER_TABLE_NAME, + Item: user, + ConditionExpression: 'attribute_not_exists(#id)', + ExpressionAttributeNames: { '#id': 'id' } + }).promise(); + return res.status(201).json({ message: 'User registered. Please verify your email/phone.' }); + } catch (error) { + if (error.code === 'ConditionalCheckFailedException') return res.status(409).json({ error: 'Account could not be created' }); + return res.status(500).json({ error: 'Error registering user.' }); + } +}); - const token = jwt.sign({ userId: user.id, role: user.role }, process.env.JWT_SECRET, { expiresIn: "7d" }); - const { password: _password, ...publicUser } = user; - res.json({ token, user: publicUser }); - } catch (error) { - res.status(500).json({ error: "Login error." }); +router.post('/login', authRateLimit, async (req, res) => { + try { + const { email, password } = req.body || {}; + if (typeof email !== 'string' || typeof password !== 'string') { + return res.status(400).json({ error: 'Email and password are required' }); + } + const { Items = [] } = await docClient.scan({ + TableName: USER_TABLE_NAME, + FilterExpression: 'email = :email', + ExpressionAttributeValues: { ':email': email.trim().toLowerCase() } + }).promise(); + const user = Items[0]; + if (!user || !(await bcrypt.compare(password, user.password))) { + return res.status(401).json({ error: 'Invalid credentials' }); } + const token = jwt.sign({ userId: user.id, role: user.role }, process.env.JWT_SECRET, { expiresIn: '7d' }); + return res.json({ token, user: sanitizeUserForAuth(user) }); + } catch { + return res.status(500).json({ error: 'Login error.' }); + } }); module.exports = router; diff --git a/backend/routes/communicationRoutes.js b/backend/routes/communicationRoutes.js index 3a0367c2..8f058204 100644 --- a/backend/routes/communicationRoutes.js +++ b/backend/routes/communicationRoutes.js @@ -1,11 +1,43 @@ const express = require('express'); const router = express.Router(); const commController = require('../controllers/communication_controller'); -const auth = require('../middleware/authMiddleware'); +const auth = require('../middleware/chatAuth'); const asyncHandler = require('../utils/asyncHandler'); +const { rateLimit, ipKeyGenerator } = require('express-rate-limit'); -router.use(auth); -router.post('/:conversationId', asyncHandler(commController.sendMessage)); -router.get('/:conversationId', asyncHandler(commController.listMessages)); +router.post('/:conversationId', rateLimit({ + windowMs: 60 * 1000, + limit: 300, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many authentication attempts' } +}), auth, rateLimit({ + windowMs: 60 * 1000, + limit: 120, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => req.user?.userId + ? 'account:' + req.user.userId + : 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many requests, please try again later' } +}), asyncHandler(commController.sendMessage)); +router.get('/:conversationId', rateLimit({ + windowMs: 60 * 1000, + limit: 300, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many authentication attempts' } +}), auth, rateLimit({ + windowMs: 60 * 1000, + limit: 120, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => req.user?.userId + ? 'account:' + req.user.userId + : 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many requests, please try again later' } +}), asyncHandler(commController.listMessages)); module.exports = router; diff --git a/backend/routes/conversationRoutes.js b/backend/routes/conversationRoutes.js index 49739001..a89e5e8a 100644 --- a/backend/routes/conversationRoutes.js +++ b/backend/routes/conversationRoutes.js @@ -1,14 +1,94 @@ const express = require('express'); const router = express.Router(); -const auth = require('../middleware/authMiddleware'); +const auth = require('../middleware/chatAuth'); const ctrl = require('../controllers/conversation_controller'); const asyncHandler = require('../utils/asyncHandler'); +const { rateLimit, ipKeyGenerator } = require('express-rate-limit'); -router.use(auth); -router.post('/', asyncHandler(ctrl.create)); -router.get('/', asyncHandler(ctrl.list)); -router.put('/:id', asyncHandler(ctrl.rename)); -router.delete('/:id', asyncHandler(ctrl.remove)); -router.post('/:id/pin', asyncHandler(ctrl.pin)); +router.post('/', rateLimit({ + windowMs: 60 * 1000, + limit: 300, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many authentication attempts' } +}), auth, rateLimit({ + windowMs: 60 * 1000, + limit: 120, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => req.user?.userId + ? 'account:' + req.user.userId + : 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many requests, please try again later' } +}), asyncHandler(ctrl.create)); +router.get('/', rateLimit({ + windowMs: 60 * 1000, + limit: 300, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many authentication attempts' } +}), auth, rateLimit({ + windowMs: 60 * 1000, + limit: 120, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => req.user?.userId + ? 'account:' + req.user.userId + : 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many requests, please try again later' } +}), asyncHandler(ctrl.list)); +router.put('/:id', rateLimit({ + windowMs: 60 * 1000, + limit: 300, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many authentication attempts' } +}), auth, rateLimit({ + windowMs: 60 * 1000, + limit: 120, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => req.user?.userId + ? 'account:' + req.user.userId + : 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many requests, please try again later' } +}), asyncHandler(ctrl.rename)); +router.delete('/:id', rateLimit({ + windowMs: 60 * 1000, + limit: 300, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many authentication attempts' } +}), auth, rateLimit({ + windowMs: 60 * 1000, + limit: 120, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => req.user?.userId + ? 'account:' + req.user.userId + : 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many requests, please try again later' } +}), asyncHandler(ctrl.remove)); +router.post('/:id/pin', rateLimit({ + windowMs: 60 * 1000, + limit: 300, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many authentication attempts' } +}), auth, rateLimit({ + windowMs: 60 * 1000, + limit: 120, + standardHeaders: 'draft-6', + legacyHeaders: false, + keyGenerator: req => req.user?.userId + ? 'account:' + req.user.userId + : 'ip:' + ipKeyGenerator(req.ip), + message: { error: 'Too many requests, please try again later' } +}), asyncHandler(ctrl.pin)); module.exports = router; diff --git a/backend/routes/userRoutes.js b/backend/routes/userRoutes.js index 18e12fa7..a532ab2e 100644 --- a/backend/routes/userRoutes.js +++ b/backend/routes/userRoutes.js @@ -1,63 +1,95 @@ const express = require('express'); +const { randomUUID } = require('node:crypto'); const bcrypt = require('../utils/bcrypt'); const router = express.Router(); const docClient = require('../lib/dynamodbClient'); -const { USER_TABLE_NAME, createUserItem } = require('../models/user'); +const { USER_TABLE_NAME, createUserItem, sanitizeUserForPublic } = require('../models/user'); +const requireAdmin = require('../middleware/requireAdmin'); +const { userWriteRateLimit } = require('../middleware/rateLimit'); -// GET /users - fetch all users, omit sensitive data +// GET /users is a public directory, not an account export. router.get('/', async (req, res) => { - const params = { - TableName: USER_TABLE_NAME - }; - - const filters = []; - const values = {}; - if (req.query.role) { - filters.push('role = :role'); - values[':role'] = req.query.role; - } - if (req.query.location) { - filters.push('location = :location'); - values[':location'] = req.query.location; - } - if (filters.length) { - params.FilterExpression = filters.join(' AND '); - params.ExpressionAttributeValues = values; - } - try { + const params = { + TableName: USER_TABLE_NAME, + ProjectionExpression: '#id, username, locationType, reputationScore, reputationWeight', + ExpressionAttributeNames: { '#id': 'id' } + }; + if (req.query.locationType) { + params.FilterExpression = 'locationType = :locationType'; + params.ExpressionAttributeValues = { ':locationType': req.query.locationType }; + } const data = await docClient.scan(params).promise(); - const users = (data.Items || []).map(({ password, ...rest }) => rest); - res.json(users); - } catch (err) { + res.json((data.Items || []).map(sanitizeUserForPublic)); + } catch { res.status(500).json({ error: 'Failed to fetch users' }); } }); -// POST /users - create a new user -router.post('/', async (req, res) => { +// Administrative provisioning only. Public registration remains /api/auth/register. +router.post('/', userWriteRateLimit, requireAdmin, async (req, res) => { try { - const { password, ...rest } = req.body; - const hashedPassword = await bcrypt.hash(password, 10); + const body = req.body || {}; + if (Object.prototype.hasOwnProperty.call(body, 'id')) { + return res.status(400).json({ error: 'Client-supplied user ids are not allowed' }); + } + + const allowed = ['username', 'email', 'phone', 'password', 'locationType', 'locationPrivacy', 'role']; + const unexpected = Object.keys(body).filter(key => !allowed.includes(key)); + if (unexpected.length) { + return res.status(400).json({ error: 'Unsupported user fields', fields: unexpected }); + } + + const { username, email, phone, password, locationType, locationPrivacy, role } = body; + if (typeof username !== 'string' || !username.trim() || + typeof email !== 'string' || !email.trim() || + typeof password !== 'string' || password.length < 8) { + return res.status(400).json({ error: 'Username, email, and a password of at least 8 characters are required' }); + } + if (locationPrivacy !== undefined && typeof locationPrivacy !== 'boolean') { + return res.status(400).json({ error: 'locationPrivacy must be a boolean' }); + } + if (locationType !== undefined && !['farm', 'production', 'delivery'].includes(locationType)) { + return res.status(400).json({ error: 'Invalid locationType' }); + } + if (role !== undefined && !['consumer', 'producer', 'farmer', 'admin'].includes(role)) { + return res.status(400).json({ error: 'Invalid role' }); + } + const item = createUserItem({ - ...rest, - password: hashedPassword, + id: randomUUID(), + username: username.trim(), + email: email.trim().toLowerCase(), + ...(typeof phone === 'string' ? { phone } : {}), + ...(locationType ? { locationType } : {}), + ...(locationPrivacy !== undefined ? { locationPrivacy } : {}), + role: role || 'consumer', + password: await bcrypt.hash(password, 10), verified: false, - reputationScore: 0 + reputationScore: 0, + reputationWeight: 0 + }); + + await docClient.put({ + TableName: USER_TABLE_NAME, + Item: item, + ConditionExpression: 'attribute_not_exists(#id)', + ExpressionAttributeNames: { '#id': 'id' } + }).promise(); + + return res.status(201).json({ + message: 'User created', + user: sanitizeUserForPublic(item) }); - await docClient.put({ TableName: USER_TABLE_NAME, Item: item }).promise(); - const { password: _pwd, ...user } = item; - res.status(201).json({ message: 'User created', user }); } catch (err) { - // Log the error for debugging - console.error('Error creating user:', err); - // Provide more specific error information if available + if (err.code === 'ConditionalCheckFailedException') { + return res.status(409).json({ error: 'User already exists' }); + } if (err.code === 'ValidationException') { - return res.status(400).json({ error: 'Validation error', details: err.message }); + return res.status(400).json({ error: 'Invalid user data' }); } - res.status(500).json({ error: err.message || 'Failed to create user' }); + return res.status(500).json({ error: 'Failed to create user' }); } }); module.exports = router; - diff --git a/backend/server.js b/backend/server.js index 7a827381..9c1aeccc 100644 --- a/backend/server.js +++ b/backend/server.js @@ -1,173 +1,44 @@ const express = require("express"); const http = require('http'); let cors; -try { - cors = require("cors"); -} catch { - cors = () => (_req, _res, next) => next(); -} +try { cors = require('cors'); } catch { cors = () => (_req, _res, next) => next(); } let dotenv; -try { - dotenv = require("dotenv"); -} catch { - dotenv = { config: () => {} }; -} -const path = require("path"); -const authMiddleware = require("./middleware/authMiddleware"); - -// Load environment variables +try { dotenv = require('dotenv'); } catch { dotenv = { config: () => {} }; } dotenv.config(); - +const authMiddleware = require('./middleware/authMiddleware'); +const { apiRateLimit, streamRateLimit, locationRateLimit } = require('./middleware/rateLimit'); const minimal = process.env.MINIMAL_SERVER === 'true'; - const app = express(); - -// --- PRODUCTION-READY CORS RESTRICTION --- -const allowedOrigins = ['https://www.ntari.org']; -app.use(cors({ - origin: function (origin, callback) { - if (!origin || allowedOrigins.includes(origin)) { - callback(null, true); - } else { - callback(new Error('CORS not allowed for this origin')); - } - }, - credentials: true, - methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], - allowedHeaders: ['Content-Type', 'Authorization', 'Idempotency-Key'] -})); -// ----------------------------------------- - -app.use(express.json()); -const tryMount = (route, modPath) => { - try { - const mod = require(modPath); - app.use(route, mod); - } catch (err) { - console.warn(`Skipping ${modPath}: ${err.message}`); - } -}; - -if (!minimal) { - tryMount("/deposit", "./routes/depositRoutes"); -} - -// Health Check Endpoint -app.get('/health', (req, res) => { - res.status(200).send('OK'); -}); - -// Server & SSE event stream +const allowedOrigins = (process.env.CORS_ALLOWED_ORIGINS || 'https://www.ntari.org').split(',').map(origin => origin.trim()).filter(Boolean); +app.use(cors({ origin(origin, callback) { if (!origin || allowedOrigins.includes(origin)) return callback(null, true); return callback(new Error('CORS not allowed for this origin')); }, credentials: true, methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], allowedHeaders: ['Content-Type', 'Authorization', 'Idempotency-Key', 'X-API-Key', 'X-Federation-Node-Id', 'X-Federation-Timestamp', 'X-Federation-Signature'] })); +app.use(express.json({ verify(req, _res, buffer) { req.rawBody = Buffer.from(buffer); } })); +const tryMount = (route, modPath) => { try { app.use(route, require(modPath)); } catch (err) { console.warn('Skipping ' + modPath + ': ' + err.message); } }; +if (!minimal) tryMount('/deposit', './routes/depositRoutes'); +app.get('/health', (_req, res) => res.status(200).send('OK')); const server = http.createServer(app); - -// Simple Server-Sent Events implementation -const sseClients = new Set(); -// Map of conversationId -> Set of response objects +// Fail closed until owner-authenticated streaming (#192) is merged. +app.get('/events', (_req, res) => res.status(410).json({ error: 'Global event stream retired; use authenticated conversation streams' })); +app.get('/stream/:conversationId', streamRateLimit, (_req, res) => res.status(410).json({ error: 'Conversation streaming is temporarily unavailable pending owner-authenticated stream support' })); const conversationStreams = new Map(); - -app.get('/events', (req, res) => { - res.writeHead(200, { - 'Content-Type': 'text/event-stream', - 'Cache-Control': 'no-cache', - Connection: 'keep-alive' - }); - sseClients.add(res); - req.on('close', () => sseClients.delete(res)); -}); - -// Unified conversation-scoped Server-Sent Events endpoint -app.get('/stream/:conversationId', (req, res) => { - const { conversationId } = req.params; - res.writeHead(200, { - 'Content-Type': 'text/event-stream', - 'Cache-Control': 'no-cache', - Connection: 'keep-alive' - }); - - if (!conversationStreams.has(conversationId)) { - conversationStreams.set(conversationId, new Set()); - } - - const clients = conversationStreams.get(conversationId); - clients.add(res); - - req.on('close', () => { - clients.delete(res); - if (clients.size === 0) { - conversationStreams.delete(conversationId); - } - }); -}); - -function sendConversationEvent(conversationId, event, data) { - const clients = conversationStreams.get(conversationId); - if (!clients) return; - const payload = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; - clients.forEach(res => res.write(payload)); -} - -function broadcast(event, data, conversationId) { - if (conversationId) { - sendConversationEvent(conversationId, event, data); - return; - } - const payload = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; - sseClients.forEach(res => res.write(payload)); -} +function sendConversationEvent(conversationId, event, data) { const clients = conversationStreams.get(conversationId); if (!clients) return; const payload = 'event: ' + event + '\ndata: ' + JSON.stringify(data) + '\n\n'; clients.forEach(res => res.write(payload)); } +function broadcast(event, data, conversationId) { if (conversationId) sendConversationEvent(conversationId, event, data); } global.broadcast = broadcast; - -// Helpers to emit token and message events -function emitToken(conversationId, id, token) { - sendConversationEvent(conversationId, 'token', { id, token }); -} - -function emitMessage(conversationId, message) { - sendConversationEvent(conversationId, 'message', { message }); -} - -global.emitToken = emitToken; -global.emitMessage = emitMessage; - -// Middleware +global.emitToken = (conversationId, id, token) => sendConversationEvent(conversationId, 'token', { id, token }); +global.emitMessage = (conversationId, message) => sendConversationEvent(conversationId, 'message', { message }); +// Federation authenticates with per-peer HMAC credentials, independently of browser API keys. +if (!minimal) tryMount('/federation', './federation/federationRoutes'); +// Public account creation/login have their own IP throttles and do not require the browser integration key. +if (!minimal) tryMount('/api/auth', './routes/authRoutes'); app.use(authMiddleware); -app.use('/uploads', express.static(path.join(__dirname, 'uploads'))); - -// Routes +app.use(apiRateLimit); +// Private uploads are disabled until conversation-owner authorization is deployed (#191). +app.use('/uploads', (_req, res) => res.status(410).json({ error: 'Attachment downloads are temporarily unavailable pending owner authorization' })); +app.use('/api/location', locationRateLimit); let runFederationSync; if (!minimal) { - [ - ['/', './routes/api'], - ['/api/auth', './routes/authRoutes'], - ['/api/keys', './routes/keyRoutes'], - ['/api/contracts', './routes/contracts'], - ['/api/admin', './routes/admin'], - ['/api/marketplace', './marketplace/marketplace_routes'], - ['/users', './routes/userRoutes'], - ['/products', './routes/products'], - ['/broadcast', './routes/broadcastRoutes'], - ['/sms', './routes/smsRoutes'], - ['/cart', './routes/cartRoutes'], - ['/orders', './routes/orderRoutes'], - ['/subscriptions', './routes/subscriptionRoutes'], - ['/conversations', './routes/conversationRoutes'], - ['/messages', './routes/communicationRoutes'], - ['/inventory', './routes/inventoryRoutes'], - ['/api/location', './routes/locationRoutes'], - ['/federation', './federation/federationRoutes'], - ['/trends', './trends/trendsRoutes'], - ].forEach(([route, mod]) => tryMount(route, mod)); - - try { - runFederationSync = require('./federation/federationSyncJob'); - } catch (err) { - console.warn(`Federation sync disabled: ${err.message}`); - } + [['/', './routes/api'], ['/api/keys', './routes/keyRoutes'], ['/api/contracts', './routes/contracts'], ['/api/admin', './routes/admin'], ['/api/marketplace', './marketplace/marketplace_routes'], ['/users', './routes/userRoutes'], ['/products', './routes/products'], ['/broadcast', './routes/broadcastRoutes'], ['/sms', './routes/smsRoutes'], ['/cart', './routes/cartRoutes'], ['/orders', './routes/orderRoutes'], ['/subscriptions', './routes/subscriptionRoutes'], ['/conversations', './routes/conversationRoutes'], ['/messages', './routes/communicationRoutes'], ['/inventory', './routes/inventoryRoutes'], ['/api/location', './routes/locationRoutes'], ['/trends', './trends/trendsRoutes']].forEach(([route, mod]) => tryMount(route, mod)); + try { runFederationSync = require('./federation/federationSyncJob'); } catch (err) { console.warn('Federation sync disabled: ' + err.message); } } - const PORT = process.env.PORT || 5000; -if (require.main === module) { - server.listen(PORT, () => console.log(`Server running on port ${PORT}`)); - if (runFederationSync) runFederationSync(); // kicks off first run when available -} - +if (require.main === module) { server.listen(PORT, () => console.log('Server running on port ' + PORT)); if (runFederationSync) runFederationSync(); } module.exports = { app, server }; diff --git a/backend/sync/__tests__/importHelper.test.js b/backend/sync/__tests__/importHelper.test.js index 82da6c83..2c1826b0 100644 --- a/backend/sync/__tests__/importHelper.test.js +++ b/backend/sync/__tests__/importHelper.test.js @@ -1,14 +1,57 @@ const { test } = require('node:test'); const assert = require('node:assert/strict'); const docClient = require('../../lib/dynamodbClient'); -const { importFederatedData } = require('../importHelper'); +const { importFederatedData, sanitizeImportedUser } = require('../importHelper'); test('empty federation import succeeds without writes', async () => { docClient.put = () => { throw new Error('unexpected write'); }; assert.deepEqual(await importFederatedData({}), { success: true }); }); +test('import requires source identity established by peer authentication', async () => { + assert.throws(() => sanitizeImportedUser({ id: 'remote-1', username: 'alice' }), /source identity/); +}); + +test('import strips account authority and preserves authenticated provenance', async () => { + const item = sanitizeImportedUser({ + id: 'remote-1', username: 'alice', password: 'hash', verificationCode: 'secret', + verified: true, role: 'admin', email: 'a@example.test', phone: '555-0100', + coordinates: { lat: 1, long: 2 }, sourceNodeId: 'forged-node', + originNodeId: 'origin-node', originUserId: 'origin-user', + arbitrarySecret: 'must-not-survive', reputationScore: 8 + }, 'authenticated-peer'); + + assert.deepEqual(item, { + id: 'remote-1', username: 'alice', reputationScore: 8, + sourceNodeId: 'authenticated-peer', remoteUserId: 'remote-1', + originNodeId: 'origin-node', originUserId: 'origin-user' + }); +}); + +test('conditional user create refuses collisions without a pre-read race', async () => { + const writes = []; + docClient.put = params => ({ + promise: async () => { + writes.push(params); + throw Object.assign(new Error('collision'), { code: 'ConditionalCheckFailedException' }); + } + }); + + await assert.rejects( + () => importFederatedData({ + users: [{ id: 'existing-id', username: 'alice', role: 'admin', password: 'stolen' }] + }, { sourceNodeId: 'peer-1' }), + error => error.code === 'ConditionalCheckFailedException' + ); + assert.equal(writes.length, 1); + assert.equal(writes[0].ConditionExpression, 'attribute_not_exists(#id)'); + assert.equal(writes[0].Item.sourceNodeId, 'peer-1'); + assert.equal(writes[0].Item.password, undefined); +}); + test('federation import propagates storage errors', async () => { docClient.put = () => ({ promise: async () => { throw new Error('write failed'); } }); - await assert.rejects(importFederatedData({ users: [{ id: 'u1' }] }), /write failed/); + await assert.rejects(importFederatedData({ users: [{ id: 'u3', username: 'bob' }] }, { + sourceNodeId: 'peer-1' + }), /write failed/); }); diff --git a/backend/sync/importHelper.js b/backend/sync/importHelper.js index 82c797c4..04df5769 100644 --- a/backend/sync/importHelper.js +++ b/backend/sync/importHelper.js @@ -3,7 +3,39 @@ const { LISTING_TABLE_NAME } = require('../models/listing'); const { TRANSACTION_TABLE_NAME } = require('../models/transaction'); const { USER_TABLE_NAME } = require('../models/user'); -async function importFederatedData(data) { +function sanitizeImportedUser(user, sourceNodeId) { + if (!user || typeof user !== 'object' || Array.isArray(user)) { + throw new Error('Invalid user entry in federated import'); + } + if (typeof user.id !== 'string' || !user.id.trim()) { + throw new Error('Federated user must have a remote user id'); + } + if (typeof user.username !== 'string' || !user.username.trim()) { + throw new Error('Federated user must have a username'); + } + if (!sourceNodeId || typeof sourceNodeId !== 'string') { + throw new Error('Authenticated federation source identity is required'); + } + + // Never spread an untrusted remote object into a local account record. + // Role, password, contact details, verification state and unknown fields are omitted. + return { + id: user.id, + username: user.username, + ...(Number.isFinite(user.reputationScore) ? { reputationScore: user.reputationScore } : {}), + ...(Number.isFinite(user.reputationWeight) ? { reputationWeight: user.reputationWeight } : {}), + sourceNodeId, // Set from verified peer authentication, not request JSON. + remoteUserId: user.id, // Explicit identity at the authenticated remote node. + originNodeId: typeof user.originNodeId === 'string' ? user.originNodeId : sourceNodeId, + originUserId: typeof user.originUserId === 'string' ? user.originUserId : user.id + }; +} + +async function importFederatedData(data, { sourceNodeId } = {}) { + if (!data || typeof data !== 'object' || Array.isArray(data)) { + throw new Error('Federation payload must be an object'); + } + for (const listing of data.listings || []) { await docClient.put({ TableName: LISTING_TABLE_NAME, Item: listing }).promise(); } @@ -11,9 +43,15 @@ async function importFederatedData(data) { await docClient.put({ TableName: TRANSACTION_TABLE_NAME, Item: transaction }).promise(); } for (const user of data.users || []) { - await docClient.put({ TableName: USER_TABLE_NAME, Item: user }).promise(); + const item = sanitizeImportedUser(user, sourceNodeId); + await docClient.put({ + TableName: USER_TABLE_NAME, + Item: item, + ConditionExpression: 'attribute_not_exists(#id)', + ExpressionAttributeNames: { '#id': 'id' } + }).promise(); } return { success: true }; } -module.exports = { importFederatedData }; +module.exports = { importFederatedData, sanitizeImportedUser }; diff --git a/backend/sync/importHelper.test.js b/backend/sync/importHelper.test.js index d45c600f..a0e5f9e9 100644 --- a/backend/sync/importHelper.test.js +++ b/backend/sync/importHelper.test.js @@ -3,14 +3,30 @@ const assert = require('node:assert/strict'); const docClient = require('../lib/dynamodbClient'); const { importFederatedData } = require('./importHelper'); -test('imports listings, transactions, and users into their DynamoDB tables', async () => { +test('imports listings and transactions and conditionally creates sanitized federated users', async () => { const calls = []; - docClient.put = (params) => ({ promise: async () => calls.push(params) }); - const data = { listings: [{ id: 'l1' }], transactions: [{ id: 't1' }], users: [{ id: 'u1' }] }; - await importFederatedData(data); + docClient.put = params => ({ promise: async () => calls.push(params) }); + const data = { + listings: [{ id: 'l1' }], + transactions: [{ id: 't1' }], + users: [{ id: 'u1', username: 'remote-user', password: 'must-not-import', role: 'admin', verified: true }] + }; + await importFederatedData(data, { sourceNodeId: 'authenticated-peer' }); assert.deepEqual(calls, [ { TableName: 'Listings', Item: data.listings[0] }, { TableName: 'Transactions', Item: data.transactions[0] }, - { TableName: 'Users', Item: data.users[0] }, + { + TableName: 'Users', + Item: { + id: 'u1', + username: 'remote-user', + sourceNodeId: 'authenticated-peer', + remoteUserId: 'u1', + originNodeId: 'authenticated-peer', + originUserId: 'u1' + }, + ConditionExpression: 'attribute_not_exists(#id)', + ExpressionAttributeNames: { '#id': 'id' } + } ]); });