-
Notifications
You must be signed in to change notification settings - Fork 231
kie-tools#3349: [cors-proxy] Implement domains filtering #3353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fantonangeli
wants to merge
10
commits into
apache:main
Choose a base branch
from
fantonangeli:kie-tools#3349-cors-proxy-Implement-domains-filtering
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4c19cd7
Fix WF issue preventing the draw of WF Diagram
fantonangeli 606afe2
Fix cors issue for sonataflow-dev-app and runtime-tools-process-dev-u…
fantonangeli 8ed3054
Updated cors-proxy and cors-proxy-image
fantonangeli 4514bb9
Implemented devmode in cors-proxy
fantonangeli 73817e7
Update cors-proxy-image
fantonangeli f406a7c
Merge remote-tracking branch 'upstream/main' into kie-tools#3308-cors…
fantonangeli 53444d4
Implemented filtering
fantonangeli f5d71a2
Updated cors-proxy-image
fantonangeli 0581d70
Fixes comments:
fantonangeli 0094787
Merge branch 'kie-tools#3308-cors-proxy-CORS-code-scanning-issue' int…
fantonangeli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,7 @@ import * as https from "https"; | |||||
| import fetch from "node-fetch"; | ||||||
| import { Request, Response } from "express"; | ||||||
| import { HttpsProxyAgent } from "https-proxy-agent"; | ||||||
| import * as minimatch from "minimatch"; | ||||||
| import { GIT_CORS_CONFIG, isGitOperation } from "./git"; | ||||||
| import { CorsProxyHeaderKeys, CorsConfig, CorsProxy } from "@kie-tools/cors-proxy-api/dist"; | ||||||
|
|
||||||
|
|
@@ -37,16 +38,16 @@ export class ExpressCorsProxy implements CorsProxy<Request, Response> { | |||||
|
|
||||||
| constructor( | ||||||
| private readonly args: { | ||||||
| origin: string; | ||||||
| allowedOrigins: string[]; | ||||||
| verbose: boolean; | ||||||
| hostsToUseHttp: string[]; | ||||||
| allowHosts: string[]; | ||||||
| } | ||||||
| ) { | ||||||
| this.logger = new Logger(args.verbose); | ||||||
|
|
||||||
| this.logger.debug(""); | ||||||
| this.logger.debug("Proxy Configuration:"); | ||||||
| this.logger.debug("* Accept Origin Header: ", `"${args.origin}"`); | ||||||
| this.logger.debug("* Verbose: ", args.verbose); | ||||||
| this.logger.debug(""); | ||||||
| } | ||||||
|
|
@@ -92,9 +93,6 @@ export class ExpressCorsProxy implements CorsProxy<Request, Response> { | |||||
| }); | ||||||
| this.logger.debug("Proxy Response status: ", proxyResponse.status); | ||||||
|
|
||||||
| // Setting up the headers to the original response... | ||||||
| res.header("Access-Control-Allow-Origin", this.args.origin); | ||||||
|
|
||||||
| if (req.method == "OPTIONS") { | ||||||
| res.header("Access-Control-Allow-Methods", info.corsConfig?.allowMethods.join(", ") ?? "*"); | ||||||
| res.header("Access-Control-Allow-Headers", info.corsConfig?.allowHeaders.join(", ") ?? "*"); | ||||||
|
|
@@ -105,6 +103,9 @@ export class ExpressCorsProxy implements CorsProxy<Request, Response> { | |||||
| } | ||||||
|
|
||||||
| proxyResponse.headers.forEach((value, header) => { | ||||||
| if (header.toLowerCase() === "access-control-allow-origin") { | ||||||
| return; | ||||||
| } | ||||||
| if (!info.corsConfig || info.corsConfig.exposeHeaders.includes(header)) { | ||||||
| res.setHeader(header, value); | ||||||
| } | ||||||
|
|
@@ -118,7 +119,7 @@ export class ExpressCorsProxy implements CorsProxy<Request, Response> { | |||||
|
|
||||||
| res.status(proxyResponse.status); | ||||||
|
|
||||||
| this.logger.debug("Writting Response..."); | ||||||
| this.logger.debug("Writing Response..."); | ||||||
| if (proxyResponse.body) { | ||||||
| const stream = proxyResponse.body.pipe(res); | ||||||
| stream.on("close", () => { | ||||||
|
|
@@ -140,7 +141,18 @@ export class ExpressCorsProxy implements CorsProxy<Request, Response> { | |||||
| } | ||||||
|
|
||||||
| private resolveRequestInfo(request: Request): ProxyRequestInfo { | ||||||
| const origin = request.header("origin"); | ||||||
| const targetUrl: string = (request.headers[CorsProxyHeaderKeys.TARGET_URL] as string) ?? request.url; | ||||||
| const parsedTargetUrl = new URL(targetUrl); | ||||||
|
|
||||||
| if (!this.args.allowHosts.some((pattern) => minimatch(parsedTargetUrl.hostname, pattern))) { | ||||||
| throw new Error(`The target URL in not allowed. Requested: ${targetUrl}`); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| if (!origin || !this.args.allowedOrigins.includes(origin)) { | ||||||
| throw new Error(`Origin ${origin} is not allowed`); | ||||||
| } | ||||||
|
|
||||||
| if (!targetUrl || targetUrl == "/") { | ||||||
| throw new Error("Couldn't resolve the target URL..."); | ||||||
| } | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue seems to be here. Things like
/api.rm2.thpm.p1.openshiftapps.com:6443/versionare not valid as a parameter fornew URL()and throw errors due to the leading/.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But even after doing
const parsedTargetUrl = new URL(targetUrl.slice(1))this is the resulting URL:Notice that the hostname is empty (probably because the URL doesn't have the protocol), and it will make the
allowHostscheck fail.