|
| 1 | +import { expect } from "chai"; |
| 2 | +import sinon from "sinon"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import os from "node:os"; |
| 5 | +import path from "node:path"; |
| 6 | +import http from "node:http"; |
| 7 | +import type { AddressInfo } from "node:net"; |
| 8 | +import { once } from "node:events"; |
| 9 | +import { brotliCompressSync } from "node:zlib"; |
| 10 | +import { ConsensusClient, ExecutionClient, Network } from "@stakingbrain/common"; |
| 11 | +import { startBrainApi } from "../../../src/modules/apiServers/brain/startBrainApi.js"; |
| 12 | +import { startLaunchpadApi } from "../../../src/modules/apiServers/launchpad/startLaunchpadApi.js"; |
| 13 | +import { startUiServer } from "../../../src/modules/apiServers/ui/startUiServer.js"; |
| 14 | +import { |
| 15 | + BeaconchainApi, |
| 16 | + BlockExplorerApi, |
| 17 | + PostgresClient, |
| 18 | + ValidatorApi, |
| 19 | + Web3SignerApi |
| 20 | +} from "../../../src/modules/apiClients/index.js"; |
| 21 | +import { BrainDataBase } from "../../../src/modules/db/index.js"; |
| 22 | +import { CronJob } from "../../../src/modules/cron/cron.js"; |
| 23 | +import { params } from "../../../src/params.js"; |
| 24 | + |
| 25 | +function postJson(server: http.Server, body: Buffer, encoding?: string): Promise<number> { |
| 26 | + const address = server.address() as AddressInfo; |
| 27 | + return new Promise((resolve, reject) => { |
| 28 | + const request = http.request( |
| 29 | + { |
| 30 | + hostname: "127.0.0.1", |
| 31 | + port: address.port, |
| 32 | + path: "/", |
| 33 | + method: "POST", |
| 34 | + headers: { |
| 35 | + "Content-Type": "application/json", |
| 36 | + "Content-Length": body.length, |
| 37 | + ...(encoding ? { "Content-Encoding": encoding } : {}) |
| 38 | + } |
| 39 | + }, |
| 40 | + (response) => { |
| 41 | + response.resume(); |
| 42 | + response.on("end", () => resolve(response.statusCode!)); |
| 43 | + response.on("error", reject); |
| 44 | + } |
| 45 | + ); |
| 46 | + request.on("error", reject); |
| 47 | + request.end(body); |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +describe("JSON request content encoding", () => { |
| 52 | + const servers = new Map<string, http.Server>(); |
| 53 | + let directory: string; |
| 54 | + |
| 55 | + before(async () => { |
| 56 | + directory = fs.mkdtempSync(path.join(os.tmpdir(), "stakingbrain-json-encoding-")); |
| 57 | + fs.writeFileSync(path.join(directory, "index.html"), "<html><head></head><body></body></html>"); |
| 58 | + for (const port of ["brainPort", "launchpadPort", "uiPort"] as const) sinon.stub(params, port).value(0); |
| 59 | + const brainDb = sinon.createStubInstance(BrainDataBase); |
| 60 | + const validatorApi = sinon.createStubInstance(ValidatorApi); |
| 61 | + const signerApi = sinon.createStubInstance(Web3SignerApi); |
| 62 | + const beaconchainApi = sinon.createStubInstance(BeaconchainApi); |
| 63 | + const reloadValidatorsCronTask = sinon.createStubInstance(CronJob); |
| 64 | + const signerUrl = "http://127.0.0.1:1"; |
| 65 | + const allowedOriginsFromEnv = null; |
| 66 | + servers.set("Brain", startBrainApi({ brainDb, allowedOriginsFromEnv })); |
| 67 | + servers.set( |
| 68 | + "Launchpad", |
| 69 | + startLaunchpadApi({ |
| 70 | + brainDb, |
| 71 | + validatorApi, |
| 72 | + signerApi, |
| 73 | + beaconchainApi, |
| 74 | + reloadValidatorsCronTask, |
| 75 | + signerUrl, |
| 76 | + network: Network.Hoodi, |
| 77 | + allowedOriginsFromEnv |
| 78 | + }) |
| 79 | + ); |
| 80 | + servers.set( |
| 81 | + "UI", |
| 82 | + startUiServer({ |
| 83 | + brainDb, |
| 84 | + validatorApi, |
| 85 | + signerApi, |
| 86 | + beaconchainApi, |
| 87 | + reloadValidatorsCronTask, |
| 88 | + blockExplorerApi: sinon.createStubInstance(BlockExplorerApi), |
| 89 | + postgresClient: sinon.createStubInstance(PostgresClient), |
| 90 | + uiBuildPath: directory, |
| 91 | + allowedOriginsFromEnv, |
| 92 | + brainConfig: { |
| 93 | + chain: { |
| 94 | + network: Network.Hoodi, |
| 95 | + executionClient: ExecutionClient.Geth, |
| 96 | + consensusClient: ConsensusClient.Teku, |
| 97 | + isMevBoostSet: false, |
| 98 | + minGenesisTime: 0, |
| 99 | + secondsPerSlot: 12, |
| 100 | + slotsPerEpoch: 32 |
| 101 | + }, |
| 102 | + apis: { |
| 103 | + signerUrl, |
| 104 | + validatorUrl: signerUrl, |
| 105 | + beaconchainUrl: signerUrl, |
| 106 | + executionClientUrl: signerUrl, |
| 107 | + prometheusUrl: signerUrl, |
| 108 | + dappmanagerUrl: signerUrl, |
| 109 | + blockExplorerUrl: signerUrl, |
| 110 | + postgresUrl: signerUrl, |
| 111 | + token: "", |
| 112 | + tlsCert: null, |
| 113 | + host: "localhost", |
| 114 | + cors: null |
| 115 | + } |
| 116 | + } |
| 117 | + }) |
| 118 | + ); |
| 119 | + await Promise.all([...servers.values()].map((server) => once(server, "listening"))); |
| 120 | + }); |
| 121 | + |
| 122 | + after(async () => { |
| 123 | + await Promise.all( |
| 124 | + [...servers.values()].map( |
| 125 | + (server) => new Promise<void>((resolve, reject) => server.close((error) => (error ? reject(error) : resolve()))) |
| 126 | + ) |
| 127 | + ); |
| 128 | + sinon.restore(); |
| 129 | + fs.rmSync(directory, { recursive: true, force: true }); |
| 130 | + }); |
| 131 | + |
| 132 | + for (const name of ["Brain", "Launchpad", "UI"]) { |
| 133 | + it(`${name} rejects Brotli-compressed JSON with HTTP 415`, async () => { |
| 134 | + const body = brotliCompressSync(Buffer.from('{"probe":true}')); |
| 135 | + expect(await postJson(servers.get(name)!, body, "br")).to.equal(415); |
| 136 | + }); |
| 137 | + |
| 138 | + it(`${name} lets identity JSON reach routing`, async () => { |
| 139 | + expect(await postJson(servers.get(name)!, Buffer.from('{"probe":true}'), "identity")).to.equal(404); |
| 140 | + }); |
| 141 | + } |
| 142 | +}); |
0 commit comments