Intelligence, Advanced Charting & Trading Platform
test(history): protect KV snapshots from data leakage and reject invalid query parameters
davoodmehraban89 committed
Oct 11, 2026 at 23:53 UTC
93e4809088d4b679c983d2044329d74e65ab6b37
1 file changed
+39
deploy/chartnova/history-security.test.mjs
new
+39
@@ -0,0 +1,39 @@
1
+import test from "node:test";
2
+import assert from "node:assert/strict";
3
+import worker from "./worker.mjs";
4
+const url=(q)=>new Request("https://app.chartnova.ir/api/history"+q);
5
+function fixtureEnv(){
6
+ const snapshot={
7
+ ok:true,symbol:"EUR_USD_ECB",dataKind:"daily_reference_point_not_ohlc",source:"ECB",
8
+ quoteCurrency:"USD",fetchedAt:"2026-10-11T12:00:00Z",
9
+ internalCredential:"postgresql://do-not-expose",qualitySource:"do-not-expose",
10
+ series:[{date:"2026-10-09",close:1.12,quality:"GOOD",internalCredential:"do-not-expose"},{date:"2026-10-08",close:1.11,quality:"GOOD"}]
11
+ };
12
+ // Correct ascending ordering as required for a valid series.
13
+ snapshot.series.reverse();
14
+ const kv=new Map([["history:ecb:EUR_USD_ECB:1y",JSON.stringify(snapshot)]]);
15
+ return {MARKET_CACHE:{get:async(k,format)=>format==="json"?JSON.parse(kv.get(k)??"null"):kv.get(k)??null}};
16
+}
17
+test("history response never leaks unknown fields from KV snapshots",async()=>{
18
+ const r=await worker.fetch(url("?symbol=EUR_USD_ECB&from=2026-10-08&to=2026-10-10"),fixtureEnv());
19
+ assert.equal(r.status,200);
20
+ const body=await r.text();
21
+ assert(!body.includes("internalCredential"));
22
+ assert(!body.includes("do-not-expose"));
23
+ assert(!body.includes("postgresql://"));
24
+ const json=JSON.parse(body);
25
+ assert.equal(json.count,2);
26
+ assert.equal(json.dataKind,"daily_reference_point_not_ohlc");
27
+ assert.deepEqual(Object.keys(json.series[0]).sort(),["close","date","quality"]);
28
+});
29
+test("duplicated or empty query parameters rejected instead of silently ignored",async()=>{
30
+ for(const q of ["?symbol=EUR_USD_ECB&symbol=EUR_GBP_ECB","?symbol=","?from=","?to=","?limit=1&limit=2"]){
31
+ const r=await worker.fetch(url(q),fixtureEnv());
32
+ assert.equal(r.status,400,q);
33
+ }
34
+});
35
+test("empty date window is not misreported as a valid historical series",async()=>{
36
+ const r=await worker.fetch(url("?symbol=EUR_USD_ECB&from=2026-07-01&to=2026-07-03"),fixtureEnv());
37
+ assert.equal(r.status,503);
38
+ assert.equal((await r.json()).ok,false);
39
+});