grafana/k6

Deny setting operations on __ENV variables from JS code

Open

#1,722 建立於 2020年11月13日

在 GitHub 查看
 (6 留言) (2 反應) (0 負責人)Go (1,537 fork)batch import
enhancementgood first issuejs-compatux

倉庫指標

Star
 (30,564 star)
PR 合併指標
 (平均合併 11天 22小時) (30 天內合併 44 個 PR)

描述

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.

貢獻者指南