Skip to content

Commit c4b51fa

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 22fbdc9 commit c4b51fa

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
@@ -2451,6 +2451,72 @@ Using `content` with a `text/plain` media type is RECOMMENDED for headers where
24512451
| ---- | :----: | ---- |
24522452
| <a name="header-content"></a>content | Map[`string`, [Media Type Object](#media-type-object)] | A map containing the representations for the header. The key is the media type and the value describes it. The map MUST only contain one entry. |
24532453

2454+
##### Representing the `Set-Cookie` Header
2455+
2456+
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.
2457+
2458+
```http
2459+
Set-Cookie: lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2460+
Set-Cookie: foo=bar; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2461+
```
2462+
2463+
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`.
2464+
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.
2465+
2466+
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:
2467+
2468+
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.
2469+
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.
2470+
2471+
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:
2472+
2473+
```yaml
2474+
components:
2475+
headers:
2476+
SetCookieWithExpires:
2477+
# Spaces within the Expires values prevent the use of `schema` and
2478+
# `style` as they would be percent-encoded, even with `allowReserved`.
2479+
content:
2480+
text/plain:
2481+
schema:
2482+
type: string
2483+
allOf:
2484+
- pattern: "^lang=[^;];.*Expires="
2485+
- pattern: "^foo=[^;];.*Expires="
2486+
examples:
2487+
WithExpires:
2488+
# This demonstrates that the text is required to be provided
2489+
# in the final format, and is not changed by serialization.
2490+
# In practice, it is not necessary to show both fields here.
2491+
dataValue: |
2492+
lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2493+
foo=bar; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2494+
serializedValue: |
2495+
lang=en-US; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2496+
foo=bar; Expires=Wed, 09 Jun 2021 10:18:14 GMT
2497+
SetCookieWithNoSpaces:
2498+
schema:
2499+
type: object
2500+
required:
2501+
- lang
2502+
- foo
2503+
additionalProperties:
2504+
type: string
2505+
pattern: "^[^[:space:]]$"
2506+
style: simple
2507+
explode: true
2508+
allowReserved: true # "=", ";", and " " are reserved
2509+
examples:
2510+
SetCookies:
2511+
dataValue: {
2512+
"lang": "en-US",
2513+
"foo": "bar"
2514+
}
2515+
serializedValue: |
2516+
lang=en-US
2517+
foo=bar
2518+
```
2519+
24542520
##### Header Object Example
24552521
24562522
A simple header of type `integer`:

0 commit comments

Comments
 (0)