-
Notifications
You must be signed in to change notification settings - Fork 158
feat: add upstream proxy configuration for outbound requests #1458
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
Andreybest
wants to merge
6
commits into
finos:main
Choose a base branch
from
Andreybest:759-upstream-proxy
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 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
be51974
feat: add upstream proxy configuration for outbound requests
Andreybest b0a4b43
Merge branch 'main' into 759-upstream-proxy
Andreybest 548e84c
fix: changes for review
Andreybest 9336511
fix: improve regular expression for redactions
Andreybest 45624bf
Update proxy.config.json
Andreybest 9af1d69
Merge branch 'main' into 759-upstream-proxy
jescalada 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -222,6 +222,26 @@ Currently supports the following out-of-the-box: | |
| - ActiveDirectory auth configuration for querying via a REST API rather than LDAP | ||
| - Gitleaks configuration | ||
|
|
||
| #### `upstreamProxy` | ||
|
|
||
| Configures routing of outbound requests from the GitProxy server to upstream Git hosts (e.g. GitHub, GitLab) via an HTTP(S) proxy. Use this when the server runs in an environment where direct Internet access is not allowed and all traffic must go through a corporate web proxy ("proxying the proxy"). | ||
|
|
||
| - **`enabled`** (boolean): When `true`, outbound connections to upstream Git hosts use the configured proxy. When `false`, the proxy is not used even if `url` or environment variables are set. | ||
| - **`url`** (string): The HTTP(S) proxy URL (e.g. `http://proxy.corp.local:8080` or `http://user:[email protected]:8080`). If omitted, GitProxy falls back to the `HTTPS_PROXY`, `https_proxy`, `HTTP_PROXY` or `http_proxy` environment variables (first defined wins). | ||
jescalada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - **`noProxy`** (array of strings, optional): Hostnames or domain suffixes for which the proxy should be bypassed (e.g. internal Git hosts). Combined with the `NO_PROXY` / `no_proxy` environment variable. | ||
|
|
||
| Example: | ||
|
|
||
| ```json | ||
| "upstreamProxy": { | ||
| "enabled": true, | ||
| "url": "http://proxy.corp.local:8080", | ||
| "noProxy": ["github.corp.local", "gitlab.corp.local"] | ||
| } | ||
| ``` | ||
|
|
||
| If `upstreamProxy` is not configured, setting only `HTTPS_PROXY` (or `HTTP_PROXY`) in the environment will also enable use of that proxy for outbound connections, unless `enabled` is explicitly set to `false` in config. | ||
|
|
||
| #### `commitConfig` | ||
|
|
||
| Used in [`checkCommitMessages`](./Processors.md#checkcommitmessages), [`checkAuthorEmails`](./Processors.md#checkauthoremails) and [`scanDiff`](./Processors.md#scandiff) processors to block pushes depending on the given rules. | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -149,6 +149,23 @@ | |
| return config.proxyUrl; | ||
| }; | ||
|
|
||
| /** | ||
| * Redacts the userinfo (credentials) from a proxy URL for safe logging. | ||
| * e.g. http://user:[email protected]:8080 → http://<redacted>@proxy.corp.local:8080 | ||
| * | ||
| * WARNING: proxyUrl may contain plaintext credentials in the userinfo portion. | ||
| * Never log a raw proxy URL — always pass it through this helper first. | ||
| */ | ||
| export const redactProxyUrl = (url: string): string => { | ||
| return url.replace(/(https?:\/\/)[^@]+@/, '$1<redacted>@'); | ||
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
|
||
| }; | ||
|
|
||
| // Get upstream proxy configuration | ||
| export const getUpstreamProxyConfig = () => { | ||
| const config = loadFullConfiguration(); | ||
| return config.upstreamProxy || {}; | ||
| }; | ||
|
|
||
| // Gets a list of authorised repositories | ||
| export const getAuthorisedList = () => { | ||
| const config = loadFullConfiguration(); | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -23,6 +23,9 @@ import { processUrlPath, validGitRequest } from './helper'; | |
| import { getAllProxiedHosts } from '../../db'; | ||
| import { ProxyOptions } from 'express-http-proxy'; | ||
| import { handleErrorAndLog } from '../../utils/errors'; | ||
| import { getUpstreamProxyConfig } from '../../config'; | ||
| import { HttpsProxyAgent } from 'https-proxy-agent'; | ||
| import { OutgoingHttpHeaders, RequestOptions } from 'http'; | ||
|
|
||
| enum ActionType { | ||
| ALLOWED = 'Allowed', | ||
|
|
@@ -144,7 +147,104 @@ const getRequestPathResolver: (prefix: string) => ProxyOptions['proxyReqPathReso | |
| }; | ||
| }; | ||
|
|
||
| const proxyReqOptDecorator: ProxyOptions['proxyReqOptDecorator'] = (proxyReqOpts) => proxyReqOpts; | ||
| const getEnvProxyUrl = () => | ||
| process.env.HTTPS_PROXY || | ||
| process.env.https_proxy || | ||
| process.env.HTTP_PROXY || | ||
| process.env.http_proxy; | ||
|
|
||
| const getEnvNoProxyList = (): string[] => { | ||
| const noProxy = process.env.NO_PROXY || process.env.no_proxy; | ||
| if (!noProxy) { | ||
| return []; | ||
| } | ||
| return noProxy | ||
| .split(',') | ||
| .map((entry) => entry.trim()) | ||
| .filter((entry) => entry.length > 0); | ||
| }; | ||
|
|
||
| const hostMatchesNoProxy = (host: string | null | undefined, noProxyList: string[]): boolean => { | ||
jescalada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!host) { | ||
| return false; | ||
| } | ||
|
|
||
| const hostname = host.split(':')[0]; | ||
|
|
||
| return noProxyList.some((pattern) => { | ||
| if (!pattern) { | ||
| return false; | ||
| } | ||
|
|
||
| const trimmed = pattern.trim().replace(/^\./, ''); // strip leading dot | ||
| if (trimmed === '*') return true; // wildcard - bypass all | ||
|
|
||
| if (trimmed === '') { | ||
| return false; | ||
| } | ||
|
|
||
| // Exact match | ||
| if (hostname === trimmed) { | ||
| return true; | ||
| } | ||
|
|
||
| // Domain suffix match, e.g. example.com matches foo.example.com | ||
| if (hostname.endsWith(`.${trimmed}`)) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| }); | ||
| }; | ||
|
|
||
| // WARNING: proxyUrl may contain plaintext credentials in the userinfo portion | ||
| // (e.g. http://user:[email protected]:8080). Never log it directly — use | ||
| // redactProxyUrl() from config for any log statements involving this value. | ||
| let _cachedProxyAgent: { proxyUrl: string; agent: HttpsProxyAgent<string> } | null = null; | ||
|
|
||
| const getOrCreateProxyAgent = (proxyUrl: string): HttpsProxyAgent<string> => { | ||
| if (!_cachedProxyAgent || _cachedProxyAgent.proxyUrl !== proxyUrl) { | ||
| _cachedProxyAgent = { proxyUrl, agent: new HttpsProxyAgent(proxyUrl) }; | ||
|
Contributor
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. Just repeating Denis earlier comment - this should be properly validated and/or throw a descriptive error |
||
| } | ||
| return _cachedProxyAgent.agent; | ||
| }; | ||
|
|
||
| const buildUpstreamProxyAgent = ( | ||
| proxyReqOpts: Omit<RequestOptions, 'headers'> & { | ||
| headers: OutgoingHttpHeaders; | ||
| }, | ||
| ) => { | ||
| const { enabled, url, noProxy } = getUpstreamProxyConfig(); | ||
|
|
||
| const proxyUrl = url || getEnvProxyUrl(); | ||
|
|
||
| if (enabled === false || !proxyUrl) { | ||
| return undefined; | ||
jescalada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const host: string | null | undefined = proxyReqOpts.host || proxyReqOpts.hostname; | ||
|
|
||
| const combinedNoProxy = [...(noProxy || []), ...getEnvNoProxyList()]; | ||
|
|
||
| if (hostMatchesNoProxy(host, combinedNoProxy)) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return getOrCreateProxyAgent(proxyUrl); | ||
| }; | ||
|
|
||
| const proxyReqOptDecorator: ProxyOptions['proxyReqOptDecorator'] = (proxyReqOpts, _srcReq) => { | ||
| const agent = buildUpstreamProxyAgent(proxyReqOpts); | ||
jescalada marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!agent) { | ||
| return proxyReqOpts; | ||
| } | ||
|
|
||
| return { | ||
| ...proxyReqOpts, | ||
| agent, | ||
| }; | ||
| }; | ||
|
|
||
| const proxyReqBodyDecorator: ProxyOptions['proxyReqBodyDecorator'] = (bodyContent, srcReq) => { | ||
| if (srcReq.method === 'GET') { | ||
|
|
@@ -273,4 +373,6 @@ export { | |
| isPackPost, | ||
| extractRawBody, | ||
| validGitRequest, | ||
| buildUpstreamProxyAgent, | ||
| hostMatchesNoProxy, | ||
| }; | ||
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.