hedera-json-rpc-relay icon indicating copy to clipboard operation
hedera-json-rpc-relay copied to clipboard

feat: capture env variables into sigleton and reusable constants

Open natanasow opened this issue 5 months ago • 5 comments

Description:

Currently, we are dynamically getting the environment variables and parsing/casting them every time. This is inefficient and also, more importantly, allows env configurations to be dynamically changed, which can modify the behavior of the relay based on the current values of the env vars, which can be a potential exploit.

Solution:

We should change this to only capture them once when the relay is initialized and reuse those as constants

Related issue(s):

Fixes #2686

Notes for reviewer:

  • created a new package that holds the env-provider singleton
  • the env-provider package is added as a dependency to relay, server, and ws-server
  • in the perfect world, the env-provider must be immutable and should not provide an interface for overriding or extending once-loaded environment variables but there are many tests where we dynamically override envs in order to test specific cases so we should add these interfaces as well
  • the main review should be applied to the env-provider package
  • almost all of the changes are just replaced values from process.env.<VAR_NAME> to EnvProviderService.getInstance().get(<VAR_NAME>)
  • in the test cases if overrides are needed, they are done like that - process.env.CHAIN_ID = '0x160c' is transformed to EnvProviderService.getInstance().dynamicOverride('CHAIN_ID', '0x160c')

Checklist

  • [ ] Documented (Code comments, README, etc.)
  • [ ] Tested (unit, integration, etc.)

natanasow avatar Sep 10 '24 11:09 natanasow