Skip to content

Commit 2e64471

Browse files
authored
Merge pull request #4035 from github/mbg/improve-global-proxy-tests
Improve global proxy tests
2 parents ecbe7c2 + a2bfb64 commit 2e64471

5 files changed

Lines changed: 95 additions & 10 deletions

File tree

.github/workflows/__global-proxy.yml

Lines changed: 29 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/entry-points.js

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pr-checks/checks/global-proxy.yml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,45 @@ versions:
55
- nightly-latest
66
container:
77
image: ubuntu:22.04
8+
options: --cap-add=NET_ADMIN
89
services:
910
squid-proxy:
1011
image: ubuntu/squid:latest
1112
ports:
1213
- 3128:3128
1314
env:
14-
https_proxy: http://squid-proxy:3128
1515
CODEQL_ACTION_TOLERATE_MISSING_GIT_VERSION: true
1616
steps:
17+
- name: Block direct internet access to force proxy usage
18+
run: |
19+
apt-get update -qq && apt-get install -y -qq iptables >/dev/null 2>&1
20+
PROXY_IP=$(getent hosts squid-proxy | awk '{ print $1 }')
21+
echo "Squid proxy IP: $PROXY_IP"
22+
# Allow all traffic to the proxy container
23+
iptables -A OUTPUT -d "$PROXY_IP" -j ACCEPT
24+
# Allow DNS resolution
25+
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
26+
iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
27+
# Allow loopback
28+
iptables -A OUTPUT -o lo -j ACCEPT
29+
# Allow already-established connections (from checkout/prepare-test)
30+
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
31+
# Block all other outbound HTTP and HTTPS, ensuring direct access fails
32+
iptables -A OUTPUT -p tcp --dport 80 -j REJECT --reject-with tcp-reset
33+
iptables -A OUTPUT -p tcp --dport 443 -j REJECT --reject-with tcp-reset
34+
echo "Direct HTTP/HTTPS access is now blocked - all traffic must go through the proxy"
35+
36+
- name: Set proxy environment variables
37+
shell: bash
38+
run: |
39+
echo "http_proxy=http://squid-proxy:3128" >> $GITHUB_ENV
40+
echo "HTTP_PROXY=http://squid-proxy:3128" >> $GITHUB_ENV
41+
echo "https_proxy=http://squid-proxy:3128" >> $GITHUB_ENV
42+
echo "HTTPS_PROXY=http://squid-proxy:3128" >> $GITHUB_ENV
43+
1744
- uses: ./../action/init
1845
with:
1946
languages: javascript
2047
tools: ${{ steps.prepare-test.outputs.tools-url }}
48+
2149
- uses: ./../action/analyze

src/api-client.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as github from "@actions/github";
22
import * as githubUtils from "@actions/github/lib/utils";
33
import test from "ava";
44
import * as sinon from "sinon";
5+
import { ProxyAgent } from "undici";
56

67
import * as actionsUtil from "./actions-util";
78
import * as api from "./api-client";
@@ -251,3 +252,23 @@ test("getRegistryProxyConfig - gets the configuration from the env vars", async
251252
)
252253
.passes(t.like, { host, port, ca });
253254
});
255+
256+
test("makeProxyRequestOptions - returns defaults without custom proxy", async (t) => {
257+
t.deepEqual(
258+
api.makeProxyRequestOptions(undefined),
259+
githubUtils.defaults.request,
260+
);
261+
});
262+
263+
test("makeProxyRequestOptions - returns fetch with custom proxy", async (t) => {
264+
const opts = api.makeProxyRequestOptions(
265+
new ProxyAgent("http://localhost:1080"),
266+
);
267+
// Fetch should be different from the defaults.
268+
t.notDeepEqual(opts?.fetch, githubUtils.defaults.request?.fetch);
269+
// The options should be the same aside from that.
270+
t.deepEqual(
271+
{ ...opts, fetch: githubUtils.defaults.request?.fetch },
272+
githubUtils.defaults.request,
273+
);
274+
});

src/api-client.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,19 @@ export function getRegistryProxy(
108108
* Constructs a `RequestRequestOptions` with a custom `fetch` implementation
109109
* that uses `dispatcher` as a proxy for requests.
110110
*
111-
* @param dispatcher The proxy to use.
111+
* @param dispatcher The proxy to use, if any.
112112
*/
113113
export function makeProxyRequestOptions(
114-
dispatcher: ProxyAgent,
115-
): RequestRequestOptions {
114+
dispatcher: ProxyAgent | undefined,
115+
): RequestRequestOptions | undefined {
116+
// If we don't have a custom `ProxyAgent`, return the defaults.
117+
if (dispatcher === undefined) {
118+
return githubUtils.defaults.request;
119+
}
120+
121+
// Otherwise, construct the custom `fetch` and add it onto the defaults.
116122
return {
123+
...githubUtils.defaults.request,
117124
fetch: (req: RequestInfo, init?: RequestInit) => {
118125
return undiciFetch(req, { ...init, dispatcher });
119126
},
@@ -136,10 +143,7 @@ function createApiClientWithDetails(
136143
const auth =
137144
(allowExternal && apiDetails.externalRepoAuth) || apiDetails.auth;
138145
const retryingOctokit = githubUtils.GitHub.plugin(retry.retry);
139-
const requestOptions =
140-
proxy === undefined
141-
? githubUtils.defaults.request
142-
: makeProxyRequestOptions(proxy);
146+
const requestOptions = makeProxyRequestOptions(proxy);
143147
return new retryingOctokit(
144148
githubUtils.getOctokitOptions(auth, {
145149
baseUrl: apiDetails.apiURL,

0 commit comments

Comments
 (0)