Skip to content

Commit defa7c3

Browse files
committed
feat!: add Repository::excludes() and simplify signature of Worktree::excludes().
Further, this change removes the `permission` module without replacement, and moves `permissions` into `open`. This corrects an artifact of this crate previously being name `gix-repository` and brings these types semantically closer to where they are actually used.
1 parent affa2a5 commit defa7c3

File tree

17 files changed

+332
-102
lines changed

17 files changed

+332
-102
lines changed

gix/src/config/cache/access.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl Cache {
197197
&self,
198198
git_dir: &std::path::Path,
199199
source: gix_worktree::cache::state::attributes::Source,
200-
attributes: crate::permissions::Attributes,
200+
attributes: crate::open::permissions::Attributes,
201201
) -> Result<gix_worktree::cache::state::Attributes, config::attribute_stack::Error> {
202202
let configured_or_user_attributes = match self
203203
.trusted_file_path("core", None, Core::ATTRIBUTES_FILE.name)

gix/src/config/cache/init.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
tree::{gitoxide, Core, Http},
1313
Cache,
1414
},
15-
repository,
15+
open,
1616
};
1717

1818
/// Initialization
@@ -32,24 +32,24 @@ impl Cache {
3232
filter_config_section: fn(&gix_config::file::Metadata) -> bool,
3333
git_install_dir: Option<&std::path::Path>,
3434
home: Option<&std::path::Path>,
35-
environment @ repository::permissions::Environment {
35+
environment @ open::permissions::Environment {
3636
git_prefix,
3737
ssh_prefix: _,
3838
xdg_config_home: _,
3939
home: _,
4040
http_transport,
4141
identity,
4242
objects,
43-
}: repository::permissions::Environment,
44-
attributes: repository::permissions::Attributes,
45-
repository::permissions::Config {
43+
}: open::permissions::Environment,
44+
attributes: open::permissions::Attributes,
45+
open::permissions::Config {
4646
git_binary: use_installation,
4747
system: use_system,
4848
git: use_git,
4949
user: use_user,
5050
env: use_env,
5151
includes: use_includes,
52-
}: repository::permissions::Config,
52+
}: open::permissions::Config,
5353
lenient_config: bool,
5454
api_config_overrides: &[BString],
5555
cli_config_overrides: &[BString],
@@ -233,12 +233,12 @@ impl Cache {
233233
}
234234

235235
pub(crate) fn make_source_env(
236-
crate::permissions::Environment {
236+
crate::open::permissions::Environment {
237237
xdg_config_home,
238238
git_prefix,
239239
home,
240240
..
241-
}: crate::permissions::Environment,
241+
}: open::permissions::Environment,
242242
) -> impl FnMut(&str) -> Option<OsString> {
243243
move |name| {
244244
match name {

gix/src/config/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ pub(crate) struct Cache {
462462
/// If true, we should default what's possible if something is misconfigured, on case by case basis, to be more resilient.
463463
/// Also available in options! Keep in sync!
464464
pub lenient_config: bool,
465-
attributes: crate::permissions::Attributes,
466-
environment: crate::permissions::Environment,
465+
attributes: crate::open::permissions::Attributes,
466+
environment: crate::open::permissions::Environment,
467467
// TODO: make core.precomposeUnicode available as well.
468468
}

gix/src/lib.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub mod head;
127127
pub mod id;
128128
pub mod object;
129129
pub mod reference;
130-
mod repository;
130+
pub mod repository;
131131
pub mod tag;
132132

133133
///
@@ -223,13 +223,6 @@ pub fn open_opts(directory: impl Into<std::path::PathBuf>, options: open::Option
223223
ThreadSafeRepository::open_opts(directory, options).map(Into::into)
224224
}
225225

226-
///
227-
pub mod permission;
228-
229-
///
230-
pub mod permissions;
231-
pub use repository::permissions::Permissions;
232-
233226
///
234227
pub mod create;
235228

gix/src/open/mod.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
use std::path::PathBuf;
22

3-
use crate::{bstr::BString, config, permission, Permissions};
3+
use crate::{bstr::BString, config};
4+
5+
/// Permissions associated with various resources of a git repository
6+
#[derive(Debug, Clone)]
7+
pub struct Permissions {
8+
/// Control which environment variables may be accessed.
9+
pub env: permissions::Environment,
10+
/// Permissions related where git configuration should be loaded from.
11+
pub config: permissions::Config,
12+
/// Permissions related to where `gitattributes` should be loaded from.
13+
pub attributes: permissions::Attributes,
14+
}
415

516
/// The options used in [`ThreadSafeRepository::open_opts()`][crate::ThreadSafeRepository::open_opts()].
617
///
@@ -44,11 +55,11 @@ pub enum Error {
4455
#[error("The git directory at '{}' is considered unsafe as it's not owned by the current user.", .path.display())]
4556
UnsafeGitDir { path: PathBuf },
4657
#[error(transparent)]
47-
EnvironmentAccessDenied(#[from] permission::env_var::resource::Error),
58+
EnvironmentAccessDenied(#[from] gix_sec::permission::Error<std::path::PathBuf>),
4859
}
4960

5061
mod options;
51-
62+
pub mod permissions;
5263
mod repository;
5364

5465
#[cfg(test)]

gix/src/open/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::PathBuf;
22

33
use super::{Error, Options};
4-
use crate::{bstr::BString, config, Permissions, ThreadSafeRepository};
4+
use crate::{bstr::BString, config, open::Permissions, ThreadSafeRepository};
55

66
impl Default for Options {
77
fn default() -> Self {

gix/src/open/permissions.rs

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
//! Various permissions to define what can be done when operating a [`Repository`][crate::Repository].
2+
use crate::open::Permissions;
3+
use gix_sec::Trust;
4+
5+
/// Configure from which sources git configuration may be loaded.
6+
///
7+
/// Note that configuration from inside of the repository is always loaded as it's definitely required for correctness.
8+
#[derive(Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Debug, Hash)]
9+
pub struct Config {
10+
/// The git binary may come with configuration as part of its configuration, and if this is true (default false)
11+
/// we will load the configuration of the git binary, if present and not a duplicate of the ones below.
12+
///
13+
/// It's disabled by default as it may involve executing the git binary once per execution of the application.
14+
pub git_binary: bool,
15+
/// Whether to use the system configuration.
16+
/// This is defined as `$(prefix)/etc/gitconfig` on unix.
17+
pub system: bool,
18+
/// Whether to use the git application configuration.
19+
///
20+
/// A platform defined location for where a user's git application configuration should be located.
21+
/// If `$XDG_CONFIG_HOME` is not set or empty, `$HOME/.config/git/config` will be used
22+
/// on unix.
23+
pub git: bool,
24+
/// Whether to use the user configuration.
25+
/// This is usually `~/.gitconfig` on unix.
26+
pub user: bool,
27+
/// Whether to use the configuration from environment variables.
28+
pub env: bool,
29+
/// Whether to follow include files are encountered in loaded configuration,
30+
/// via `include` and `includeIf` sections.
31+
pub includes: bool,
32+
}
33+
34+
impl Config {
35+
/// Allow everything which usually relates to a fully trusted environment
36+
pub fn all() -> Self {
37+
Config {
38+
git_binary: false,
39+
system: true,
40+
git: true,
41+
user: true,
42+
env: true,
43+
includes: true,
44+
}
45+
}
46+
47+
/// Load only configuration local to the git repository.
48+
pub fn isolated() -> Self {
49+
Config {
50+
git_binary: false,
51+
system: false,
52+
git: false,
53+
user: false,
54+
env: false,
55+
includes: false,
56+
}
57+
}
58+
}
59+
60+
impl Default for Config {
61+
fn default() -> Self {
62+
Self::all()
63+
}
64+
}
65+
66+
/// Configure from which `gitattribute` files may be loaded.
67+
///
68+
/// Note that `.gitattribute` files from within the repository are always loaded.
69+
#[derive(Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Debug, Hash)]
70+
pub struct Attributes {
71+
/// The git binary may come with attribute configuration in its installation directory, and if this is true (default false)
72+
/// we will load the configuration of the git binary.
73+
///
74+
/// It's disabled by default as it involves executing the git binary once per execution of the application.
75+
pub git_binary: bool,
76+
/// Whether to use the system configuration.
77+
/// This is typically defined as `$(prefix)/etc/gitconfig`.
78+
pub system: bool,
79+
/// Whether to use the git application configuration.
80+
///
81+
/// A platform defined location for where a user's git application configuration should be located.
82+
/// If `$XDG_CONFIG_HOME` is not set or empty, `$HOME/.config/git/attributes` will be used
83+
/// on unix.
84+
pub git: bool,
85+
}
86+
87+
impl Attributes {
88+
/// Allow everything which usually relates to a fully trusted environment
89+
pub fn all() -> Self {
90+
Attributes {
91+
git_binary: false,
92+
system: true,
93+
git: true,
94+
}
95+
}
96+
97+
/// Allow loading attributes that are local to the git repository.
98+
pub fn isolated() -> Self {
99+
Attributes {
100+
git_binary: false,
101+
system: false,
102+
git: false,
103+
}
104+
}
105+
}
106+
107+
impl Default for Attributes {
108+
fn default() -> Self {
109+
Self::all()
110+
}
111+
}
112+
113+
/// Permissions related to the usage of environment variables
114+
#[derive(Debug, Clone, Copy)]
115+
pub struct Environment {
116+
/// Control whether resources pointed to by `XDG_CONFIG_HOME` can be used when looking up common configuration values.
117+
///
118+
/// Note that [`gix_sec::Permission::Forbid`] will cause the operation to abort if a resource is set via the XDG config environment.
119+
pub xdg_config_home: gix_sec::Permission,
120+
/// Control the way resources pointed to by the home directory (similar to `xdg_config_home`) may be used.
121+
pub home: gix_sec::Permission,
122+
/// Control if environment variables to configure the HTTP transport, like `http_proxy` may be used.
123+
///
124+
/// Note that http-transport related environment variables prefixed with `GIT_` may also be included here
125+
/// if they match this category like `GIT_HTTP_USER_AGENT`.
126+
pub http_transport: gix_sec::Permission,
127+
/// Control if the `EMAIL` environment variables may be read.
128+
///
129+
/// Note that identity related environment variables prefixed with `GIT_` may also be included here
130+
/// if they match this category.
131+
pub identity: gix_sec::Permission,
132+
/// Control if environment variables related to the object database are handled. This includes features and performance
133+
/// options alike.
134+
pub objects: gix_sec::Permission,
135+
/// Control if resources pointed to by `GIT_*` prefixed environment variables can be used, **but only** if they
136+
/// are not contained in any other category. This is a catch-all section.
137+
pub git_prefix: gix_sec::Permission,
138+
/// Control if resources pointed to by `SSH_*` prefixed environment variables can be used (like `SSH_ASKPASS`)
139+
pub ssh_prefix: gix_sec::Permission,
140+
}
141+
142+
impl Environment {
143+
/// Allow access to the entire environment.
144+
pub fn all() -> Self {
145+
let allow = gix_sec::Permission::Allow;
146+
Environment {
147+
xdg_config_home: allow,
148+
home: allow,
149+
git_prefix: allow,
150+
ssh_prefix: allow,
151+
http_transport: allow,
152+
identity: allow,
153+
objects: allow,
154+
}
155+
}
156+
157+
/// Don't allow loading any environment variables.
158+
pub fn isolated() -> Self {
159+
let deny = gix_sec::Permission::Deny;
160+
Environment {
161+
xdg_config_home: deny,
162+
home: deny,
163+
ssh_prefix: deny,
164+
git_prefix: deny,
165+
http_transport: deny,
166+
identity: deny,
167+
objects: deny,
168+
}
169+
}
170+
}
171+
172+
impl Permissions {
173+
/// Secure permissions are similar to `all()`
174+
pub fn secure() -> Self {
175+
Permissions {
176+
env: Environment::all(),
177+
config: Config::all(),
178+
attributes: Attributes::all(),
179+
}
180+
}
181+
182+
/// Everything is allowed with this set of permissions, thus we read all configuration and do what git typically
183+
/// does with owned repositories.
184+
pub fn all() -> Self {
185+
Permissions {
186+
env: Environment::all(),
187+
config: Config::all(),
188+
attributes: Attributes::all(),
189+
}
190+
}
191+
192+
/// Don't read any but the local git configuration and deny reading any environment variables.
193+
pub fn isolated() -> Self {
194+
Permissions {
195+
config: Config::isolated(),
196+
attributes: Attributes::isolated(),
197+
env: Environment::isolated(),
198+
}
199+
}
200+
}
201+
202+
impl gix_sec::trust::DefaultForLevel for Permissions {
203+
fn default_for_level(level: Trust) -> Self {
204+
match level {
205+
Trust::Full => Permissions::all(),
206+
Trust::Reduced => Permissions::secure(),
207+
}
208+
}
209+
}
210+
211+
impl Default for Permissions {
212+
fn default() -> Self {
213+
Permissions::secure()
214+
}
215+
}

gix/src/open/repository.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::{
1010
cache::{interpolate_context, util::ApplyLeniency},
1111
tree::{gitoxide, Core, Key, Safe},
1212
},
13-
permission, Permissions, ThreadSafeRepository,
13+
open::Permissions,
14+
ThreadSafeRepository,
1415
};
1516

1617
#[derive(Default, Clone)]
@@ -26,7 +27,7 @@ pub(crate) struct EnvironmentOverrides {
2627
}
2728

2829
impl EnvironmentOverrides {
29-
fn from_env() -> Result<Self, permission::env_var::resource::Error> {
30+
fn from_env() -> Result<Self, gix_sec::permission::Error<std::path::PathBuf>> {
3031
let mut worktree_dir = None;
3132
if let Some(path) = std::env::var_os(Core::WORKTREE.the_environment_override()) {
3233
worktree_dir = PathBuf::from(path).into();

gix/src/permission.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

gix/src/permissions.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)