Skip to content

Commit 1aee5aa

Browse files
pklitscherbkendallyuchenshi
authored andcommitted
Feature CORS-RFC1918 Support (#4305)
* Add support for cors rfc1918 * Remove null from next() Co-authored-by: Bryan Kendall <[email protected]> * Remove null from next() Co-authored-by: Bryan Kendall <[email protected]> * Revert incorrect linting changes endOfLine rule was temporarily changed for prettier/prettier to allow tests to pass on Windows + VS Code. Reverting to original repo settings * Update CHANGELOG.md * Update src/test/emulators/auth/rest.spec.ts Co-authored-by: Bryan Kendall <[email protected]> * Update CHANGELOG.md * Update CHANGELOG * Update src/emulator/storage/server.ts Co-authored-by: Yuchen Shi <[email protected]> * Update CHANGELOG.md Co-authored-by: Bryan Kendall <[email protected]> Co-authored-by: Yuchen Shi <[email protected]> Co-authored-by: Bryan Kendall <[email protected]>
1 parent 96c74c6 commit 1aee5aa

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
- Refactor mechanism for invoking function triggers (#4886).
33
- Add support for `HTTP_PROXY` and `HTTPS_PROXY` environment variables to `crashlytics:mappingfile:upload` and `crashlytics:symbols:upload` commands (#4604).
44
- Fix Emulators not shutting down / exporting correctly when CLI update available (#4981).
5+
- Adds `access-control-allow-private-network=true` header to Auth and Storage emulators. Enables accessing at localhost:port when site is exposed via tunnel (#4227).

src/emulator/auth/server.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ export async function createApp(
120120
): Promise<express.Express> {
121121
const app = express();
122122
app.set("json spaces", 2);
123+
124+
// Retrun access-control-allow-private-network heder if requested
125+
// Enables accessing locahost when site is exposed via tunnel see https://github.com/firebase/firebase-tools/issues/4227
126+
// Aligns with https://wicg.github.io/private-network-access/#headers
127+
// Replace with cors option if adopted, see https://github.com/expressjs/cors/issues/236
128+
app.use("/", (req, res, next) => {
129+
if (req.headers["access-control-request-private-network"]) {
130+
res.setHeader("access-control-allow-private-network", "true");
131+
}
132+
next();
133+
});
134+
123135
// Enable CORS for all APIs, all origins (reflected), and all headers (reflected).
124136
// This is similar to production behavior. Safe since all APIs are cookieless.
125137
app.use(cors({ origin: true }));

src/emulator/storage/server.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ export function createApp(
2525
`Temp file directory for storage emulator: ${storageLayer.dirPath}`
2626
);
2727

28+
// Retrun access-control-allow-private-network header if requested
29+
// Enables accessing locahost when site is exposed via tunnel see https://github.com/firebase/firebase-tools/issues/4227
30+
// Aligns with https://wicg.github.io/private-network-access/#headers
31+
// Replace with cors option if adopted, see https://github.com/expressjs/cors/issues/236
32+
app.use("/", (req, res, next) => {
33+
if (req.headers["access-control-request-private-network"]) {
34+
res.setHeader("access-control-allow-private-network", "true");
35+
}
36+
next();
37+
});
38+
2839
// Enable CORS for all APIs, all origins (reflected), and all headers (reflected).
2940
// This is similar to production behavior. Safe since all APIs are cookieless.
3041
app.use(

src/test/emulators/auth/rest.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describeAuthEmulator("REST API mapping", ({ authApi }) => {
1717
.options("/")
1818
.set("Origin", "example.com")
1919
.set("Access-Control-Request-Headers", "Authorization,X-Client-Version,X-Whatever-Header")
20+
.set("Access-Control-Request-Private-Network", "true")
2021
.then((res) => {
2122
expectStatusCode(204, res);
2223

@@ -29,6 +30,10 @@ describeAuthEmulator("REST API mapping", ({ authApi }) => {
2930
"X-Client-Version",
3031
"X-Whatever-Header",
3132
]);
33+
34+
// Check that access-control-allow-private-network = true
35+
// Enables accessing locahost when site is exposed via tunnel see https://github.com/firebase/firebase-tools/issues/4227
36+
expect(res.header["access-control-allow-private-network"]).to.eql("true");
3237
});
3338
});
3439

0 commit comments

Comments
 (0)