Unverified Commit 0cf3e4cc authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add missing matcher support (#42660)

This adds support for a `missing` field which is complimentary to `has`
to only match when `query`, `header`, or `cookie` values are not
present. The `missing` field can be applied for middleware matchers as
well to prevent running middleware when these fields are present.

x-ref: https://github.com/vercel/vercel/pull/8874
x-ref: [slack
thread](https://vercel.slack.com/archives/C03S8ED1DKM/p1667935428788529?thread_ts=1667850697.542269&cid=C03S8ED1DKM)
x-ref: https://github.com/vercel/next.js/discussions/25653
x-ref: https://github.com/vercel/next.js/issues/34046
x-ref: https://github.com/vercel/next.js/issues/28185
x-ref: https://github.com/vercel/next.js/issues/25930
x-ref: https://github.com/vercel/next.js/issues/24608
x-ref: https://github.com/vercel/next.js/pull/27431

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a 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 a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 1f6c0d0f
...@@ -25,6 +25,7 @@ export interface MiddlewareMatcher { ...@@ -25,6 +25,7 @@ export interface MiddlewareMatcher {
regexp: string regexp: string
locale?: false locale?: false
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
} }
export interface PageStaticInfo { export interface PageStaticInfo {
......
...@@ -178,8 +178,13 @@ export function getUtils({ ...@@ -178,8 +178,13 @@ export function getUtils({
) )
let params = matcher(parsedUrl.pathname) let params = matcher(parsedUrl.pathname)
if (rewrite.has && params) { if ((rewrite.has || rewrite.missing) && params) {
const hasParams = matchHas(req, rewrite.has, parsedUrl.query) const hasParams = matchHas(
req,
parsedUrl.query,
rewrite.has,
rewrite.missing
)
if (hasParams) { if (hasParams) {
Object.assign(params, hasParams) Object.assign(params, hasParams)
......
...@@ -24,6 +24,7 @@ export type Rewrite = { ...@@ -24,6 +24,7 @@ export type Rewrite = {
basePath?: false basePath?: false
locale?: false locale?: false
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
} }
export type Header = { export type Header = {
...@@ -32,6 +33,7 @@ export type Header = { ...@@ -32,6 +33,7 @@ export type Header = {
locale?: false locale?: false
headers: Array<{ key: string; value: string }> headers: Array<{ key: string; value: string }>
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
} }
// internal type used for validation (not user facing) // internal type used for validation (not user facing)
...@@ -41,6 +43,7 @@ export type Redirect = { ...@@ -41,6 +43,7 @@ export type Redirect = {
basePath?: false basePath?: false
locale?: false locale?: false
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
} & ( } & (
| { | {
statusCode?: never statusCode?: never
...@@ -56,6 +59,7 @@ export type Middleware = { ...@@ -56,6 +59,7 @@ export type Middleware = {
source: string source: string
locale?: false locale?: false
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
} }
const allowedHasTypes = new Set(['header', 'cookie', 'query', 'host']) const allowedHasTypes = new Set(['header', 'cookie', 'query', 'host'])
...@@ -132,8 +136,9 @@ export function checkCustomRoutes( ...@@ -132,8 +136,9 @@ export function checkCustomRoutes(
let numInvalidRoutes = 0 let numInvalidRoutes = 0
let hadInvalidStatus = false let hadInvalidStatus = false
let hadInvalidHas = false let hadInvalidHas = false
let hadInvalidMissing = false
const allowedKeys = new Set<string>(['source', 'locale', 'has']) const allowedKeys = new Set<string>(['source', 'locale', 'has', 'missing'])
if (type === 'rewrite') { if (type === 'rewrite') {
allowedKeys.add('basePath') allowedKeys.add('basePath')
...@@ -198,48 +203,65 @@ export function checkCustomRoutes( ...@@ -198,48 +203,65 @@ export function checkCustomRoutes(
invalidParts.push('`locale` must be undefined or false') invalidParts.push('`locale` must be undefined or false')
} }
if (typeof route.has !== 'undefined' && !Array.isArray(route.has)) { const checkInvalidHasMissing = (
invalidParts.push('`has` must be undefined or valid has object') items: any,
hadInvalidHas = true fieldName: 'has' | 'missing'
} else if (route.has) { ) => {
const invalidHasItems = [] let hadInvalidItem = false
for (const hasItem of route.has) { if (typeof items !== 'undefined' && !Array.isArray(items)) {
let invalidHasParts = [] invalidParts.push(
`\`${fieldName}\` must be undefined or valid has object`
)
hadInvalidItem = true
} else if (items) {
const invalidHasItems = []
if (!allowedHasTypes.has(hasItem.type)) { for (const hasItem of items) {
invalidHasParts.push(`invalid type "${hasItem.type}"`) let invalidHasParts = []
}
if (typeof hasItem.key !== 'string' && hasItem.type !== 'host') {
invalidHasParts.push(`invalid key "${hasItem.key}"`)
}
if (
typeof hasItem.value !== 'undefined' &&
typeof hasItem.value !== 'string'
) {
invalidHasParts.push(`invalid value "${hasItem.value}"`)
}
if (typeof hasItem.value === 'undefined' && hasItem.type === 'host') {
invalidHasParts.push(`value is required for "host" type`)
}
if (invalidHasParts.length > 0) { if (!allowedHasTypes.has(hasItem.type)) {
invalidHasItems.push( invalidHasParts.push(`invalid type "${hasItem.type}"`)
`${invalidHasParts.join(', ')} for ${JSON.stringify(hasItem)}` }
) if (typeof hasItem.key !== 'string' && hasItem.type !== 'host') {
invalidHasParts.push(`invalid key "${hasItem.key}"`)
}
if (
typeof hasItem.value !== 'undefined' &&
typeof hasItem.value !== 'string'
) {
invalidHasParts.push(`invalid value "${hasItem.value}"`)
}
if (typeof hasItem.value === 'undefined' && hasItem.type === 'host') {
invalidHasParts.push(`value is required for "host" type`)
}
if (invalidHasParts.length > 0) {
invalidHasItems.push(
`${invalidHasParts.join(', ')} for ${JSON.stringify(hasItem)}`
)
}
} }
}
if (invalidHasItems.length > 0) { if (invalidHasItems.length > 0) {
hadInvalidHas = true hadInvalidItem = true
const itemStr = `item${invalidHasItems.length === 1 ? '' : 's'}` const itemStr = `item${invalidHasItems.length === 1 ? '' : 's'}`
console.error( console.error(
`Invalid \`has\` ${itemStr}:\n` + invalidHasItems.join('\n') `Invalid \`${fieldName}\` ${itemStr}:\n` +
) invalidHasItems.join('\n')
console.error() )
invalidParts.push(`invalid \`has\` ${itemStr} found`) console.error()
invalidParts.push(`invalid \`${fieldName}\` ${itemStr} found`)
}
} }
return hadInvalidItem
}
if (checkInvalidHasMissing(route.has, 'has')) {
hadInvalidHas = true
}
if (checkInvalidHasMissing(route.missing, 'missing')) {
hadInvalidMissing = true
} }
if (!route.source) { if (!route.source) {
...@@ -421,6 +443,19 @@ export function checkCustomRoutes( ...@@ -421,6 +443,19 @@ export function checkCustomRoutes(
)}` )}`
) )
} }
if (hadInvalidMissing) {
console.error(
`\nValid \`missing\` object shape is ${JSON.stringify(
{
type: [...allowedHasTypes].join(', '),
key: 'the key to check for',
value: 'undefined or a value string to match against',
},
null,
2
)}`
)
}
console.error() console.error()
console.error( console.error(
`Error: Invalid ${type}${numInvalidRoutes === 1 ? '' : 's'} found` `Error: Invalid ${type}${numInvalidRoutes === 1 ? '' : 's'} found`
......
...@@ -30,6 +30,7 @@ type RouteResult = { ...@@ -30,6 +30,7 @@ type RouteResult = {
export type Route = { export type Route = {
match: RouteMatch match: RouteMatch
has?: RouteHas[] has?: RouteHas[]
missing?: RouteHas[]
type: string type: string
check?: boolean check?: boolean
statusCode?: number statusCode?: number
...@@ -416,8 +417,13 @@ export default class Router { ...@@ -416,8 +417,13 @@ export default class Router {
}) })
let params = route.match(matchPathname) let params = route.match(matchPathname)
if (route.has && params) { if ((route.has || route.missing) && params) {
const hasParams = matchHas(req, route.has, parsedUrlUpdated.query) const hasParams = matchHas(
req,
parsedUrlUpdated.query,
route.has,
route.missing
)
if (hasParams) { if (hasParams) {
Object.assign(params, hasParams) Object.assign(params, hasParams)
} else { } else {
......
...@@ -25,8 +25,8 @@ export function getMiddlewareRouteMatcher( ...@@ -25,8 +25,8 @@ export function getMiddlewareRouteMatcher(
continue continue
} }
if (matcher.has) { if (matcher.has || matcher.missing) {
const hasParams = matchHas(req, matcher.has, query) const hasParams = matchHas(req, query, matcher.has, matcher.missing)
if (!hasParams) { if (!hasParams) {
continue continue
} }
......
...@@ -42,12 +42,13 @@ function unescapeSegments(str: string) { ...@@ -42,12 +42,13 @@ function unescapeSegments(str: string) {
export function matchHas( export function matchHas(
req: BaseNextRequest | IncomingMessage, req: BaseNextRequest | IncomingMessage,
has: RouteHas[], query: Params,
query: Params has: RouteHas[] = [],
missing: RouteHas[] = []
): false | Params { ): false | Params {
const params: Params = {} const params: Params = {}
const allMatch = has.every((hasItem) => { const hasMatch = (hasItem: RouteHas) => {
let value: undefined | string let value: undefined | string
let key = hasItem.key let key = hasItem.key
...@@ -100,7 +101,11 @@ export function matchHas( ...@@ -100,7 +101,11 @@ export function matchHas(
} }
} }
return false return false
}) }
const allMatch =
has.every((item) => hasMatch(item)) &&
!missing.some((item) => hasMatch(item))
if (allMatch) { if (allMatch) {
return params return params
......
...@@ -44,7 +44,7 @@ export default function resolveRewrites( ...@@ -44,7 +44,7 @@ export default function resolveRewrites(
let params = matcher(parsedAs.pathname) let params = matcher(parsedAs.pathname)
if (rewrite.has && params) { if ((rewrite.has || rewrite.missing) && params) {
const hasParams = matchHas( const hasParams = matchHas(
{ {
headers: { headers: {
...@@ -58,8 +58,9 @@ export default function resolveRewrites( ...@@ -58,8 +58,9 @@ export default function resolveRewrites(
return acc return acc
}, {}), }, {}),
} as any, } as any,
parsedAs.query,
rewrite.has, rewrite.has,
parsedAs.query rewrite.missing
) )
if (hasParams) { if (hasParams) {
......
...@@ -57,5 +57,25 @@ export const config = { ...@@ -57,5 +57,25 @@ export const config = {
}, },
], ],
}, },
{
source: '/missing-match-1',
missing: [
{
type: 'header',
key: 'hello',
value: '(.*)',
},
],
},
{
source: '/missing-match-2',
missing: [
{
type: 'query',
key: 'test',
value: 'value',
},
],
},
], ],
} }
...@@ -21,6 +21,28 @@ describe('Middleware custom matchers', () => { ...@@ -21,6 +21,28 @@ describe('Middleware custom matchers', () => {
afterAll(() => next.destroy()) afterAll(() => next.destroy())
const runTests = () => { const runTests = () => {
it('should match missing header correctly', async () => {
const res = await fetchViaHTTP(next.url, '/missing-match-1')
expect(res.headers.get('x-from-middleware')).toBeDefined()
const res2 = await fetchViaHTTP(next.url, '/missing-match-1', undefined, {
headers: {
hello: 'world',
},
})
expect(res2.headers.get('x-from-middleware')).toBeFalsy()
})
it('should match missing query correctly', async () => {
const res = await fetchViaHTTP(next.url, '/missing-match-2')
expect(res.headers.get('x-from-middleware')).toBeDefined()
const res2 = await fetchViaHTTP(next.url, '/missing-match-2', {
test: 'value',
})
expect(res2.headers.get('x-from-middleware')).toBeFalsy()
})
it('should match source path', async () => { it('should match source path', async () => {
const res = await fetchViaHTTP(next.url, '/source-match') const res = await fetchViaHTTP(next.url, '/source-match')
expect(res.status).toBe(200) expect(res.status).toBe(200)
......
...@@ -204,6 +204,38 @@ module.exports = { ...@@ -204,6 +204,38 @@ module.exports = {
], ],
destination: '/blog-catchall/:post', destination: '/blog-catchall/:post',
}, },
{
source: '/missing-rewrite-1',
missing: [
{
type: 'header',
key: 'x-my-header',
value: '(?<myHeader>.*)',
},
],
destination: '/with-params',
},
{
source: '/missing-rewrite-2',
missing: [
{
type: 'query',
key: 'my-query',
},
],
destination: '/with-params',
},
{
source: '/missing-rewrite-3',
missing: [
{
type: 'cookie',
key: 'loggedIn',
value: '(?<loggedIn>true)',
},
],
destination: '/with-params?authorized=1',
},
{ {
source: '/blog/about', source: '/blog/about',
destination: '/hello', destination: '/hello',
......
...@@ -913,6 +913,52 @@ const runTests = (isDev = false) => { ...@@ -913,6 +913,52 @@ const runTests = (isDev = false) => {
) )
}) })
it('should match missing header rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/missing-rewrite-1', undefined, {
headers: {
'x-my-header': 'hello world!!',
},
})
expect(res.status).toBe(404)
const res2 = await fetchViaHTTP(appPort, '/missing-rewrite-1')
const $2 = cheerio.load(await res2.text())
expect(res2.status).toBe(200)
expect(JSON.parse($2('#query').text())).toEqual({})
})
it('should match missing query rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/missing-rewrite-2', {
'my-query': 'hellooo',
})
expect(res.status).toBe(404)
const res2 = await fetchViaHTTP(appPort, '/missing-rewrite-2')
const $2 = cheerio.load(await res2.text())
expect(res2.status).toBe(200)
expect(JSON.parse($2('#query').text())).toEqual({})
})
it('should match missing cookie rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/missing-rewrite-3', undefined, {
headers: {
cookie: 'loggedIn=true',
},
})
expect(res.status).toBe(404)
const res2 = await fetchViaHTTP(appPort, '/missing-rewrite-3')
const $2 = cheerio.load(await res2.text())
expect(JSON.parse($2('#query').text())).toEqual({
authorized: '1',
})
expect(res2.status).toBe(200)
})
it('should match has header rewrite correctly', async () => { it('should match has header rewrite correctly', async () => {
const res = await fetchViaHTTP(appPort, '/has-rewrite-1', undefined, { const res = await fetchViaHTTP(appPort, '/has-rewrite-1', undefined, {
headers: { headers: {
...@@ -2079,6 +2125,41 @@ const runTests = (isDev = false) => { ...@@ -2079,6 +2125,41 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx('^\\/has-rewrite-8(?:\\/)?$'), regex: normalizeRegEx('^\\/has-rewrite-8(?:\\/)?$'),
source: '/has-rewrite-8', source: '/has-rewrite-8',
}, },
{
destination: '/with-params',
missing: [
{
key: 'x-my-header',
type: 'header',
value: '(?<myHeader>.*)',
},
],
regex: normalizeRegEx('^\\/missing-rewrite-1(?:\\/)?$'),
source: '/missing-rewrite-1',
},
{
destination: '/with-params',
missing: [
{
key: 'my-query',
type: 'query',
},
],
regex: normalizeRegEx('^\\/missing-rewrite-2(?:\\/)?$'),
source: '/missing-rewrite-2',
},
{
destination: '/with-params?authorized=1',
missing: [
{
key: 'loggedIn',
type: 'cookie',
value: '(?<loggedIn>true)',
},
],
regex: normalizeRegEx('^\\/missing-rewrite-3(?:\\/)?$'),
source: '/missing-rewrite-3',
},
{ {
destination: '/hello', destination: '/hello',
regex: normalizeRegEx('^\\/blog\\/about(?:\\/)?$'), regex: normalizeRegEx('^\\/blog\\/about(?:\\/)?$'),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment