File size: 2,916 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { isCsrfOriginAllowed } from './csrf-protection'

describe('isCsrfOriginAllowed', () => {
  it('should return true when allowedOrigins contains originDomain', () => {
    expect(isCsrfOriginAllowed('vercel.com', ['vercel.com'])).toBe(true)
    expect(isCsrfOriginAllowed('www.vercel.com', ['www.vercel.com'])).toBe(true)
  })

  it('should return true when allowedOrigins contains originDomain with matching pattern', () => {
    expect(isCsrfOriginAllowed('asdf.vercel.com', ['*.vercel.com'])).toBe(true)
    expect(isCsrfOriginAllowed('asdf.vercel.com', ['**.vercel.com'])).toBe(true)
    expect(isCsrfOriginAllowed('asdf.jkl.vercel.com', ['**.vercel.com'])).toBe(
      true
    )
  })

  it("should correctly handle origins that don't have a TLD (eg for localhost)", () => {
    // Single level wildcard
    expect(isCsrfOriginAllowed('subdomain.localhost', ['*.localhost'])).toBe(
      true
    )
    expect(isCsrfOriginAllowed('localhost', ['*.localhost'])).toBe(false)

    // Multi-level wildcard
    expect(isCsrfOriginAllowed('subdomain.localhost', ['**.localhost'])).toBe(
      true
    )
    expect(isCsrfOriginAllowed('a.b.localhost', ['**.localhost'])).toBe(true)
    expect(isCsrfOriginAllowed('localhost', ['**.localhost'])).toBe(false)

    // Exact match
    expect(isCsrfOriginAllowed('localhost', ['localhost'])).toBe(true)
    expect(isCsrfOriginAllowed('subdomain.localhost', ['localhost'])).toBe(
      false
    )

    // Multiple patterns
    expect(
      isCsrfOriginAllowed('subdomain.localhost', ['localhost', '*.localhost'])
    ).toBe(true)
    expect(
      isCsrfOriginAllowed('a.b.localhost', [
        'localhost',
        '*.localhost',
        '**.localhost',
      ])
    ).toBe(true)
  })

  it('should return false when allowedOrigins contains originDomain with non-matching pattern', () => {
    expect(isCsrfOriginAllowed('asdf.vercel.com', ['*.vercel.app'])).toBe(false)
    expect(isCsrfOriginAllowed('asdf.jkl.vercel.com', ['*.vercel.com'])).toBe(
      false
    )
    expect(isCsrfOriginAllowed('asdf.jkl.vercel.app', ['**.vercel.com'])).toBe(
      false
    )
  })

  it('should return false when allowedOrigins does not contain originDomain', () => {
    expect(isCsrfOriginAllowed('vercel.com', ['nextjs.org'])).toBe(false)
  })

  it('should return false when allowedOrigins is undefined', () => {
    expect(isCsrfOriginAllowed('vercel.com', undefined)).toBe(false)
  })

  it('should return false when allowedOrigins is empty', () => {
    expect(isCsrfOriginAllowed('vercel.com', [])).toBe(false)
  })

  it('should return false when allowedOrigins is empty string', () => {
    expect(isCsrfOriginAllowed('vercel.com', [''])).toBe(false)
  })

  it('wildcards are only supported below the domain level', () => {
    expect(isCsrfOriginAllowed('vercel.com', ['*'])).toBe(false)
    expect(isCsrfOriginAllowed('vercel.com', ['**'])).toBe(false)
  })
})