@@ -9124,6 +9124,107 @@ process: { run_as_user: sandbox, run_as_group: sandbox }
91249124 );
91259125 }
91269126
9127+ /// A kept answer gives no access the current policy does not: once its
9128+ /// host leaves the policy, or the policy is quarantined, it is refused by
9129+ /// the policy decision.
9130+ #[tokio::test]
9131+ async fn policy_dns_answer_is_refused_once_the_policy_no_longer_allows_it() {
9132+ let rego = include_str!("../data/sandbox-policy.rego");
9133+ let engine = Arc::new(native_identity_engine(rego, POLICY_DNS_OPEN_POLICY).unwrap());
9134+ let store = policy_dns_test_store();
9135+ let without_pypi = POLICY_DNS_OPEN_POLICY.replace(
9136+ " - { host: pypi.org, port: 80 }",
9137+ " - { host: files.pythonhosted.org, port: 80 }",
9138+ );
9139+ let kept = keep_pypi_answer_across(&engine, &store, &without_pypi).await;
9140+ assert_eq!(
9141+ admit_staged(&store, &engine, SocketAddr::new(kept, 80)).await,
9142+ (false, TcpOpenDecision::Denied(TcpOpenDenial::PolicyDenied))
9143+ );
9144+
9145+ let engine = Arc::new(native_identity_engine(rego, POLICY_DNS_OPEN_POLICY).unwrap());
9146+ let store = policy_dns_test_store();
9147+ let approved = POLICY_DNS_OPEN_POLICY.replace(
9148+ "filesystem_policy:",
9149+ " github:\n name: github\n endpoints:\n - { host: github.com, port: 443 }\n binaries: [{ path: /usr/bin/curl }]\nfilesystem_policy:",
9150+ );
9151+ let kept = keep_pypi_answer_across(&engine, &store, &approved).await;
9152+ engine.enter_fail_closed("test quarantine").unwrap();
9153+ let (admitted, _) = admit_staged(&store, &engine, SocketAddr::new(kept, 80)).await;
9154+ assert!(!admitted);
9155+ }
9156+
9157+ /// One name allowed for two binaries with different destination rules:
9158+ /// psql may reach a private address of its exact host, curl only public
9159+ /// addresses through a wildcard. The answer pins both addresses, and each
9160+ /// connection reaches only what its own endpoint permits.
9161+ #[tokio::test]
9162+ async fn policy_dns_answer_reaches_only_what_the_connecting_binary_may() {
9163+ let policy = r"
9164+ network_policies:
9165+ database:
9166+ name: database
9167+ endpoints:
9168+ - { host: db.service.example, port: 5432, protocol: tcp }
9169+ binaries: [{ path: /usr/bin/psql }]
9170+ service:
9171+ name: service
9172+ endpoints:
9173+ - { host: '*.service.example', port: 5432, protocol: tcp }
9174+ binaries: [{ path: /usr/bin/curl }]
9175+ filesystem_policy: { include_workdir: true, read_only: [], read_write: [] }
9176+ landlock: { compatibility: best_effort }
9177+ process: { run_as_user: sandbox, run_as_group: sandbox }
9178+ ";
9179+ let rego = include_str!("../data/sandbox-policy.rego");
9180+ let engine = Arc::new(
9181+ native_identity_engine(rego, &policy_with_native_executable_paths(policy)).unwrap(),
9182+ );
9183+ let store = policy_dns_test_store();
9184+ let dns = crate::policy_dns::test_service(
9185+ engine.clone(),
9186+ store.clone(),
9187+ vec!["10.2.3.4".parse().unwrap(), "8.8.8.8".parse().unwrap()],
9188+ );
9189+ let answer = dns
9190+ .answer_query(
9191+ "db.service.example",
9192+ crate::policy_dns::AddressFamily::Ipv4,
9193+ std::time::Instant::now(),
9194+ )
9195+ .await
9196+ .unwrap();
9197+ let mapping = store
9198+ .lookup(answer.address, 5432, std::time::Instant::now())
9199+ .unwrap();
9200+ assert_eq!(
9201+ mapping.record.pinned_addresses,
9202+ [
9203+ "10.2.3.4".parse::<IpAddr>().unwrap(),
9204+ "8.8.8.8".parse().unwrap()
9205+ ]
9206+ );
9207+
9208+ let destination = SocketAddr::new(answer.address, 5432);
9209+ let (open, completion) = staged_curl_open(destination, engine.current_generation());
9210+ let (_, _, _, transparent) = preauthorize_transparent_open(
9211+ open,
9212+ Some(&store),
9213+ &engine,
9214+ &BinaryIdentityCache::new(),
9215+ None,
9216+ None,
9217+ false,
9218+ None,
9219+ None,
9220+ )
9221+ .await
9222+ .expect("curl may reach the public address");
9223+ assert_eq!(completion.await.unwrap(), TcpOpenDecision::RelayReady);
9224+ let (_, connector) = transparent.and_then(|open| open.authorization).unwrap();
9225+ assert_eq!(connector.addrs(), &["8.8.8.8:5432".parse().unwrap()]);
9226+ }
9227+
91279228 #[test]
91289229 fn staged_denial_names_the_mapped_host_and_keeps_the_synthetic_address() {
91299230 let (open, _) = staged_curl_open("198.18.0.2:80".parse().unwrap(), 1);
0 commit comments