From fd40437927a1d773a9c8e7d5a4d2c1a31169be73 Mon Sep 17 00:00:00 2001 From: ibbem Date: Sat, 22 Feb 2025 00:03:57 +0100 Subject: [PATCH 1/3] Allow disabling of all network accesses --- History.markdown | 6 ++++++ docs/configuration.md | 1 + lib/jekyll-github-metadata/client.rb | 20 ++++++++++++-------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/History.markdown b/History.markdown index 61a482c..e58937c 100644 --- a/History.markdown +++ b/History.markdown @@ -1,3 +1,9 @@ +## HEAD + +## Minor Enhancements + + * Disable network related features if the env var `PAGES_DISABLE_NETWORK` is set + ## 2.16.1 / 2023-12-22 ### Bug Fixes diff --git a/docs/configuration.md b/docs/configuration.md index 9725677..fb7373a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -44,6 +44,7 @@ Some `site.github` values can be overridden by environment variables. - `PAGES_GITHUB_HOSTNAME` – the `site.github.hostname` (default: `github.com`) - `PAGES_PAGES_HOSTNAME` – the `site.github.pages_hostname` (default: `github.io`) - `NO_NETRC` – set if you don't want the fallback to `~/.netrc` +- `PAGES_DISABLE_NETWORK` – set to prevent all network accesses (disables features that need to access the GitHub API) ## Using with GitHub Enterprise diff --git a/lib/jekyll-github-metadata/client.rb b/lib/jekyll-github-metadata/client.rb index f655152..aab378b 100644 --- a/lib/jekyll-github-metadata/client.rb +++ b/lib/jekyll-github-metadata/client.rb @@ -110,15 +110,19 @@ def authenticated? def internet_connected? return @internet_connected if defined?(@internet_connected) - require "resolv" - begin - Resolv::DNS.open do |dns| - dns.timeouts = 2 - dns.getaddress("api.github.com") - end - @internet_connected = true - rescue Resolv::ResolvError + if ENV["PAGES_DISABLE_NETWORK"] @internet_connected = false + else + require "resolv" + begin + Resolv::DNS.open do |dns| + dns.timeouts = 2 + dns.getaddress("api.github.com") + end + @internet_connected = true + rescue Resolv::ResolvError + @internet_connected = false + end end end From e03ef04753185497d3b581bb3c6dac9b93cb5407 Mon Sep 17 00:00:00 2001 From: ibbem Date: Mon, 24 Feb 2025 09:57:23 +0100 Subject: [PATCH 2/3] fixup! Allow disabling of all network accesses --- History.markdown | 6 ------ spec/client_spec.rb | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/History.markdown b/History.markdown index e58937c..61a482c 100644 --- a/History.markdown +++ b/History.markdown @@ -1,9 +1,3 @@ -## HEAD - -## Minor Enhancements - - * Disable network related features if the env var `PAGES_DISABLE_NETWORK` is set - ## 2.16.1 / 2023-12-22 ### Bug Fixes diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 0d56158..d90f459 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -38,4 +38,21 @@ subject.contributors("jekyll/github-metadata") end.to raise_error(described_class::BadCredentialsError) end + + before { ENV["PAGES_DISABLE_NETWORK"] = nil } + + it "supresses network accesses if requested" do + WebMock.disable_net_connect! + + ENV["PAGES_DISABLE_NETWORK"] = "1" + expect(subject.contributors("jekyll/github-metadata")).to be(false) + end + + it "allows network accesses by default" do + WebMock.disable_net_connect! + + expect do + subject.contributors("jekyll/github-metadata") + end.to raise_error(WebMock::NetConnectNotAllowedError) + end end From 11b982b60dffdb793b0d2ed43c964c7ab7aa4bb3 Mon Sep 17 00:00:00 2001 From: ibbem Date: Mon, 24 Feb 2025 13:59:02 +0100 Subject: [PATCH 3/3] fixup! Allow disabling of all network accesses --- spec/client_spec.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index d90f459..7ab97f2 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -39,13 +39,12 @@ end.to raise_error(described_class::BadCredentialsError) end - before { ENV["PAGES_DISABLE_NETWORK"] = nil } - it "supresses network accesses if requested" do WebMock.disable_net_connect! - ENV["PAGES_DISABLE_NETWORK"] = "1" - expect(subject.contributors("jekyll/github-metadata")).to be(false) + with_env("PAGES_DISABLE_NETWORK", "1") do + expect(subject.contributors("jekyll/github-metadata")).to be(false) + end end it "allows network accesses by default" do