@@ -9725,6 +9725,107 @@ process: { run_as_user: sandbox, run_as_group: sandbox }
97259725 );
97269726 }
97279727
9728+ /// A kept answer gives no access the current policy does not: once its
9729+ /// host leaves the policy, or the policy is quarantined, it is refused by
9730+ /// the policy decision.
9731+ #[tokio::test]
9732+ async fn policy_dns_answer_is_refused_once_the_policy_no_longer_allows_it() {
9733+ let rego = include_str!("../data/sandbox-policy.rego");
9734+ let engine = Arc::new(native_identity_engine(rego, POLICY_DNS_OPEN_POLICY).unwrap());
9735+ let store = policy_dns_test_store();
9736+ let without_pypi = POLICY_DNS_OPEN_POLICY.replace(
9737+ " - { host: pypi.org, port: 80 }",
9738+ " - { host: files.pythonhosted.org, port: 80 }",
9739+ );
9740+ let kept = keep_pypi_answer_across(&engine, &store, &without_pypi).await;
9741+ assert_eq!(
9742+ admit_staged(&store, &engine, SocketAddr::new(kept, 80)).await,
9743+ (false, TcpOpenDecision::Denied(TcpOpenDenial::PolicyDenied))
9744+ );
9745+
9746+ let engine = Arc::new(native_identity_engine(rego, POLICY_DNS_OPEN_POLICY).unwrap());
9747+ let store = policy_dns_test_store();
9748+ let approved = POLICY_DNS_OPEN_POLICY.replace(
9749+ "filesystem_policy:",
9750+ " github:\n name: github\n endpoints:\n - { host: github.com, port: 443 }\n binaries: [{ path: /usr/bin/curl }]\nfilesystem_policy:",
9751+ );
9752+ let kept = keep_pypi_answer_across(&engine, &store, &approved).await;
9753+ engine.enter_fail_closed("test quarantine").unwrap();
9754+ let (admitted, _) = admit_staged(&store, &engine, SocketAddr::new(kept, 80)).await;
9755+ assert!(!admitted);
9756+ }
9757+
9758+ /// One name allowed for two binaries with different destination rules:
9759+ /// psql may reach a private address of its exact host, curl only public
9760+ /// addresses through a wildcard. The answer pins both addresses, and each
9761+ /// connection reaches only what its own endpoint permits.
9762+ #[tokio::test]
9763+ async fn policy_dns_answer_reaches_only_what_the_connecting_binary_may() {
9764+ let policy = r"
9765+ network_policies:
9766+ database:
9767+ name: database
9768+ endpoints:
9769+ - { host: db.service.example, port: 5432, protocol: tcp }
9770+ binaries: [{ path: /usr/bin/psql }]
9771+ service:
9772+ name: service
9773+ endpoints:
9774+ - { host: '*.service.example', port: 5432, protocol: tcp }
9775+ binaries: [{ path: /usr/bin/curl }]
9776+ filesystem_policy: { include_workdir: true, read_only: [], read_write: [] }
9777+ landlock: { compatibility: best_effort }
9778+ process: { run_as_user: sandbox, run_as_group: sandbox }
9779+ ";
9780+ let rego = include_str!("../data/sandbox-policy.rego");
9781+ let engine = Arc::new(
9782+ native_identity_engine(rego, &policy_with_native_executable_paths(policy)).unwrap(),
9783+ );
9784+ let store = policy_dns_test_store();
9785+ let dns = crate::policy_dns::test_service(
9786+ engine.clone(),
9787+ store.clone(),
9788+ vec!["10.2.3.4".parse().unwrap(), "8.8.8.8".parse().unwrap()],
9789+ );
9790+ let answer = dns
9791+ .answer_query(
9792+ "db.service.example",
9793+ crate::policy_dns::AddressFamily::Ipv4,
9794+ std::time::Instant::now(),
9795+ )
9796+ .await
9797+ .unwrap();
9798+ let mapping = store
9799+ .lookup(answer.address, 5432, std::time::Instant::now())
9800+ .unwrap();
9801+ assert_eq!(
9802+ mapping.record.pinned_addresses,
9803+ [
9804+ "10.2.3.4".parse::<IpAddr>().unwrap(),
9805+ "8.8.8.8".parse().unwrap()
9806+ ]
9807+ );
9808+
9809+ let destination = SocketAddr::new(answer.address, 5432);
9810+ let (open, completion) = staged_curl_open(destination, engine.current_generation());
9811+ let (_, _, _, transparent) = preauthorize_transparent_open(
9812+ open,
9813+ Some(&policy_dns_test_service(&store, &engine)),
9814+ &engine,
9815+ &BinaryIdentityCache::new(),
9816+ None,
9817+ None,
9818+ false,
9819+ None,
9820+ None,
9821+ )
9822+ .await
9823+ .expect("curl may reach the public address");
9824+ assert_eq!(completion.await.unwrap(), TcpOpenDecision::RelayReady);
9825+ let (_, connector) = transparent.and_then(|open| open.authorization).unwrap();
9826+ assert_eq!(connector.addrs(), &["8.8.8.8:5432".parse().unwrap()]);
9827+ }
9828+
97289829 #[test]
97299830 fn staged_denial_names_the_mapped_host_and_keeps_the_synthetic_address() {
97309831 let (open, _) = staged_curl_open("198.18.0.2:80".parse().unwrap(), 1);
0 commit comments