Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b3ea0f5
feat(agent): read 2.0 request bodies into the published request types
CBenoit Nov 30, 2026
7193262
feat(agent): convert between the 2.0 request types and the policy eva…
CBenoit Nov 30, 2026
4e321a2
feat(agent): match a 2.0 request to its endpoint
CBenoit Nov 30, 2026
b118056
test(agent): check a repeated key in every 2.0 request body
CBenoit Nov 30, 2026
43873c8
feat(agent): build 2.0 error responses the contract allows
CBenoit Nov 30, 2026
3a44d67
test(agent): check an object written as the list of its members
CBenoit Nov 30, 2026
85b4c86
refactor(agent): let command builders take the elevation of the run
CBenoit Nov 30, 2026
52cebfe
feat(agent): let an event channel announce the version of its protocol
CBenoit Nov 30, 2026
431ee5f
feat(agent): hold the operations started by 2.0 requests
CBenoit Nov 30, 2026
42482f1
fix(agent): let nobody open the event pipe of a 2.0 operation for wri…
CBenoit Nov 30, 2026
4397c6d
test(agent): cite the rule behind refusing elevated pre and post comm…
CBenoit Nov 30, 2026
ffe8bd6
fix(agent): plan a 2.0 operation before locking the registry
CBenoit Nov 30, 2026
0f701ba
fix(agent): include pre-releases when a PowerShell command names one
CBenoit Nov 30, 2026
ad8f657
fix(agent): end a 2.0 operation whose task stops without an end
CBenoit Nov 30, 2026
b00c382
fix(agent): decide the access to an event pipe for each channel version
CBenoit Nov 30, 2026
a6393c6
fix(agent): end an operation without a panic on a poisoned event queue
CBenoit Nov 30, 2026
8699a82
fix(agent): escape a process name left out of a 2.0 operation in the log
CBenoit Nov 30, 2026
2d15c03
test(agent): give each row of a test table its own expected value
CBenoit Nov 30, 2026
452dea3
test(agent): read an execution request from a literal body
CBenoit Nov 30, 2026
e928d5c
test(agent): expect an ended operation gone only once the broker drop…
CBenoit Nov 30, 2026
9fad4fd
test(agent): tell two owners apart by a letter in paths of one length
CBenoit Nov 30, 2026
ee58b28
test(agent): cite the source that decides each expected value
CBenoit Nov 30, 2026
ef12c02
fix(agent): escape in the log the reason a 2.0 operation failed
CBenoit Dec 1, 2026
9ab7790
Merge branch 'claude/broker-v2-policy-7702d2' into claude/broker-v2-r…
CBenoit Dec 1, 2026
6c948aa
fix(agent): refuse a 2.0 update over its source record only when it n…
CBenoit Dec 1, 2026
63ef2e0
feat(agent): build the command of a 2.0 request that names no source
CBenoit Dec 1, 2026
acd0045
feat(agent): build the Scoop command of a 2.0 request that names no s…
CBenoit Dec 1, 2026
ae3da3d
feat(agent): build the Vcpkg command of a 2.0 request that names no s…
CBenoit Dec 1, 2026
9d2d757
test(agent): mark the refusal of an elevated plan without a source fo…
CBenoit Dec 1, 2026
0c1e8a8
docs(agent): name the leap second among the instants no 2.0 timestamp…
CBenoit Dec 1, 2026
f3e8676
Merge branch 'claude/broker-v2-policy-7702d2' into claude/broker-v2-r…
CBenoit Dec 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions crates/now-package-broker/src/command_builder/bun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use anyhow::bail;
use now_policy_api::{Architecture, Elevation, Operation, PackageRequest, Scope};

use super::{uses_default_source, validate_npm_package_name};
use super::{SourceChoice, uses_default_source, validate_npm_package_name};

const BUN_SOURCE_NAME: &str = "Bun";
const BUN_SOURCE_URL: &str = "https://www.npmjs.com";
Expand All @@ -22,7 +22,20 @@ const BUN_SOURCE_URL: &str = "https://www.npmjs.com";
///
/// Returns the command as a list of arguments (first element is the executable).
pub fn build_bun_command(request: &PackageRequest) -> anyhow::Result<Vec<String>> {
validate_bun_request(request)?;
build_bun_command_as(
request,
SourceChoice::Request,
crate::evaluator::effective_execution_elevation(request),
)
}

/// Build the command of a run whose token has the elevation `elevation`, on the source that `source` chooses.
pub(crate) fn build_bun_command_as(
request: &PackageRequest,
source: SourceChoice,
elevation: Elevation,
) -> anyhow::Result<Vec<String>> {
validate_bun_request(request, source, elevation)?;

let operation = match request.operation {
Operation::Install | Operation::Update => "add",
Expand All @@ -44,16 +57,16 @@ pub fn build_bun_command(request: &PackageRequest) -> anyhow::Result<Vec<String>
])
}

fn validate_bun_request(request: &PackageRequest) -> anyhow::Result<()> {
fn validate_bun_request(request: &PackageRequest, source: SourceChoice, elevation: Elevation) -> anyhow::Result<()> {
if request.options.scope == Some(Scope::Machine) {
bail!("machine-scope Bun package operations are not supported by the broker");
}

if crate::evaluator::effective_execution_elevation(request) == Elevation::Elevated {
if elevation == Elevation::Elevated {
bail!("elevated Bun package operations are not supported by the broker");
}

if !uses_default_source("Bun", request)?
if !uses_default_source("Bun", request, source)?
&& (!request.source.name.eq_ignore_ascii_case(BUN_SOURCE_NAME)
|| request
.source
Expand Down
23 changes: 18 additions & 5 deletions crates/now-package-broker/src/command_builder/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use anyhow::bail;
use now_policy_api::{Architecture, Elevation, Operation, PackageRequest, Scope};

use super::{set_if_specified, uses_default_source, validate_batch_arguments, validate_package_version};
use super::{SourceChoice, set_if_specified, uses_default_source, validate_batch_arguments, validate_package_version};

const CRATES_IO_SOURCE_NAME: &str = "crates.io";
const CRATES_IO_SOURCE_URL: &str = "https://index.crates.io";
Expand All @@ -14,7 +14,20 @@ const CARGO_VERSION_EXTRA_CHARACTERS: &[char] = &['=', '~', '*'];
///
/// Returns the command as a list of arguments (first element is the executable).
pub fn build_cargo_command(request: &PackageRequest) -> anyhow::Result<Vec<String>> {
validate_cargo_request(request)?;
build_cargo_command_as(
request,
SourceChoice::Request,
crate::evaluator::effective_execution_elevation(request),
)
}

/// Build the command of a run whose token has the elevation `elevation`, on the source that `source` chooses.
pub(crate) fn build_cargo_command_as(
request: &PackageRequest,
source: SourceChoice,
elevation: Elevation,
) -> anyhow::Result<Vec<String>> {
validate_cargo_request(request, source, elevation)?;

let operation = match request.operation {
Operation::Install | Operation::Update => "install",
Expand Down Expand Up @@ -60,14 +73,14 @@ pub fn build_cargo_command(request: &PackageRequest) -> anyhow::Result<Vec<Strin
Ok(command)
}

fn validate_cargo_request(request: &PackageRequest) -> anyhow::Result<()> {
fn validate_cargo_request(request: &PackageRequest, source: SourceChoice, elevation: Elevation) -> anyhow::Result<()> {
if request.options.scope == Some(Scope::Machine) {
bail!("cargo machine-scope operations are not supported by the broker");
}
if crate::evaluator::effective_execution_elevation(request) == Elevation::Elevated {
if elevation == Elevation::Elevated {
bail!("cargo elevated operations are not supported by the broker");
}
if !uses_default_source("cargo", request)? {
if !uses_default_source("cargo", request, source)? {
if !request.source.name.eq_ignore_ascii_case(CRATES_IO_SOURCE_NAME) {
bail!("cargo package source must be crates.io");
}
Expand Down
35 changes: 22 additions & 13 deletions crates/now-package-broker/src/command_builder/chocolatey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ use anyhow::bail;
use now_policy_api::{Architecture, Operation, PackageRequest, Scope};

use super::{
set_if_true, uses_default_source, validate_batch_arguments, validate_package_version, validate_source_name,
SourceChoice, set_if_true, uses_default_source, validate_batch_arguments, validate_package_version,
validate_source_name,
};

/// Build the Chocolatey command line from a validated request.
///
/// Returns the command as a list of arguments (first element is the executable).
pub fn build_chocolatey_command(request: &PackageRequest) -> anyhow::Result<Vec<String>> {
validate_chocolatey_request(request)?;
build_chocolatey_command_with(request, SourceChoice::Request)
}

/// Build the Chocolatey command line of a request, on the source that `source` chooses.
pub(crate) fn build_chocolatey_command_with(
request: &PackageRequest,
source: SourceChoice,
) -> anyhow::Result<Vec<String>> {
validate_chocolatey_request(request, source)?;

let operation = match request.operation {
Operation::Install => "install",
Expand All @@ -30,7 +39,7 @@ pub fn build_chocolatey_command(request: &PackageRequest) -> anyhow::Result<Vec<

if matches!(request.operation, Operation::Install | Operation::Update) {
command.push("--no-progress".to_owned());
if let Some(source) = chocolatey_source(request)? {
if let Some(source) = chocolatey_source(request, source)? {
command.push("--source".to_owned());
command.push(source.to_owned());
}
Expand All @@ -57,7 +66,7 @@ pub fn build_chocolatey_command(request: &PackageRequest) -> anyhow::Result<Vec<
Ok(command)
}

fn validate_chocolatey_request(request: &PackageRequest) -> anyhow::Result<()> {
fn validate_chocolatey_request(request: &PackageRequest, source: SourceChoice) -> anyhow::Result<()> {
validate_chocolatey_package_id(&request.package.id.0)?;

if request.source.url.is_some() {
Expand Down Expand Up @@ -116,7 +125,7 @@ fn validate_chocolatey_request(request: &PackageRequest) -> anyhow::Result<()> {
}

if matches!(request.operation, Operation::Install | Operation::Update) {
chocolatey_source(request)?;
chocolatey_source(request, source)?;
if let Some(version) = request.package.version.as_deref() {
validate_package_version("Chocolatey", version, &[])?;
}
Expand Down Expand Up @@ -163,22 +172,22 @@ fn validate_chocolatey_package_id(id: &str) -> anyhow::Result<()> {
const CHOCOLATEY_SOURCE_TYPES: [&str; 5] = ["cygwin", "python", "ruby", "windowsfeature", "windowsfeatures"];

/// The named source for `--source`, or `None` to use Chocolatey's configured default sources.
fn chocolatey_source(request: &PackageRequest) -> anyhow::Result<Option<&str>> {
if uses_default_source("Chocolatey", request)? {
fn chocolatey_source(request: &PackageRequest, source: SourceChoice) -> anyhow::Result<Option<&str>> {
if uses_default_source("Chocolatey", request, source)? {
return Ok(None);
}
let source = request.source.name.trim();
if source.is_empty() {
let name = request.source.name.trim();
if name.is_empty() {
bail!("Chocolatey package source name is required");
}
validate_source_name("Chocolatey", source)?;
validate_source_name("Chocolatey", name)?;
if CHOCOLATEY_SOURCE_TYPES
.iter()
.any(|source_type| source_type.eq_ignore_ascii_case(source))
.any(|source_type| source_type.eq_ignore_ascii_case(name))
{
bail!("Chocolatey package source name '{source}' selects a Chocolatey source type, not a configured source");
bail!("Chocolatey package source name '{name}' selects a Chocolatey source type, not a configured source");
}
Ok(Some(source))
Ok(Some(name))
}

#[cfg(test)]
Expand Down
30 changes: 21 additions & 9 deletions crates/now-package-broker/src/command_builder/dotnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use anyhow::bail;
use now_policy_api::{Architecture, Operation, PackageRequest, Scope};

use super::{set_if_specified, set_if_true, uses_default_source, validate_package_version};
use super::{SourceChoice, set_if_specified, set_if_true, uses_default_source, validate_package_version};

const NUGET_ORG_V3_SOURCE: &str = "https://api.nuget.org/v3/index.json";
/// NuGet version range syntax, such as `[1.0,2.0)` or `1.*`.
Expand All @@ -18,7 +18,12 @@ const DOTNET_VERSION_RANGE_CHARACTERS: &[char] = &['[', ']', '(', ')', ',', '*']
///
/// Returns the command as a list of arguments (first element is the executable).
pub fn build_dotnet_command(request: &PackageRequest) -> anyhow::Result<Vec<String>> {
validate_dotnet_request(request)?;
build_dotnet_command_with(request, SourceChoice::Request)
}

/// Build the .NET tool command line of a request, on the source that `source` chooses.
pub(crate) fn build_dotnet_command_with(request: &PackageRequest, source: SourceChoice) -> anyhow::Result<Vec<String>> {
validate_dotnet_request(request, source)?;

let operation = match request.operation {
Operation::Install => "install",
Expand All @@ -44,7 +49,7 @@ pub fn build_dotnet_command(request: &PackageRequest) -> anyhow::Result<Vec<Stri
}

if matches!(request.operation, Operation::Install | Operation::Update) {
append_source(&mut command, request)?;
append_source(&mut command, request, source)?;
set_if_specified(&mut command, "--version", request.package.version.as_deref());
set_if_true(&mut command, "--prerelease", request.options.pre_release);

Expand All @@ -63,7 +68,7 @@ pub fn build_dotnet_command(request: &PackageRequest) -> anyhow::Result<Vec<Stri
Ok(command)
}

fn validate_dotnet_request(request: &PackageRequest) -> anyhow::Result<()> {
fn validate_dotnet_request(request: &PackageRequest, source: SourceChoice) -> anyhow::Result<()> {
// `dotnet tool` reads an element starting with `-` as an option, so such an id names no package.
if request.package.id.0.starts_with('-') {
bail!(".NET package id cannot start with a hyphen");
Expand Down Expand Up @@ -93,7 +98,7 @@ fn validate_dotnet_request(request: &PackageRequest) -> anyhow::Result<()> {
bail!(".NET prerelease selection is not supported for uninstall operations");
}
if matches!(request.operation, Operation::Install | Operation::Update) {
dotnet_source(request)?;
dotnet_source(request, source)?;
if let Some(version) = request.package.version.as_deref() {
validate_package_version(".NET", version, DOTNET_VERSION_RANGE_CHARACTERS)?;
}
Expand All @@ -102,17 +107,24 @@ fn validate_dotnet_request(request: &PackageRequest) -> anyhow::Result<()> {
Ok(())
}

fn append_source(command: &mut Vec<String>, request: &PackageRequest) -> anyhow::Result<()> {
if let Some(source) = dotnet_source(request)? {
fn append_source(command: &mut Vec<String>, request: &PackageRequest, source: SourceChoice) -> anyhow::Result<()> {
if let Some(source) = dotnet_source(request, source)? {
command.push("--source".to_owned());
command.push(source.to_owned());
}
Ok(())
}

/// The canonical feed to pass as `--source`, or `None` to use the NuGet configuration's default feeds.
fn dotnet_source(request: &PackageRequest) -> anyhow::Result<Option<&'static str>> {
if uses_default_source(".NET", request)? {
fn dotnet_source(request: &PackageRequest, source: SourceChoice) -> anyhow::Result<Option<&'static str>> {
// A request without a source is read with `nuget.org`, the only source name of an install or update
// ([§bypass-reading]), so its command is the one of that name.
//
// [§bypass-reading]: https://github.com/Devolutions/now-libraries/blob/master/policies/CONTRACT.wire.md#bypass-reading
if source == SourceChoice::Manager {
return Ok(Some(NUGET_ORG_V3_SOURCE));
}
if uses_default_source(".NET", request, source)? {
return Ok(None);
}

Expand Down
57 changes: 53 additions & 4 deletions crates/now-package-broker/src/command_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod vcpkg;
pub mod winget;

use anyhow::bail;
use now_policy_api::{ManagerName, Operation, PackageRequest};
use now_policy_api::{Elevation, ManagerName, Operation, PackageRequest};

/// Build a command line from a validated request, dispatching to the appropriate
/// package manager builder.
Expand Down Expand Up @@ -51,6 +51,32 @@ fn build_manager_command(request: &PackageRequest) -> anyhow::Result<Vec<String>
}
}

/// Build the command line of a run whose token elevation the caller states, with the installer arguments it carries.
///
/// [`build_command`] derives the elevation from the request's requested elevation and scope, and passes no installer
/// arguments; this serves a request that states its run elevation itself, and may have no source.
pub(crate) fn build_command_as(
request: &PackageRequest,
source: SourceChoice,
elevation: Elevation,
installer_arguments: Option<winget::InstallerArguments<'_>>,
) -> anyhow::Result<Vec<String>> {
match request.manager {
Comment on lines +58 to +64
ManagerName::Bun => bun::build_bun_command_as(request, source, elevation),
ManagerName::Cargo => cargo::build_cargo_command_as(request, source, elevation),
ManagerName::Dotnet => dotnet::build_dotnet_command_with(request, source),
ManagerName::Npm => npm::build_npm_command_as(request, source, elevation),
ManagerName::Winget => winget::build_winget_command_with(request, source, installer_arguments),
ManagerName::PowerShell => powershell::build_powershell5_command_as(request, source, elevation),
ManagerName::PowerShell7 => powershell::build_powershell7_command_as(request, source, elevation),
ManagerName::Chocolatey => chocolatey::build_chocolatey_command_with(request, source),
ManagerName::Pip => pip::build_pip_command_as(request, source, elevation),
ManagerName::Scoop => scoop::build_scoop_command_as(request, source, elevation),
ManagerName::Vcpkg => vcpkg::build_vcpkg_command_as(request, source, elevation),
unsupported => bail!("package manager is not supported by the broker: {unsupported}"),
}
}

/// Build a command line for a remote grant.
///
/// Unlike [`build_command`], this allows a PowerShell 7 update with an empty source and scopes its uninstalls.
Expand Down Expand Up @@ -126,11 +152,34 @@ pub(crate) fn validate_source_name(manager: &str, name: &str) -> anyhow::Result<
Ok(())
}

/// Who chooses the source that the command of a request acts on.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SourceChoice {
/// The request, by its source name.
///
/// An empty name builds no install, and leaves an update or an uninstall to the sources its manager is
/// configured with.
Request,
/// The manager, for a request that has no source ([§bypass-reading]).
///
/// The source name of the request is not read.
///
/// [§bypass-reading]: https://github.com/Devolutions/now-libraries/blob/master/policies/CONTRACT.wire.md#bypass-reading
Manager,
}

/// Report whether the request leaves the source to the manager's ordinary default.
///
/// Only update and uninstall may carry an empty source name, and never with a URL.
/// An install must always name its source.
pub(crate) fn uses_default_source(manager: &str, request: &PackageRequest) -> anyhow::Result<bool> {
/// A request that chooses its source may carry an empty source name only for update and uninstall, and never with a
/// URL: its install must always name its source.
pub(crate) fn uses_default_source(
manager: &str,
request: &PackageRequest,
source: SourceChoice,
) -> anyhow::Result<bool> {
if source == SourceChoice::Manager {
return Ok(true);
}
if !request.source.name.is_empty() {
return Ok(false);
}
Expand Down
25 changes: 20 additions & 5 deletions crates/now-package-broker/src/command_builder/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use anyhow::bail;
use now_policy_api::{Architecture, Elevation, Operation, PackageRequest, Scope};

use super::{quote_powershell_literal, uses_default_source, validate_npm_package_name, validate_package_version};
use super::{
SourceChoice, quote_powershell_literal, uses_default_source, validate_npm_package_name, validate_package_version,
};

/// npm version selectors that are inert when passed through the `npm.cmd` shim, such as `~1.2` or `1.x`.
const NPM_VERSION_EXTRA_CHARACTERS: &[char] = &['~', '*'];
Expand All @@ -28,7 +30,20 @@ const SCRIPT_PREFIX: &str = "$ErrorActionPreference = 'Stop'; \

/// Build an npm command from a validated request.
pub fn build_npm_command(request: &PackageRequest) -> anyhow::Result<Vec<String>> {
validate_npm_request(request)?;
build_npm_command_as(
request,
SourceChoice::Request,
crate::evaluator::effective_execution_elevation(request),
)
}

/// Build the command of a run whose token has the elevation `elevation`, on the source that `source` chooses.
pub(crate) fn build_npm_command_as(
request: &PackageRequest,
source: SourceChoice,
elevation: Elevation,
) -> anyhow::Result<Vec<String>> {
validate_npm_request(request, source, elevation)?;

let mut script = String::from(SCRIPT_PREFIX);

Expand All @@ -55,17 +70,17 @@ pub fn build_npm_command(request: &PackageRequest) -> anyhow::Result<Vec<String>
])
}

fn validate_npm_request(request: &PackageRequest) -> anyhow::Result<()> {
fn validate_npm_request(request: &PackageRequest, source: SourceChoice, elevation: Elevation) -> anyhow::Result<()> {
if request.options.scope == Some(Scope::Machine) {
bail!("npm machine-scope operations are not supported by the broker");
}
if crate::evaluator::effective_execution_elevation(request) == Elevation::Elevated {
if elevation == Elevation::Elevated {
bail!("npm elevated operations are not supported by the broker");
}
if request.source.url.is_some() {
bail!("npm package sources with URLs are not supported by the broker");
}
if !uses_default_source("npm", request)? && !request.source.name.eq_ignore_ascii_case("npm") {
if !uses_default_source("npm", request, source)? && !request.source.name.eq_ignore_ascii_case("npm") {
bail!("npm package source must be 'npm'");
}
match request.package.architecture {
Expand Down
Loading
Loading