Skip to content

Commit afb0205

Browse files
committed
test(providers): strengthen git-transport regression and add clone e2e
Pin the exact allowed rule set for the built-in github git-transport endpoint in both the provider-profile and composed-policy tests, so a broader or additional POST rule (e.g. POST **) that could enable push via git-receive-pack fails the test instead of passing a substring check. Add an e2e test that attaches the built-in github provider and clones a public repo over HTTPS, exercising provider attachment, effective-policy composition, TLS interception, and real git behavior. Update the Providers V2 docs so the github.com git-transport endpoint shows explicit clone/fetch rules instead of the stale read-only preset. Refs #1769 Signed-off-by: Russell Bryant <[email protected]>
1 parent 7561835 commit afb0205

4 files changed

Lines changed: 182 additions & 25 deletions

File tree

‎crates/openshell-providers/src/profiles.rs‎

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,24 +1789,52 @@ mod tests {
17891789
.find(|endpoint| endpoint.host == "github.com" && endpoint.port == 443)
17901790
.expect("github.com git transport endpoint");
17911791

1792-
// Clone/fetch over git smart HTTP issues POST to */git-upload-pack.
1792+
// The git transport carries explicit rules rather than an access preset
1793+
// (an empty preset would otherwise expand to GET/HEAD/OPTIONS).
17931794
assert!(
1794-
git_transport.rules.iter().any(|rule| {
1795-
rule.allow.as_ref().is_some_and(|allow| {
1796-
allow.method == "POST" && allow.path.contains("git-upload-pack")
1797-
})
1798-
}),
1799-
"git transport must allow POST to */git-upload-pack for clone/fetch"
1795+
git_transport.access.is_empty(),
1796+
"git transport must use explicit rules, not an access preset"
18001797
);
18011798

1802-
// Push uses git-receive-pack and must stay blocked by default.
1803-
assert!(
1804-
!git_transport.rules.iter().any(|rule| {
1805-
rule.allow
1799+
// Assert the EXACT allowed rule set. Clone/fetch over git smart HTTP
1800+
// performs GET */info/refs (ref discovery) followed by POST
1801+
// */git-upload-pack. A substring check alone is not enough: a broader or
1802+
// additional POST rule (e.g. POST **) would also permit push via
1803+
// git-receive-pack while still passing a "some rule allows upload-pack"
1804+
// check. Pinning the whole set fails on any such regression. See #1769.
1805+
let mut allowed: Vec<(&str, &str)> = git_transport
1806+
.rules
1807+
.iter()
1808+
.map(|rule| {
1809+
let allow = rule
1810+
.allow
18061811
.as_ref()
1807-
.is_some_and(|allow| allow.path.contains("git-receive-pack"))
1808-
}),
1809-
"git transport must not allow git-receive-pack (push)"
1812+
.expect("git transport rules must be allow rules");
1813+
(allow.method.as_str(), allow.path.as_str())
1814+
})
1815+
.collect();
1816+
allowed.sort_unstable();
1817+
1818+
let mut expected = vec![
1819+
("GET", "**"),
1820+
("HEAD", "**"),
1821+
("OPTIONS", "**"),
1822+
("POST", "/**/git-upload-pack"),
1823+
];
1824+
expected.sort_unstable();
1825+
1826+
assert_eq!(
1827+
allowed, expected,
1828+
"git transport allow rules must be exactly the read-only methods \
1829+
plus POST */git-upload-pack (clone/fetch); a broader or extra POST \
1830+
rule would enable push (git-receive-pack)"
1831+
);
1832+
1833+
// Blocking push must not depend on a deny rule, which could mask an
1834+
// over-broad allow and hide a regression.
1835+
assert!(
1836+
git_transport.deny_rules.is_empty(),
1837+
"git transport should block push via its narrow allow set, not deny rules"
18101838
);
18111839
}
18121840

‎crates/openshell-server/src/grpc/policy.rs‎

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5127,16 +5127,44 @@ mod tests {
51275127
}),
51285128
"github API endpoints should be read-only; git transport uses explicit rules"
51295129
);
5130+
// Pin the exact composed rule set for the git transport. Clone/fetch
5131+
// needs GET */info/refs then POST */git-upload-pack; a broader or extra
5132+
// POST rule (e.g. POST **) would also permit push (git-receive-pack), so
5133+
// matching the whole set fails on any such regression (see #1769).
5134+
let git_transport = layers[0]
5135+
.rule
5136+
.endpoints
5137+
.iter()
5138+
.find(|endpoint| endpoint.host == "github.com")
5139+
.expect("composed policy should include the github.com git transport");
5140+
let mut allowed: Vec<(&str, &str)> = git_transport
5141+
.rules
5142+
.iter()
5143+
.map(|rule| {
5144+
let allow = rule
5145+
.allow
5146+
.as_ref()
5147+
.expect("git transport rules must be allow rules");
5148+
(allow.method.as_str(), allow.path.as_str())
5149+
})
5150+
.collect();
5151+
allowed.sort_unstable();
5152+
let mut expected = vec![
5153+
("GET", "**"),
5154+
("HEAD", "**"),
5155+
("OPTIONS", "**"),
5156+
("POST", "/**/git-upload-pack"),
5157+
];
5158+
expected.sort_unstable();
5159+
assert_eq!(
5160+
allowed, expected,
5161+
"composed git transport allow rules must be exactly the read-only \
5162+
methods plus POST */git-upload-pack; a broader POST rule would \
5163+
enable push (git-receive-pack)"
5164+
);
51305165
assert!(
5131-
layers[0].rule.endpoints.iter().any(|endpoint| {
5132-
endpoint.host == "github.com"
5133-
&& endpoint.rules.iter().any(|rule| {
5134-
rule.allow.as_ref().is_some_and(|allow| {
5135-
allow.method == "POST" && allow.path.contains("git-upload-pack")
5136-
})
5137-
})
5138-
}),
5139-
"github git transport should allow POST git-upload-pack for clone/fetch"
5166+
git_transport.deny_rules.is_empty(),
5167+
"composed git transport should block push via its narrow allow set, not deny rules"
51405168
);
51415169
}
51425170

‎docs/sandboxes/providers-v2.mdx‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,11 +605,17 @@ endpoints:
605605
- host: github.com
606606
port: 443
607607
protocol: rest
608-
access: read-only
609608
enforcement: enforce
609+
rules:
610+
- allow: { method: GET, path: "**" }
611+
- allow: { method: HEAD, path: "**" }
612+
- allow: { method: OPTIONS, path: "**" }
613+
- allow: { method: POST, path: "/**/git-upload-pack" }
610614
binaries: [/usr/bin/gh, /usr/local/bin/gh, /usr/bin/git, /usr/local/bin/git]
611615
```
612616

617+
The `github.com` git-transport endpoint uses explicit rules instead of the `read-only` preset so HTTPS clone and fetch work out of the box. Git smart HTTP performs a `GET` on `*/info/refs` followed by a `POST` to `*/git-upload-pack`; the read-only preset (`GET`/`HEAD`/`OPTIONS`) blocks that `POST`. The rules permit the read-only methods plus `POST */git-upload-pack` only, so clone and fetch succeed while push (`git-receive-pack`) and other API mutations stay denied. Enabling push requires an explicit policy proposal.
618+
613619
If a sandbox attaches a provider named `work-github`, the effective policy includes a generated provider rule:
614620

615621
```yaml wordWrap showLineNumbers={false}
@@ -642,8 +648,12 @@ network_policies:
642648
- host: github.com
643649
port: 443
644650
protocol: rest
645-
access: read-only
646651
enforcement: enforce
652+
rules:
653+
- allow: { method: GET, path: "**" }
654+
- allow: { method: HEAD, path: "**" }
655+
- allow: { method: OPTIONS, path: "**" }
656+
- allow: { method: POST, path: "/**/git-upload-pack" }
647657
binaries:
648658
- path: /usr/bin/gh
649659
- path: /usr/local/bin/gh

‎e2e/python/test_sandbox_providers.py‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,34 @@ def _delete_provider(stub: object, name: str) -> None:
9999
raise
100100

101101

102+
@contextmanager
103+
def _providers_v2_enabled(stub: object) -> Iterator[None]:
104+
"""Enable the gateway-global ``providers_v2_enabled`` opt-in for the block.
105+
106+
Composing a provider's network policy onto a sandbox is gated behind this
107+
setting, which defaults off. The built-in github profile's git-transport
108+
rules only reach the sandbox with it enabled. ``global`` is a Python keyword,
109+
so it is passed positionally through a dict expansion.
110+
"""
111+
stub.UpdateConfig(
112+
openshell_pb2.UpdateConfigRequest(
113+
setting_key="providers_v2_enabled",
114+
setting_value=sandbox_pb2.SettingValue(bool_value=True),
115+
**{"global": True},
116+
)
117+
)
118+
try:
119+
yield
120+
finally:
121+
stub.UpdateConfig(
122+
openshell_pb2.UpdateConfigRequest(
123+
setting_key="providers_v2_enabled",
124+
delete_setting=True,
125+
**{"global": True},
126+
)
127+
)
128+
129+
102130
# ===========================================================================
103131
# Tests: placeholder visibility
104132
# ===========================================================================
@@ -484,3 +512,66 @@ def test_update_provider_rejects_type_change(
484512
assert "type cannot be changed" in exc_info.value.details()
485513
finally:
486514
_delete_provider(stub, name)
515+
516+
517+
# ===========================================================================
518+
# Tests: git transport network policy
519+
# ===========================================================================
520+
521+
522+
def test_github_provider_allows_https_git_clone(
523+
sandbox: Callable[..., Sandbox],
524+
sandbox_client: SandboxClient,
525+
) -> None:
526+
"""Built-in github provider permits anonymous HTTPS clone/fetch (#1769).
527+
528+
Git smart HTTP clone/fetch issues a POST to ``*/git-upload-pack``. The
529+
read-only preset (GET/HEAD/OPTIONS) denied that POST, so ``git clone`` over
530+
HTTPS failed. Attaching the github provider composes its network policy onto
531+
the sandbox, exercising provider attachment, effective-policy composition,
532+
TLS interception, and real git behavior end to end. git delegates HTTPS to a
533+
``git-remote-https`` helper whose ancestor is ``/usr/bin/git``, so the
534+
profile's git binary covers it via ancestor matching.
535+
"""
536+
with _providers_v2_enabled(sandbox_client._stub), provider(
537+
sandbox_client._stub,
538+
name="e2e-test-github-clone",
539+
provider_type="github",
540+
# A required credential value is needed to create the provider, but an
541+
# anonymous clone of a public repo never uses it: git only sends the
542+
# token when a credential helper is configured.
543+
credentials={"GITHUB_TOKEN": "e2e-placeholder-unused"},
544+
) as provider_name:
545+
# git opens /dev/null O_RDWR, so it must be read-write; the shared
546+
# _default_policy only grants /dev/urandom. Everything else (binaries,
547+
# CA bundle, clone target) is covered by the standard allowlist.
548+
policy = _default_policy()
549+
policy.filesystem.read_write.append("/dev/null")
550+
spec = datamodel_pb2.SandboxSpec(
551+
policy=policy,
552+
providers=[provider_name],
553+
)
554+
555+
with sandbox(spec=spec, delete_on_exit=True) as sb:
556+
clone = sb.exec(
557+
[
558+
"git",
559+
"clone",
560+
"--depth",
561+
"1",
562+
"https://github.com/octocat/Hello-World.git",
563+
"/tmp/hello-world",
564+
],
565+
timeout_seconds=120,
566+
)
567+
assert clone.exit_code == 0, (
568+
"git clone over HTTPS should succeed with the github provider "
569+
f"attached; stdout={clone.stdout!r} stderr={clone.stderr!r}"
570+
)
571+
572+
# A completed clone materializes .git/HEAD, proving ref discovery
573+
# (GET) and upload-pack (POST) both succeeded, not just a handshake.
574+
head = sb.exec(["cat", "/tmp/hello-world/.git/HEAD"])
575+
assert head.exit_code == 0, (
576+
f"cloned repo is missing .git/HEAD; stderr={head.stderr!r}"
577+
)

0 commit comments

Comments
 (0)