Skip to content

Commit 17cc5af

Browse files
authored
Allow disabling of all network accesses (#285)
Merge pull request 285
1 parent a605d04 commit 17cc5af

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Some `site.github` values can be overridden by environment variables.
4444
- `PAGES_GITHUB_HOSTNAME` – the `site.github.hostname` (default: `github.com`)
4545
- `PAGES_PAGES_HOSTNAME` – the `site.github.pages_hostname` (default: `github.io`)
4646
- `NO_NETRC` – set if you don't want the fallback to `~/.netrc`
47+
- `PAGES_DISABLE_NETWORK` – set to prevent all network accesses (disables features that need to access the GitHub API)
4748

4849
## Using with GitHub Enterprise
4950

lib/jekyll-github-metadata/client.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,19 @@ def authenticated?
110110
def internet_connected?
111111
return @internet_connected if defined?(@internet_connected)
112112

113-
require "resolv"
114-
begin
115-
Resolv::DNS.open do |dns|
116-
dns.timeouts = 2
117-
dns.getaddress("api.github.com")
118-
end
119-
@internet_connected = true
120-
rescue Resolv::ResolvError
113+
if ENV["PAGES_DISABLE_NETWORK"]
121114
@internet_connected = false
115+
else
116+
require "resolv"
117+
begin
118+
Resolv::DNS.open do |dns|
119+
dns.timeouts = 2
120+
dns.getaddress("api.github.com")
121+
end
122+
@internet_connected = true
123+
rescue Resolv::ResolvError
124+
@internet_connected = false
125+
end
122126
end
123127
end
124128

spec/client_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,20 @@
3838
subject.contributors("jekyll/github-metadata")
3939
end.to raise_error(described_class::BadCredentialsError)
4040
end
41+
42+
it "supresses network accesses if requested" do
43+
WebMock.disable_net_connect!
44+
45+
with_env("PAGES_DISABLE_NETWORK", "1") do
46+
expect(subject.contributors("jekyll/github-metadata")).to be(false)
47+
end
48+
end
49+
50+
it "allows network accesses by default" do
51+
WebMock.disable_net_connect!
52+
53+
expect do
54+
subject.contributors("jekyll/github-metadata")
55+
end.to raise_error(WebMock::NetConnectNotAllowedError)
56+
end
4157
end

0 commit comments

Comments
 (0)