Azure Function 于 2022 年 4 月 26 日禁用将“用户”字段设置为 HTTP 请求对象

Far*_*dov 5 node.js azure-functions azure-functions-runtime

(!) 问题无法在本地重现。
Azure 函数版本 ~4
节点版本 14.18.1

创建一个简单的 HTTP 触发的 Azure Function 并设置两个简单的属性,我们得到以下代码:

module.exports = async function (context, req) {

    req.auth = {authField: 'some value'}
    req.user = {userField: 'some value'}
    context.log(`${JSON.stringify(req,null,2)}`);

    context.res = {
        body: 'responseMessage'
    };
}
Run Code Online (Sandbox Code Playgroud)

记录器打印以下对象:

{
  "method": "POST",
  "url": "xxx",
  "originalUrl": "xxx",
  "headers": {
    /// ...
  },
  "query": {},
  "params": {},
  "body": { "name": "Azure" },
  "rawBody": "{\"name\":\"Azure\"}",
  "auth": { "authField": "some value" }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,只有authset 而不是 set user。在版本中可以看到相同的失败行为4.2.0

当我使用 Azure Function ~3 进行测试时,输出如下所示:

{
  "method": "POST",
  "url": "xxx",
  "originalUrl": "xxx",
  "headers": {
    // ...
  },
  "query": {},
  "params": {},
  "body": { "name": "Azure" },
  "rawBody": "{\"name\":\"Azure\"}",
  "auth": { "authField": "some value" },
  "user": { "userField": "some value" }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,该字段已设置。以下自定义 v44.1.0-17156也设置该user字段。

我们通过express-jwt (v6.1.0)user使用该字段,当没有为 提供值时,它会使用它。requestProperty

我还无法重现它,但在从 Typescript 项目中转译出来时,我们收到以下运行时错误:

FailureException: Cannot set property user of [object Object] which has only a getterStack: TypeError: Cannot set property user of [object Object] which has only a getterat Object.run
Run Code Online (Sandbox Code Playgroud)

该问题始于 2022 年 4 月 26 日。

问题:

  • 是什么原因?
  • 是否可以快速回滚到正常运行的 Azure Function 自定义版本?

Far*_*dov 5

我在这个PR中找到了罪魁祸首,它是为 Azure Function 运行时 v4.2.0 添加的。

用户字段仅添加了一个 getter。
我们获取了代码并制作了最小的示例:

class Request {
    #cachedUser?: string | null;
    constructor() {
    }
    get user(): string | null {
        if (this.#cachedUser === undefined) {
            this.#cachedUser = "some value";
        }
        return this.#cachedUser;
    }
}
Run Code Online (Sandbox Code Playgroud)

并得到以下转译版本:

class Request {
    #cachedUser?: string | null;
    constructor() {
    }
    get user(): string | null {
        if (this.#cachedUser === undefined) {
            this.#cachedUser = "some value";
        }
        return this.#cachedUser;
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,它总是返回初始值,在我们的示例中是“某个值”,但在原始代码中很简单undefined并且不允许设置它。