Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: response-rewrite plugin fine-grained cookie control #7943

Open
LiteSun opened this issue Sep 19, 2022 · 2 comments
Open

feat: response-rewrite plugin fine-grained cookie control #7943

LiteSun opened this issue Sep 19, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@LiteSun
Copy link
Member

LiteSun commented Sep 19, 2022

Description

The response-rewrite plugin headers feature is currently overridden, resulting in cookies returned upstream being overridden by APISIX.

The add function in the last version of the response-rewrite plugin can also only append the new header to the upstream header.

We need to have more granular control over cookies to meet different user scenarios.

For example, parsing the key-value structure in the upstream set-cookie so that only the value of the key with the same name is overwritten and the upstream set-cookie is retained.

After talking to @spacewander, the following changes can be made to our plugin.

We can add a new cookie configuration to the response-rewrite plugin to allow cookies to be modified.

The configuration is as follows.
cookies = {
    properties = {
        set = {
            type = "object",
            minProperties = 1,
            patternProperties = {
                ["^.*$"] = {
                    properties = {
                        value = {type = "string"},
                        samesite = {enum = {"None", "Lax", "Strict"}},
                        ...
                    },
                }
            },
        },
        remove = {
            type = "array",
            minItems = 1,
            items = {
                type = "string",
                minLength = 1,
            }
        },
    },
},

The set operation modifies or adds a cookie with the given name, where value is the value of the cookie and parameters such as SameSite are additional options.
The remove operation removes the cookie with the given name.
The order of execution is set followed by remove.

For example, the following configuration would set the cookie named id to a3fWa and delete the cookie idx.
"plugins": {
    "response-rewrite": {
        "cookies": {
            "set": {
                "id": {
                    "value": "a3fWa",
                    "samesite": "Lax"
                }
            },
            "remove": [
                "idx"
            ]
        }
    }
},

The cookie manipulation will take place after the header modification, so the original header configuration is not affected.

We can implement the cookie modification function based on the following PR.
https://github.com/cloudflare/lua-resty-cookie/pull/35/files

To delete a cookie, you need to get the current Set-Cookie header, remove the one to be removed and set it back.

Thanks for @spacewander 's support.

Feel free to comment here 😊

@LiteSun LiteSun added the enhancement New feature or request label Sep 19, 2022
@tokers
Copy link
Contributor

tokers commented Sep 20, 2022

For example, parsing the key-value structure in the upstream set-cookie so that only the value of the key with the same name is overwritten and the upstream cookie is retained.

This sentence confuses me. What's the point to mention the upstream cookie? Cookie is a request header, but what you were describing is the response rewrite plugin.

@LiteSun
Copy link
Member Author

LiteSun commented Sep 22, 2022

For example, parsing the key-value structure in the upstream set-cookie so that only the value of the key with the same name is overwritten and the upstream cookie is retained.

This sentence confuses me. What's the point to mention the upstream cookie? Cookie is a request header, but what you were describing is the response rewrite plugin.

oh, it should be ..... the upstream set-cookie is retained

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants