Intelligence, Advanced Charting & Trading Platform
test(history): validate Neon read-only query, bounds, cache, and redaction
davoodmehraban89 committed
Oct 11, 2026 at 23:14 UTC
c4be8fac931553d1538f5a24f8f4dcca64b8a221
1 file changed
+52
deploy/chartnova/history-direct.test.mjs
new
+52
@@ -0,0 +1,52 @@
1
+import test from "node:test";
2
+import assert from "node:assert/strict";
3
+import worker from "./worker.mjs";
4
+const today="2026-10-11";
5
+const secret="postgresql://reader:fakepassword@ep-test.aws.neon.tech/neondb?sslmode=require";
6
+function env(){const kv=new Map();return {CHARTNOVA_HISTORY_DATABASE_URL:secret,MARKET_CACHE:{get:async(k,t)=>{const v=kv.get(k);return t==="json"&&v?JSON.parse(v):v??null},put:async(k,v)=>kv.set(k,v)}}}
7
+function request(query){return new Request("https://app.chartnova.ir/api/history"+query)}
8
+const p="EUR_USD_ECB";
9
+test("invalid dates, future dates, unknown fields, oversized limits and symbols are rejected before reading database",async()=>{
10
+ const e=env();
11
+ for(const suffix of ["?from=2026-02-30","?from=2026-10-10&to=2025-10-11","?from=2020-01-01&to=2026-10-11","?limit=501","?limit=0","?limit=1.5","?to=2099-01-01","?foo=bar","?symbol=../../etc/passwd"]){
12
+ const response=await worker.fetch(request(suffix),e);
13
+ assert.equal(response.status,400,suffix);
14
+ }
15
+});
16
+test("readonly direct Neon query is parameterized, bounded and never discloses the credential",async()=>{
17
+ const e=env(),nativeFetch=globalThis.fetch;let calls=0;
18
+ globalThis.fetch=async(url,init)=>{
19
+ calls++;
20
+ assert.equal(url,"https://ep-test.aws.neon.tech/sql");
21
+ assert.equal(init.headers["Neon-Connection-String"],secret);
22
+ const body=JSON.parse(init.body);
23
+ assert.match(body.query,/public\.chartnova_history_public/);
24
+ assert.match(body.query,/LIMIT \$4::integer/);
25
+ assert.deepEqual(body.params,[p,"2026-10-08T00:00:00Z","2026-10-11T00:00:00.000Z",2]);
26
+ return Response.json({rows:[
27
+ {symbol:p,quote_asset:"USD",source:"ecb_exr",observation_at:"2026-10-09T23:59:59Z",price:"1.1206",quality:"GOOD",flags:{data_kind:"daily_reference_point_not_ohlc",observation_date:"2026-10-09"}},
28
+ {symbol:p,quote_asset:"USD",source:"ecb_exr",observation_at:"2026-10-08T23:59:59Z",price:"1.1208",quality:"GOOD",flags:{data_kind:"daily_reference_point_not_ohlc",observation_date:"2026-10-08"}}
29
+ ]});
30
+ };
31
+ try{
32
+ const r=await worker.fetch(request("?symbol=EUR_USD_ECB&from=2026-10-08&to=2026-10-10&limit=2"),e);
33
+ assert.equal(r.status,200);
34
+ const d=await r.json();
35
+ assert.equal(d.count,2);assert.equal(d.dataKind,"daily_reference_point_not_ohlc");
36
+ assert.deepEqual(d.series.map(x=>x.date),["2026-10-08","2026-10-09"]);
37
+ assert(!JSON.stringify(d).includes("fakepassword"));assert(!JSON.stringify(d).includes("postgresql://"));
38
+ assert.equal(calls,1);
39
+ const again=await worker.fetch(request("?symbol=EUR_USD_ECB&from=2026-10-08&to=2026-10-10&limit=2"),e);
40
+ assert.equal(again.status,200);assert.equal(calls,1,"cached API must not refetch Postgres on repeat");
41
+ }finally{globalThis.fetch=nativeFetch}
42
+});
43
+test("database error fails closed without returning a secret",async()=>{
44
+ const nativeFetch=globalThis.fetch;
45
+ globalThis.fetch=async()=>new Response("connection failed", {status:503});
46
+ try{
47
+ const response=await worker.fetch(request("?symbol=EUR_USD_ECB&from=2026-10-08&to=2026-10-10"),env());
48
+ assert.equal(response.status,503);
49
+ const body=await response.text();
50
+ assert(!body.includes("fakepassword"));assert(!body.includes("postgresql://"));
51
+ }finally{globalThis.fetch=nativeFetch}
52
+});