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

Allow custom path for preview mode cookies #38313

Merged
merged 8 commits into from Aug 8, 2022

Conversation

lucasassisrosa
Copy link
Contributor

@lucasassisrosa lucasassisrosa commented Jul 5, 2022

Fixes #38312

Bug

  • Related issues linked using fixes #number
  • Integration tests added
  • Errors have helpful link attached, see contributing.md

Feature

  • Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
  • Related issues linked using fixes #number
  • Integration tests added
  • Documentation added
  • Telemetry added. In case of a feature if it's used or not.
  • Errors have helpful link attached, see contributing.md

Documentation / Examples

  • Make sure the linting passes by running pnpm lint
  • The examples guidelines are followed from our contributing doc

Comment on lines +517 to +519
...(options.path !== undefined
? ({ path: options.path } as CookieSerializeOptions)
: undefined),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also considering doing something like

Suggested change
...(options.path !== undefined
? ({ path: options.path } as CookieSerializeOptions)
: undefined),
...(options.path !== undefined
? ({ path: options.path || '/' } as CookieSerializeOptions)
: undefined),

but I found out that there's a normalization function for the cookie serialize function params.

Also, I though about refactoring this maxAge and path props to have a single object like

Suggested change
...(options.path !== undefined
? ({ path: options.path } as CookieSerializeOptions)
: undefined),
...({ path: options.path , maxAge: options.maxAge }),

But when path or maxAge is empty, we'd be passing { path: undefined, maxAge: undefined } instead of { }. Those are actually different, and I'm not certain how the options object properties are treated down the chain

...(options.maxAge !== undefined
? ({ maxAge: options.maxAge } as CookieSerializeOptions)
: undefined),
...(options.path !== undefined
? ({ path: options.path } as CookieSerializeOptions)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While checking CookieSerializeOptions type definitions, I noticed that it'd be good to update it to v0.5.1 to get the latest (and more complete definitions). /@types/cookie/0.5.1 has correct links to https://datatracker.ietf.org/doc/html/rfc6265#section-5.2.4 which is way more helpful to understand how this path and maxAge behave. I thought about doing it in the same PR, but I think it's rather better to keep deps updates isolated

@@ -461,6 +461,7 @@ function setPreviewData<T>(
data: object | string, // TODO: strict runtime type checking
options: {
maxAge?: number
path?: string
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it'd be good enough to just accept raw path as string in this setPreviewData function here without extra parsing/validation on it because developers would control the setPreviewData call and its parameters, just like they do with redirects inside the preview API endpoint. The security concerns around path remain the same though - https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.2.4

Copy link
Member

@ijjk ijjk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, being able to enable preview mode for specific paths sounds like a good addition, thanks for the PR!

@ijjk ijjk merged commit a5029e9 into vercel:canary Aug 8, 2022
ijjk added a commit that referenced this pull request Sep 5, 2022
As updated in #38313 this ensures the `path` option can also be passed to `clearPreviewData` to properly clear the preview cookies. 

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

Fixes: #39853
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable Preview Mode by Path
2 participants