【问题标题】:How to pass sameSite: none from Dockerized Node app running on Azure AppService?如何通过在 Azure AppService 上运行的 Dockerized Node 应用程序中的 sameSite: none?
【发布时间】:2020-09-10 14:24:29
【问题描述】:

我有几个 Node 应用程序在 Azure AppServices 上托管的 Docker 容器中运行。

我试图弄清楚如何处理 SameSite cookie 问题,但要做到这一点,我需要 https。目前,Node/Express 应用程序在容器内运行 http,然后 Azure 将我们的证书附加到 AppService 以运行 https。

我们正在使用带有以下选项的 cookie-session:

app.use(
  cookieSession(
    {
      name: 'session',
      keys: [ '123456789ABCD' ],
      // Cookie Options
      maxAge:   24 * 60 * 60 * 1000,  // 24 hours
      httpOnly: true,
      sameSite: 'none',
      secure:   true
    }
  )
)

sameSite: 'none'secure: true 是新的,但每当我部署 secure: true 时都会炸毁我的应用程序,因为我没有在容器内运行 http,所以 cookie 会被剥离。

我也试过app.set('trust proxy', 1),但不知道有什么影响。

Dockerfile:

# Create a container image for the app
FROM node:erbium-alpine

# Allow NODE_ENV to be set to different values
# depending on the image build/deployment environment
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

# Create app directory
WORKDIR /usr/app

# Copy package.json and yarn.lock into app directory
COPY ./package.json ./
COPY ./yarn.lock ./

# Install backend NPM modules
RUN yarn install

# COPY the client package and yarn files into app
COPY ./client/package.json ./client/
COPY ./client/yarn.lock ./client/

# Install client NPM modules
RUN yarn run install:client

# COPY the rest of the files into app directory
COPY ./ ./

# build the client
RUN yarn run build:client

# Expose ports for accessing the app
EXPOSE 5000 80

# Launch the Express server
CMD ["node", "server.js"]

考虑到这种部署方案,有什么好的方法?我是在容器中添加一些自签名证书,还是只是在某处缺少设置?

然后在我为 Azure 修复此问题后,如何在开发中运行它?

【问题讨论】:

    标签: node.js azure docker https samesite


    【解决方案1】:

    在 AppService 中,SSL 终止发生在网络负载平衡器上,因此所有 HTTPS 请求都作为未加密的 HTTP 请求到达您的应用程序。但是,SameSite=None 必须是 secure,即cookie 只能通过 HTTPS 发送。如果 secure 设置为 true 并且 Node.js 不像这种情况下直接通过 TLS 连接,则需要正确设置受信任的代理,以便传递 X-Forwarded-* 标头并正确设置 cookie。在您的快速应用引导期间设置以下内容。

    app.set('trust proxy', 2);
    

    您提到您尝试过app.set('trust proxy', 1),这意味着它仅适用于第一跳。在这种情况下,除了网络负载均衡器之外,我们需要 2 个跃点来包含容器的入口。详情请参考this

    然后在我为 Azure 修复此问题后,如何在开发中运行它?

    如果设置app.set('trust proxy', 2);,相信本地调试不需要任何特殊处理。

    【讨论】:

    • Krishnendu,我尝试将 trust proxy 设置为 true,但应用程序仍然无法设置我的 cookie。您还有其他建议吗?
    • 你能试试app.set('trust proxy', 2);(第二跳),然后是3等等..(你说你已经用1试过了)
    • 感谢 Krishnendu。 app.set('trust proxy', 2) 为我工作。
    猜你喜欢
    • 1970-01-01
    • 2021-11-25
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2022-12-15
    • 2019-08-10
    • 2018-04-15
    相关资源
    最近更新 更多