grafana/k6

Deny setting operations on __ENV variables from JS code

Open

#1,722 opened on Nov 13, 2020

View on GitHub
 (6 comments) (2 reactions) (0 assignees)Go (30,564 stars) (1,537 forks)batch import
enhancementgood first issuejs-compatux

Description

Update — the current agreement from the comments:

https://github.com/grafana/k6/issues/1722#issuecomment-796752500

So, yeah, I think we should either close this issue and https://github.com/grafana/k6/pull/1877, or "fix" it by freezing __ENV so any attempts to change it won't silently fail, but would throw an exception. The script pattern could be used for having default values of script options, instead of modifying __ENV:

const myScriptOption = (typeof __ENV.something !== 'undefined') ? __ENV.something : 'defaultValue';

This has been broken for a while, but very few people have complained and there's an easy workaround shown above, so my vote is for "fixing" the current behavior in the sense of making it permanent and explicit by freezing __ENV 😅

if (typeof __ENV.something == "undefined")  {
 __ENV.something= "test";
}
export default function () {
    console.log(__ENV.something);
}

This code should print "test" and it did up to https://github.com/loadimpact/k6/commit/e1a5c0af001977b6a10945791f5b16288937d391 and if the golang version is go1.13.8 but not if it is 1.15.3 (or 1.14.6) image image

This is fairly common pattern to fix your __ENV variables in cases where you didn't provide them but you use them through the script.

Contributor guide