|
6 | 6 |
|
7 | 7 | import { expect } from "chai"; |
8 | 8 | import { Container } from "inversify"; |
9 | | -import express from "express"; |
10 | 9 | import { Config } from "../config"; |
11 | 10 | import { NonceService } from "./nonce-service"; |
12 | 11 |
|
@@ -65,76 +64,4 @@ describe("NonceService", () => { |
65 | 64 | expect(nonceService.validateNonce(undefined, undefined)).to.be.false; |
66 | 65 | }); |
67 | 66 | }); |
68 | | - |
69 | | - describe("validateOrigin", () => { |
70 | | - it("should accept requests from expected SCM provider origin", () => { |
71 | | - const req = { |
72 | | - get: (header: string) => { |
73 | | - if (header === "Origin") return "https://github.com"; |
74 | | - return undefined; |
75 | | - }, |
76 | | - } as Partial<express.Request> as express.Request; |
77 | | - |
78 | | - const isValid = nonceService.validateOrigin(req, "github.com"); |
79 | | - expect(isValid).to.be.true; |
80 | | - }); |
81 | | - |
82 | | - it("should reject requests from different origin", () => { |
83 | | - const req = { |
84 | | - get: (header: string) => { |
85 | | - if (header === "Origin") return "https://evil.com"; |
86 | | - return undefined; |
87 | | - }, |
88 | | - } as Partial<express.Request> as express.Request; |
89 | | - |
90 | | - const isValid = nonceService.validateOrigin(req, "github.com"); |
91 | | - expect(isValid).to.be.false; |
92 | | - }); |
93 | | - |
94 | | - it("should reject requests without origin or referer", () => { |
95 | | - const req = { |
96 | | - get: () => undefined, |
97 | | - } as Partial<express.Request> as express.Request; |
98 | | - |
99 | | - const isValid = nonceService.validateOrigin(req, "github.com"); |
100 | | - expect(isValid).to.be.false; |
101 | | - }); |
102 | | - |
103 | | - it("should accept requests with valid referer from expected host", () => { |
104 | | - const req = { |
105 | | - get: (header: string) => { |
106 | | - if (header === "Referer") return "https://gitlab.com/oauth/authorize"; |
107 | | - return undefined; |
108 | | - }, |
109 | | - } as Partial<express.Request> as express.Request; |
110 | | - |
111 | | - const isValid = nonceService.validateOrigin(req, "gitlab.com"); |
112 | | - expect(isValid).to.be.true; |
113 | | - }); |
114 | | - |
115 | | - it("should work with different SCM providers", () => { |
116 | | - const testCases = [ |
117 | | - { origin: "https://github.com", expectedHost: "github.com", shouldPass: true }, |
118 | | - { origin: "https://gitlab.com", expectedHost: "gitlab.com", shouldPass: true }, |
119 | | - { origin: "https://bitbucket.org", expectedHost: "bitbucket.org", shouldPass: true }, |
120 | | - { origin: "https://github.com", expectedHost: "gitlab.com", shouldPass: false }, |
121 | | - { origin: "https://evil.com", expectedHost: "github.com", shouldPass: false }, |
122 | | - ]; |
123 | | - |
124 | | - testCases.forEach(({ origin, expectedHost, shouldPass }) => { |
125 | | - const req = { |
126 | | - get: (header: string) => { |
127 | | - if (header === "Origin") return origin; |
128 | | - return undefined; |
129 | | - }, |
130 | | - } as Partial<express.Request> as express.Request; |
131 | | - |
132 | | - const isValid = nonceService.validateOrigin(req, expectedHost); |
133 | | - expect(isValid).to.equal( |
134 | | - shouldPass, |
135 | | - `${origin} vs ${expectedHost} should ${shouldPass ? "pass" : "fail"}`, |
136 | | - ); |
137 | | - }); |
138 | | - }); |
139 | | - }); |
140 | 67 | }); |
0 commit comments