0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

OCI API Gateway で発生するSet-Cookieヘッダーのドメイン属性上書き問題とその解決

Last updated at Posted at 2025-03-24

はじめに

OCIのAPI Gatewayを利用して、バックエンドにOCI Functionsを設定し、クライアントにSet-Cookieヘッダーを返す際に、意図しないドメイン属性への変更が発生する問題に直面しました。
本記事では、問題の切り分け、調査過程、最終的な結論をまとめます。

問題の概要

API Gatewayにはsub.example.comというドメインを設定しています。
そのAPI Gatewayのバックエンドに設定したOCI Functionsで、Set-Cookie ヘッダーを利用してクライアントへクッキーを送信した際、ドメイン属性(Domain)を「example.com」に設定しても、レスポンスヘッダーでは「sub.example.com」に上書きされる問題が発生しました。

以下のようなコードで Set-Cookie を設定しています。

import io
import json
import secrets

from fdk.response import Response
from fdk.context import InvokeContext

def handler(ctx: InvokeContext, data: io.BytesIO = None) -> Response:

    value = secrets.token_hex(32)

    return Response(
        ctx, response_data=json.dumps(
                {
                    "code": 200,
                    "testCookie": value
                }
        ),
        headers={
            "Content-Type": "application/json",
            "Set-Cookie": f"testCookie={value}; Secure; HttpOnly; Domain=example.com; Path=/; Max-Age=2592000; SameSite=None"
        },
        status_code=200
    )

しかし、実際のレスポンスヘッダーでは Domain=sub.example.com に書き換えられてしまいます。

問題の切り分け

1.ドメイン属性の指定

  • Domain=example.com を指定 → sub.example.com に上書き
  • Domain=.example.com を指定 → sub.example.com に上書き
  • Domain を指定しない → Set-Cookie に Domain 属性が含まれない。セットされるCookieのドメイン属性はsub.example.comとなる。

2.CORS の影響

OCI サポートから「CORS ポリシーが影響している可能性がある」との指摘を受けたため、一時的に CORS ポリシーを無効化して動作を確認しました。

しかし、CORS ポリシーを無効にしても、Set-Cookie の Domain が sub.example.com に上書きされる挙動は変わりませんでした。

3.API Gateway の仕様かバグか

再度 OCI サポートに問い合わせたところ、「現在の挙動はバグであり、修正対応中」との回答を得ました。

まとめ

OCI API Gateway で Set-Cookie ヘッダーのドメイン属性が上書きされる問題について、調査を進めた結果、OCI 側のバグであることが判明しました。

最終的には OCIサポートの対応により迅速に修正がされ、意図した Domain 属性でクッキーをセットできるようになりました。

この問題を通じて、OCI API Gateway の挙動に関する理解が深まり、また、公式サポートへの問い合わせの重要性を再認識しました。

同様の問題に直面した際には、まず API Gateway の仕様を確認し、必要に応じてサポートに問い合わせることが有効です。

本記事が、同じ問題に悩む方の参考になれば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?