Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/location-storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Location storage regression

on:
pull_request:
paths:
- 'backend/**'
- '.github/workflows/location-storage.yml'

permissions:
contents: read

jobs:
location-storage:
runs-on: ubuntu-latest
services:
dynamodb:
image: amazon/dynamodb-local@sha256:6ada631ec7aa5fc83446855eb17233e3dad184fec47477e8140c6c569cfe0c86
ports:
- 18001:8000
defaults:
run:
working-directory: backend
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
cache: npm
cache-dependency-path: backend/package-lock.json
- run: npm ci
- run: npm test
- name: Wait for DynamoDB Local
run: |
for attempt in $(seq 1 30); do
if curl --silent --output /dev/null http://127.0.0.1:18001; then exit 0; fi
sleep 1
done
exit 1
- run: node --test scripts/check-locations.js
4 changes: 4 additions & 0 deletions CONFORMANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Adopts the covenant scale (LBTAS, v2 floor) and carries its own record instance.

The reference economy. Its operator/federation model is the named reference for the substrate's frontend-as-operator design target.

### Operator-local location data

Location persistence uses the separate `Locations` table, never the commons record or generic Users federation payloads. `backend/controllers/locationController.js` requires a verified account JWT for owner writes/reloads and explicit boolean consent for discovery. Public discovery emits an allowlisted location DTO and encrypted pagination cursors. `backend/scripts/check-locations.js` binds these claims to real DynamoDB storage checks. See [deployment, privacy semantics and residual limits](docs/location-api.md). This does not promote any in-review ledger binding or fix legacy user/federation account-security gaps.

The application message API supports optional retry receipts, scoped to the
authenticated caller and conversation. These are private operational metadata,
not sealed dialog or commons entries; they store hashes and message references,
Expand Down
4 changes: 4 additions & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ STRIPE_KEY=
UI_STRING=Fruitful_app
NODE_ID=backend-node-01
NODE_TYPE=backend

# Operator-local location table (string partition key: id).
# Provision before enabling location persistence; never include in federation exports.
LOCATION_TABLE_NAME=Locations
4 changes: 4 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,7 @@ See `backend/package.json` for Jest config and tests. Add route/model tests unde

---
Contributions welcome! Please keep `server.js` slim by adding routes and logic in domain folders.

## Location persistence

See [the location API guide](../docs/location-api.md) for table provisioning, owner JWTs, explicit publication consent, precision, kilometre radius semantics, and bounded discovery pagination. New location data stays in a separate operator-local table, outside generic user exports.
74 changes: 74 additions & 0 deletions backend/__tests__/locationRateLimit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const request = require('supertest');
const http = require('node:http');
const jwt = require('jsonwebtoken');
const { rateLimit } = require('express-rate-limit');

jest.mock('../lib/dynamodbClient', () => ({
get: ({ Key }) => ({ promise: async () => ({ Item: { id: Key.id, coordinates: { lat: 0, long: 0 }, privacy: false } }) }),
}));

describe('location request budgets', () => {
let app, server, agent;
const send = (method, url) => request(server)[method](url).agent(agent);
const env = { ...process.env };
const authorization = userId => `Bearer ${jwt.sign({ userId }, process.env.JWT_SECRET, { expiresIn: '2h' })}`;

beforeAll(() => {
process.env.JWT_SECRET = 'location-budget-test';
process.env.API_KEY = 'location-budget-key';
});
beforeEach(async () => {
jest.resetModules();
const express = require('express');
app = express();
// Bound this test server too, while allowing the route's 1,000-request
// budget and independent-account behavior to be exercised first.
app.use(express.json(), rateLimit({ windowMs: 60 * 1000, limit: 10000 }), require('../middleware/authMiddleware'));
app.use('/api/location', require('../routes/locationRoutes'));
agent = new http.Agent({ keepAlive: true });
server = app.listen(0, '127.0.0.1');
await new Promise(resolve => server.once('listening', resolve));
});
afterEach(async () => {
agent.destroy();
await new Promise(resolve => server.close(resolve));
});
afterAll(() => { process.env = env; });

test('owner reads and writes share a budget without changing ownership denials', async () => {
const token = authorization('alice');
for (let i = 0; i < 1000; i++) {
const method = i % 2 ? 'post' : 'get';
const response = await send(method, '/api/location/foreign').set('Authorization', token);
expect(response.status).toBe(403);
}
const blocked = await send('get', '/api/location/foreign').set('Authorization', authorization('alice'));
expect(blocked.status).toBe(429);
expect(Number(blocked.headers['retry-after'])).toBeGreaterThan(0);
expect((await send('post', '/api/location/foreign').set('Authorization', token)
.set('X-Forwarded-For', '203.0.113.9')).status).toBe(429);
expect((await send('get', '/api/location/foreign').set('Authorization', authorization('bob'))).status).toBe(403);
}, 20000);

test('API-key requests use an IP budget that cannot be reset by forwarded headers', async () => {
for (let i = 0; i < 1000; i++) {
expect((await send('get', '/api/location/foreign').set('x-api-key', process.env.API_KEY)).status).toBe(401);
}
const blocked = await send('get', '/api/location/foreign').set('x-api-key', process.env.API_KEY)
.set('X-Forwarded-For', '203.0.113.10');
expect(blocked.status).toBe(429);
// Owner authentication accepts any nonempty string ID, including spaces.
expect((await send('get', '/api/location/%20').set('Authorization', authorization(' '))).status).toBe(200);
}, 20000);

test('adding an API key cannot give an owner another account budget', async () => {
const token = authorization('credential-owner');
for (let i = 0; i < 1000; i++) {
expect((await send('get', '/api/location/credential-owner').set('Authorization', token)).status).toBe(200);
}
for (const key of [process.env.API_KEY, 'invalid-key']) {
expect((await send('get', '/api/location/credential-owner').set('Authorization', token)
.set('x-api-key', key)).status).toBe(429);
}
}, 20000);
});
13 changes: 13 additions & 0 deletions backend/__tests__/message_retries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,19 @@ test('deleting a conversation never lets an old retry resurrect its messages or
assert.equal(aiCalls, 1);
});


test('a missing persisted assistant reply cannot replay a partial success', async () => {
const first = await send();
assert.equal(first.status, 201);
const [replyKey] = [...db.tables.get('Messages')].find(([, row]) => row.id === first.body.reply.id);
db.tables.get('Messages').delete(replyKey);
const retry = await send();
assert.equal(retry.status, 410);
assert.equal(retry.body.code, 'message_send_removed');
assert.equal(messages().length, 1);
assert.equal(aiCalls, 1);
});

test('a missing receipt table refuses keyed sends before any message or AI work', async () => {
const put = docClient.put;
docClient.put = params => params.TableName === 'MessageSends'
Expand Down
116 changes: 70 additions & 46 deletions backend/controllers/locationController.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,82 @@
const User = require('../models/user');
const jwt = require('jsonwebtoken');
const docClient = require('../lib/dynamodbClient');
const { LOCATION_TABLE_NAME, publicLocation, encodeCursor, decodeCursor } = require('../models/location');
const { parseAgriNet, validateCoords, isWithinGeofence } = require('../utils/geo');
const allowedTypes = ['farm', 'production', 'delivery'];

// Store coordinates for a user using AgriNet format
exports.setLocation = async (req, res) => {
try {
const { coordinates, locationType, privacy } = req.body;
const { lat, long } = parseAgriNet(coordinates);
validateCoords(lat, long);
function invalid(message) { return Object.assign(new Error(message), { status: 400 }); }
function fail(res, err) {
return res.status(err.status || 500).json({ error: err.status ? err.message : 'Location storage unavailable' });
}

const allowedTypes = ['farm', 'production', 'delivery'];
if (locationType && !allowedTypes.includes(locationType)) {
throw new Error('Invalid location type');
}
// General API-key authentication does not establish an account owner.
exports.requireOwner = (req, res, next) => {
try {
const header = req.headers.authorization;
if (!header || !header.startsWith('Bearer ')) throw new Error('Missing token');
const owner = jwt.verify(header.slice(7), process.env.JWT_SECRET, { algorithms: ['HS256'] });
if (typeof owner.userId !== 'string' || !owner.userId) throw new Error('Missing account');
if (owner.userId !== req.params.id) return res.status(403).json({ error: 'Location belongs to another account' });
req.user = owner;
next();
} catch {
return res.status(401).json({ error: 'A valid account token is required' });
}
};

const update = {
coordinates: { lat, long },
locationType,
locationPrivacy: !!privacy
};
exports.setLocation = async (req, res) => {
try {
const { coordinates, locationType, privacy } = req.body || {};
if (typeof coordinates !== 'string') throw invalid('Coordinates must use AgriNet format');
if (typeof privacy !== 'boolean') throw invalid('privacy must explicitly be true (shared) or false (private)');
if (locationType !== undefined && !allowedTypes.includes(locationType)) throw invalid('Invalid location type');
let parsed;
try { parsed = parseAgriNet(coordinates); validateCoords(parsed.lat, parsed.long); }
catch (err) { throw invalid(err.message); }
const item = { id: req.params.id, coordinates: parsed, privacy,
...(locationType === undefined ? {} : { locationType }) };
await docClient.put({ TableName: LOCATION_TABLE_NAME, Item: item }).promise();
res.json({ message: 'Location saved', ...item });
} catch (err) { fail(res, err); }
};

// Update user record; implementation depends on DB layer
if (User.findByIdAndUpdate) {
await User.findByIdAndUpdate(req.params.id, update, { new: true });
}
exports.getLocation = async (req, res) => {
try {
const { Item } = await docClient.get({ TableName: LOCATION_TABLE_NAME, Key: { id: req.params.id }, ConsistentRead: true }).promise();
if (!Item) return res.status(404).json({ error: 'Location not found' });
res.json({ ...publicLocation(Item), privacy: Item.privacy === true });
} catch (err) { fail(res, err); }
};

res.json({ message: 'Location saved', coordinates: update.coordinates });
} catch (err) {
res.status(400).json({ error: err.message });
function number(value) {
if (typeof value !== 'string' || !/^-?(?:\d+(?:\.\d*)?|\.\d+)$/.test(value) || !Number.isFinite(Number(value))) {
throw invalid('Invalid geofence parameters');
}
};
return Number(value);
}

// Retrieve public locations within a geofence for marketplace discovery
exports.getPublicLocations = async (req, res) => {
try {
const { lat, long, radius, type } = req.query;
const geofence = {
lat: parseFloat(lat),
long: parseFloat(long),
radius: parseFloat(radius)
};
if (isNaN(geofence.lat) || isNaN(geofence.long) || isNaN(geofence.radius)) {
throw new Error('Invalid geofence parameters');
const geofence = { lat: number(req.query.lat), long: number(req.query.long), radius: number(req.query.radius) };
try { validateCoords(geofence.lat, geofence.long); } catch (err) { throw invalid(err.message); }
if (geofence.radius < 0) throw invalid('Radius must be non-negative kilometres');
const type = req.query.type;
if (type !== undefined && !allowedTypes.includes(type)) throw invalid('Invalid location type');
const scope = JSON.stringify({ ...geofence, type: type || null });
let start;
if (req.query.cursor !== undefined) {
try { start = decodeCursor(req.query.cursor, scope); } catch { throw invalid('Invalid location cursor'); }
}

let users = [];
if (User.find) {
const query = { locationPrivacy: true };
if (type) query.locationType = type;
users = await User.find(query, '-password');
}

const filtered = users.filter(u => u.coordinates && isWithinGeofence(u.coordinates, geofence));
res.json(filtered);
} catch (err) {
res.status(400).json({ error: err.message });
}
const page = await docClient.scan({ TableName: LOCATION_TABLE_NAME, Limit: 100, ConsistentRead: true,
ProjectionExpression: 'id, coordinates, locationType, privacy',
...(start ? { ExclusiveStartKey: start } : {}) }).promise();
// Do not rely on client-supplied filters or include complete user records.
const found = (page.Items || []).filter(item => {
if (item.privacy !== true || (type && item.locationType !== type)) return false;
try { validateCoords(item.coordinates?.lat, item.coordinates?.long); } catch { return false; }
return isWithinGeofence(item.coordinates, geofence);
}).map(publicLocation);
if (page.LastEvaluatedKey) res.set('X-Next-Cursor', encodeCursor(page.LastEvaluatedKey, scope));
res.json(found);
} catch (err) { fail(res, err); }
};
12 changes: 12 additions & 0 deletions backend/middleware/requestRateLimit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { rateLimit, ipKeyGenerator } = require('express-rate-limit');

// req.user is populated by authentication middleware, never by request data.
module.exports = () => rateLimit({
windowMs: 60 * 1000,
limit: 1000,
keyGenerator: req => typeof req.user?.userId === 'string' && req.user.userId
? `account:${req.user.userId}` : `ip:${ipKeyGenerator(req.ip)}`,
standardHeaders: 'draft-6',
legacyHeaders: false,
message: { error: 'Too many requests, please try again later' },
});
31 changes: 31 additions & 0 deletions backend/models/location.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { createHash, randomBytes, createCipheriv, createDecipheriv } = require('node:crypto');

// Separate storage keeps local coordinates out of raw Users exports/imports.
const LOCATION_TABLE_NAME = process.env.LOCATION_TABLE_NAME || 'Locations';
function publicLocation(item) {
return { id: item.id, coordinates: { lat: item.coordinates.lat, long: item.coordinates.long },
...(item.locationType === undefined ? {} : { locationType: item.locationType }) };
}
function key() {
if (!process.env.JWT_SECRET) throw new Error('JWT_SECRET is required');
return createHash('sha256').update('agrinet-location-cursor-v1\0').update(process.env.JWT_SECRET).digest();
}
// Encrypt the scan key: it may belong to a private location, even on an empty page.
function encodeCursor(start, scope) {
const iv = randomBytes(12);
const cipher = createCipheriv('aes-256-gcm', key(), iv);
cipher.setAAD(Buffer.from(scope));
const data = Buffer.concat([cipher.update(JSON.stringify({ start, expires: Date.now() + 3600000 })), cipher.final()]);
return Buffer.concat([iv, cipher.getAuthTag(), data]).toString('base64url');
}
function decodeCursor(token, scope) {
if (typeof token !== 'string' || token.length > 2048 || !/^[\w-]+$/.test(token)) throw new Error('Invalid cursor');
const bytes = Buffer.from(token, 'base64url');
const cipher = createDecipheriv('aes-256-gcm', key(), bytes.subarray(0, 12));
cipher.setAAD(Buffer.from(scope));
cipher.setAuthTag(bytes.subarray(12, 28));
const result = JSON.parse(Buffer.concat([cipher.update(bytes.subarray(28)), cipher.final()]).toString());
if (!Number.isFinite(result.expires) || result.expires < Date.now() || typeof result.start?.id !== 'string') throw new Error('Expired cursor');
return { id: result.start.id };
}
module.exports = { LOCATION_TABLE_NAME, publicLocation, encodeCursor, decodeCursor };
Loading
Loading