Skip to content

Commit 7c98128

Browse files
committed
Provide guidance for Set-Cookie
The Set-Cookie response header breaks the normal rules for headers with multiple values and requires special handling.
1 parent 2c7432c commit 7c98128

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/oas.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2524,6 +2524,72 @@ components:
25242524
$ref: '#/components/mediaTypes/CollectionLinks'
25252525
```
25262526
2527+
##### Representing the `Set-Cookie` Header
2528+
2529+
As noted in [[!RFC9110]] [Section 5.3](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.3) the `Set-Cookie` response header violates the requirements for representing multiple values as a comma-separated list, as `style: "simple"` produces.
2530+
2531+
```http
2532+
Set-Cookie: lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2533+
Set-Cookie: foo=bar; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2534+
```
2535+
2536+
If these values were to be place on a single line using `style: "simple"`, the result would be `lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT,foo=bar; Expires=Wed, 09 Jun 2021 10:18:14 GMT`, which when split would then produce four values: `lang=en-US; Expires=Wed`, `09 Jun 2021 10:18:14 GMT`, `foo=bar; Expires=Wed`, and `09 Jun 2021 10:18:14 GMT`.
2537+
While the two dates (`09...`) are not valid `Set-Cookie` values on their own, [[?RFC6265]] does not provide any guarantee that all such embedded uses of commas will produce detectable errors when split in this way.
2538+
2539+
RFC9110 therefore advises recipients to 'handle "Set-Cookie" as a special case while processing fields,' so the OAS similarly special-cases its handling of `Set-Cookie` as follows:
2540+
2541+
For the `Set-Cookie` response header _**only**_, `style: "simple"` MUST be treated as producing a newline-delimited list instead of a comma-separated list, with each line corresponding to the value of a single `Set-Cookie:` header field.
2542+
This newline-delimited format MUST be used whenever a string representing the values is required, including in the [Example Object's](#example-object) serialized example fields, and when using `content` with a `text/plain` [Media Type Object](#media-type-object) as is necessary to prevent percent-encoding whitespace.
2543+
2544+
The following example shows two different ways to describe `Set-Cookie` headers that require cookies named `"lang"` and `"foo"`. The first uses `content` to preserve the necessary whitespace in the `"Expires"` cookie attribute, while the second shows the use of `style: "simple"` and forbids whitespace to ensure that values work with this serialization approach:
2545+
2546+
```yaml
2547+
components:
2548+
headers:
2549+
SetCookieWithExpires:
2550+
# Spaces within the Expires values prevent the use of `schema` and
2551+
# `style` as they would be percent-encoded, even with `allowReserved`.
2552+
content:
2553+
text/plain:
2554+
schema:
2555+
type: string
2556+
allOf:
2557+
- pattern: "^lang=[^;];.*Expires="
2558+
- pattern: "^foo=[^;];.*Expires="
2559+
examples:
2560+
WithExpires:
2561+
# This demonstrates that the text is required to be provided
2562+
# in the final format, and is not changed by serialization.
2563+
# In practice, it is not necessary to show both fields here.
2564+
dataValue: |
2565+
lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2566+
foo=bar; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2567+
serializedValue: |
2568+
lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2569+
foo=bar; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2570+
SetCookieWithNoSpaces:
2571+
schema:
2572+
type: object
2573+
required:
2574+
- lang
2575+
- foo
2576+
additionalProperties:
2577+
type: string
2578+
pattern: "^[^[:space:]]$"
2579+
style: simple
2580+
explode: true
2581+
allowReserved: true # "=", ";", and " " are reserved
2582+
examples:
2583+
SetCookies:
2584+
dataValue: {
2585+
"lang": "en-US",
2586+
"foo": "bar"
2587+
}
2588+
serializedValue: |
2589+
lang=en-US
2590+
foo=bar
2591+
```
2592+
25272593
##### Header Object Example
25282594
25292595
A simple header of type `integer`:

0 commit comments

Comments
 (0)