DEV: Fix ember-cli proxy issues (#15071)

* DEV: Fix ember-cli proxy compat with node < 16
* DEV: Fix uploading via ember-cli
* DEV: Fix proxying /logs & other CSP-enabled pages
This commit is contained in:
Jarek Radosz 2021-11-24 12:52:25 +01:00 committed by GitHub
parent 3ea8937157
commit e217364a46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,7 @@ const getJSON = bent("json");
const { encode } = require("html-entities");
const cleanBaseURL = require("clean-base-url");
const path = require("path");
const fs = require("fs/promises");
const { promises: fs } = require("fs");
// via https://stackoverflow.com/a/6248722/165668
function generateUID() {
@ -240,13 +240,19 @@ async function handleRequest(proxy, baseURL, req, res) {
const { location } = response.headers;
if (location) {
const newLocation = location
.replace(req.headers.host, originalHost)
.replace(/^https/, "http");
const newLocation = location.replace(proxy, `http://${originalHost}`);
res.set("location", newLocation);
}
const csp = response.headers["content-security-policy"];
if (csp) {
const newCSP = csp.replace(
new RegExp(proxy, "g"),
`http://${originalHost}`
);
res.set("content-security-policy", newCSP);
}
if (response.headers["x-discourse-bootstrap-required"] === "true") {
const html = await buildFromBootstrap(proxy, baseURL, req, response);
res.set("content-type", "text/html");
@ -280,7 +286,9 @@ to serve API requests. For example:
baseURL = rootURL === "" ? "/" : cleanBaseURL(rootURL || baseURL);
app.use(express.raw({ type: "*/*" }), async (req, res, next) => {
const rawMiddleware = express.raw({ type: "*/*", limit: "100mb" });
app.use(rawMiddleware, async (req, res, next) => {
try {
if (this.shouldHandleRequest(req)) {
await handleRequest(proxy, baseURL, req, res);
@ -301,12 +309,20 @@ to serve API requests. For example:
},
shouldHandleRequest(request) {
if (request.get("Accept")?.includes("text/html")) {
if (request.get("Accept") && request.get("Accept").includes("text/html")) {
return true;
}
if (
request.get("Content-Type")?.includes("application/x-www-form-urlencoded")
request.get("Content-Type") &&
request.get("Content-Type").includes("application/x-www-form-urlencoded")
) {
return true;
}
if (
request.get("Content-Type") &&
request.get("Content-Type").includes("multipart/form-data")
) {
return true;
}