jj-lib 0.40.0

Library for Jujutsu - an experimental version control system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
// Copyright 2025 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A mechanism to access config files for a repo securely.

use std::cell::RefCell;
use std::fs;
use std::io::ErrorKind::NotFound;
use std::io::Write as _;
use std::path::Path;
use std::path::PathBuf;

use prost::Message as _;
use rand::RngExt as _;
use rand_chacha::ChaCha20Rng;
use tempfile::NamedTempFile;
use thiserror::Error;

use crate::file_util::BadPathEncoding;
use crate::file_util::IoResultExt as _;
use crate::file_util::PathError;
use crate::file_util::path_from_bytes;
use crate::file_util::path_to_bytes;
use crate::hex_util::encode_hex;
use crate::protos::secure_config::ConfigMetadata;

const CONFIG_FILE: &str = "config.toml";
const METADATA_FILE: &str = "metadata.binpb";
const CONFIG_ID_BYTES: usize = 10;
#[cfg(not(unix))]
const CONTENT_PREFIX: &str = r###"# DO NOT EDIT.
# This file is for old versions of jj.
# It will be used for jj >= v0.37.
# Use `jj config path` or `jj config edit` to find and edit the new file

"###;
const CONFIG_NOT_FOUND: &str = r###"Per-repo config not found. Generating an empty one.
Per-repo config is stored in the same directory as your user config for security reasons.
If you work across multiple computers, you may want to keep your user config directory in sync."###;

/// A mechanism to access config files for a repo securely.
#[derive(Clone, Debug)]
pub struct SecureConfig {
    /// Technically this is either a repo or a workspace.
    repo_dir: PathBuf,
    /// The name of the config id file.
    config_id_name: &'static str,
    /// The name of the legacy config file.
    legacy_config_name: &'static str,
    /// A cache of the output \[maybe_\]load_config
    cache: RefCell<Option<(Option<PathBuf>, ConfigMetadata)>>,
}

/// An error when attempting to load config from disk.
#[derive(Error, Debug)]
pub enum SecureConfigError {
    /// Failed to read / write to the specified path
    #[error(transparent)]
    PathError(#[from] PathError),

    /// Failed to decode the user configuration file.
    #[error(transparent)]
    DecodeError(#[from] prost::DecodeError),

    /// The path failed to decode to bytes.
    #[error(transparent)]
    BadPathEncoding(#[from] BadPathEncoding),

    /// The config ID isn't CONFIG_ID_BYTES * 2 hex chars.
    #[error("Found an invalid config ID")]
    BadConfigIdError,
}

/// The path to the config file for a secure config.
/// Also contains metadata about the repo and info to be displayed to the
/// user.
#[derive(Clone, Debug, Default)]
pub struct LoadedSecureConfig {
    /// The path to the config file.
    /// Can be None if the config-ID is not generated.
    pub config_file: Option<PathBuf>,
    /// The metadata for the config.
    pub metadata: ConfigMetadata,
    /// Any warnings that we want to be reported to the user.
    pub warnings: Vec<String>,
}

fn atomic_write(path: &Path, content: &[u8]) -> Result<(), SecureConfigError> {
    let d = path.parent().unwrap();
    let mut temp_file = NamedTempFile::new_in(d).context(d)?;
    temp_file.write_all(content).context(temp_file.path())?;
    temp_file.persist(path).map_err(|e| PathError {
        path: path.to_path_buf(),
        source: e.error,
    })?;
    Ok(())
}

fn generate_config_id(rng: &mut ChaCha20Rng) -> String {
    encode_hex(&rng.random::<[u8; CONFIG_ID_BYTES]>())
}

fn update_metadata(config_dir: &Path, metadata: &ConfigMetadata) -> Result<(), SecureConfigError> {
    let metadata_path = config_dir.join(METADATA_FILE);
    atomic_write(&metadata_path, &metadata.encode_to_vec())?;
    Ok(())
}

impl SecureConfig {
    /// Creates a secure config.
    fn new(
        repo_dir: PathBuf,
        config_id_name: &'static str,
        legacy_config_name: &'static str,
    ) -> Self {
        Self {
            repo_dir,
            config_id_name,
            legacy_config_name,
            cache: RefCell::new(None),
        }
    }

    /// Creates a secure config for a repo. Takes the .jj/repo directory.
    pub fn new_repo(repo_dir: PathBuf) -> Self {
        Self::new(repo_dir, "config-id", "config.toml")
    }

    /// Creates a secure config for a workspace. Takes the .jj directory.
    pub fn new_workspace(workspace_dir: PathBuf) -> Self {
        Self::new(
            workspace_dir,
            "workspace-config-id",
            "workspace-config.toml",
        )
    }

    fn generate_config(
        &self,
        root_config_dir: &Path,
        config_id: &str,
        content: Option<&[u8]>,
        metadata: &ConfigMetadata,
    ) -> Result<PathBuf, SecureConfigError> {
        let config_dir = root_config_dir.join(config_id);
        let config_path = config_dir.join(CONFIG_FILE);
        fs::create_dir_all(&config_dir).context(&config_dir)?;
        update_metadata(&config_dir, metadata)?;
        if let Some(content) = content {
            fs::write(&config_path, content).context(&config_path)?;
        }

        // Write the config ID atomically. A half-formed config ID would be very bad.
        atomic_write(
            &self.repo_dir.join(self.config_id_name),
            config_id.as_bytes(),
        )?;
        Ok(config_path)
    }

    fn generate_initial_config(
        &self,
        root_config_dir: &Path,
        config_id: &str,
    ) -> Result<(PathBuf, ConfigMetadata), SecureConfigError> {
        let metadata = ConfigMetadata {
            path: path_to_bytes(&self.repo_dir).ok().map(|b| b.to_vec()),
        };
        let path = self.generate_config(root_config_dir, config_id, None, &metadata)?;
        Ok((path, metadata))
    }

    /// Validates that the metadata path matches the repo path.
    /// If there's a mismatch, takes appropriate action.
    /// Returns the updated config dir and metadata.
    fn handle_metadata_path(
        &self,
        rng: &mut ChaCha20Rng,
        root_config_dir: &Path,
        config_dir: PathBuf,
        mut metadata: ConfigMetadata,
    ) -> Result<LoadedSecureConfig, SecureConfigError> {
        let encoded = path_to_bytes(&self.repo_dir).ok();
        let got = metadata.path.as_deref().map(path_from_bytes).transpose()?;

        if got == encoded.is_some().then_some(self.repo_dir.as_path()) {
            return Ok(LoadedSecureConfig {
                config_file: Some(config_dir.join(CONFIG_FILE)),
                metadata,
                warnings: vec![],
            });
        }
        let got = match got {
            Some(d) if d.is_dir() => d.to_path_buf(),
            _ => {
                // The old repo does not exist. Assume the user moved it.
                metadata.path = encoded.map(|b| b.to_vec());
                update_metadata(&config_dir, &metadata)?;
                return Ok(LoadedSecureConfig {
                    config_file: Some(config_dir.join(CONFIG_FILE)),
                    metadata,
                    warnings: vec![],
                });
            }
        };
        // We attempt to create a temporary file in the new repo.
        // If it fails, we have readonly access to a repo, so we do nothing.
        // If we write to the new repo and it shows up in the old one,
        // we can skip this step, since it's not a copy.
        if let Ok(tmp) = NamedTempFile::new_in(&self.repo_dir)
            && !got.join(tmp.path().file_name().unwrap()).exists()
        {
            // We now assume the repo was copied. Since the repo was copied,
            // the config should be copied too, rather than sharing the
            // config with what it copied from.
            let old_config_path = config_dir.join(CONFIG_FILE);
            metadata.path = encoded.map(|b| b.to_vec());
            let old_config_content = fs::read(&old_config_path).context(&old_config_path)?;
            let config_path = self.generate_config(
                root_config_dir,
                &generate_config_id(rng),
                Some(&old_config_content),
                &metadata,
            )?;
            return Ok(LoadedSecureConfig {
                config_file: Some(config_path.clone()),
                metadata,
                warnings: vec![format!(
                    "Your repo appears to have been copied from {} to {}. The corresponding repo \
                     config file has also been copied.",
                    got.display(),
                    &self.repo_dir.display()
                )],
            });
        }
        Ok(LoadedSecureConfig {
            config_file: Some(config_dir.join(CONFIG_FILE)),
            metadata,
            warnings: vec![],
        })
    }

    #[cfg(unix)]
    fn update_legacy_config_file(
        &self,
        new_config: &Path,
        _content: &[u8],
    ) -> Result<(), SecureConfigError> {
        let legacy_config = self.repo_dir.join(self.legacy_config_name);
        // Make old versions and new versions of jj share the same config file.
        fs::remove_file(&legacy_config).context(&legacy_config)?;
        std::os::unix::fs::symlink(new_config, &legacy_config).context(&legacy_config)?;
        Ok(())
    }

    #[cfg(not(unix))]
    fn update_legacy_config_file(
        &self,
        _new_config: &Path,
        content: &[u8],
    ) -> Result<(), SecureConfigError> {
        let legacy_config = self.repo_dir.join(self.legacy_config_name);
        // I considered making this readonly, but that would prevent you from
        // updating the config with old versions of jj.
        // In the future, we consider something a little more robust, where as
        // the non-legacy config changes, we propagate that to the legacy config.
        // However, it seems a little overkill, considering it only affects windows
        // users who use multiple versions of jj at once, and only for a year.
        let mut new_content = CONTENT_PREFIX.as_bytes().to_vec();
        new_content.extend_from_slice(content);
        fs::write(&legacy_config, new_content).context(&legacy_config)?;
        Ok(())
    }

    /// Migrates the legacy config, if it exists.
    fn maybe_migrate_legacy_config(
        &self,
        rng: &mut ChaCha20Rng,
        root_config_dir: &Path,
    ) -> Result<LoadedSecureConfig, SecureConfigError> {
        // TODO: This function should be updated in jj 0.49 to no longer
        // automatically migrate repos, but instead print out a warning.
        let legacy_config = self.repo_dir.join(self.legacy_config_name);
        let config = match fs::read(&legacy_config).context(&legacy_config) {
            Ok(config_content) => config_content,
            // No legacy config files found.
            Err(e) if e.source.kind() == NotFound => return Ok(Default::default()),
            Err(e) => return Err(e.into()),
        };
        let metadata = ConfigMetadata {
            path: path_to_bytes(&self.repo_dir).ok().map(|b| b.to_vec()),
        };
        let config_file = self.generate_config(
            root_config_dir,
            &generate_config_id(rng),
            Some(&config),
            &metadata,
        )?;
        self.update_legacy_config_file(&config_file, &config)?;
        Ok(LoadedSecureConfig {
            warnings: vec![format!(
                "Your config file has been migrated from {} to {}. You can edit the new file with \
                 `jj config edit`",
                legacy_config.display(),
                config_file.display(),
            )],
            config_file: Some(config_file),
            metadata,
        })
    }

    /// Determines the path to the config, and any metadata associated with it.
    /// If no config exists, the path will be None.
    pub fn maybe_load_config(
        &self,
        rng: &mut ChaCha20Rng,
        root_config_dir: &Path,
    ) -> Result<LoadedSecureConfig, SecureConfigError> {
        if let Some(cache) = self.cache.borrow().as_ref() {
            return Ok(LoadedSecureConfig {
                config_file: cache.0.clone(),
                metadata: cache.1.clone(),
                warnings: vec![],
            });
        }
        let config_id_path = self.repo_dir.join(self.config_id_name);
        let loaded = match fs::read_to_string(&config_id_path).context(&config_id_path) {
            Ok(config_id) => {
                if config_id.len() != CONFIG_ID_BYTES * 2
                    || !config_id.chars().all(|c| c.is_ascii_hexdigit())
                {
                    return Err(SecureConfigError::BadConfigIdError);
                }
                let config_dir = root_config_dir.join(&config_id);
                let metadata_path = config_dir.join(METADATA_FILE);
                match fs::read(&metadata_path).context(&metadata_path) {
                    Ok(buf) => self.handle_metadata_path(
                        rng,
                        root_config_dir,
                        config_dir,
                        ConfigMetadata::decode(buf.as_slice())?,
                    )?,
                    Err(e) if e.source.kind() == NotFound => {
                        let (path, metadata) =
                            self.generate_initial_config(root_config_dir, &config_id)?;
                        LoadedSecureConfig {
                            config_file: Some(path),
                            metadata,
                            warnings: vec![CONFIG_NOT_FOUND.to_string()],
                        }
                    }
                    Err(e) => return Err(e.into()),
                }
            }
            Err(e) if e.source.kind() == NotFound => {
                self.maybe_migrate_legacy_config(rng, root_config_dir)?
            }
            Err(e) => return Err(SecureConfigError::PathError(e)),
        };
        *self.cache.borrow_mut() = Some((loaded.config_file.clone(), loaded.metadata.clone()));
        Ok(loaded)
    }

    /// Determines the path to the config, and any metadata associated with it.
    /// If no config exists, an empty config file will be generated.
    pub fn load_config(
        &self,
        rng: &mut ChaCha20Rng,
        root_config_dir: &Path,
    ) -> Result<LoadedSecureConfig, SecureConfigError> {
        let mut loaded = self.maybe_load_config(rng, root_config_dir)?;
        if loaded.config_file.is_none() {
            let (path, metadata) =
                self.generate_initial_config(root_config_dir, &generate_config_id(rng))?;
            *self.cache.borrow_mut() = Some((Some(path.clone()), metadata.clone()));
            loaded.config_file = Some(path);
            loaded.metadata = metadata;
        }
        Ok(loaded)
    }
}

#[cfg(test)]
mod tests {
    use std::ffi::OsStr;

    use rand::SeedableRng as _;
    use tempfile::TempDir;

    use super::*;
    use crate::tests::TestResult;

    struct TestEnv {
        _td: TempDir,
        rng: ChaCha20Rng,
        config: SecureConfig,
        repo_dir: PathBuf,
        config_dir: PathBuf,
    }

    impl TestEnv {
        fn new() -> Self {
            let td = crate::tests::new_temp_dir();
            let repo_dir = td.path().join("repo");
            fs::create_dir(&repo_dir).unwrap();
            let config_dir = td.path().join("config");
            fs::create_dir(&config_dir).unwrap();
            Self {
                _td: td,
                rng: ChaCha20Rng::seed_from_u64(0),
                config: SecureConfig::new(repo_dir.clone(), "config-id", "legacy-config.toml"),
                repo_dir,
                config_dir,
            }
        }

        fn secure_config_for_dir(&self, d: PathBuf) -> SecureConfig {
            SecureConfig::new(d, "config-id", "legacy-config.toml")
        }
    }

    #[test]
    fn test_no_initial_config() -> TestResult {
        let mut env = TestEnv::new();

        // We shouldn't generate the config.
        let loaded = env
            .config
            .maybe_load_config(&mut env.rng, &env.config_dir)?;
        assert_eq!(loaded.config_file, None);
        assert_eq!(loaded.metadata, Default::default());
        assert!(loaded.warnings.is_empty());
        // The cache entry should be filled.
        assert!(env.config.cache.borrow().is_some());

        // load_config should generate the config if it previously didn't exist.
        let loaded = env.config.load_config(&mut env.rng, &env.config_dir)?;
        let path = loaded.config_file.unwrap();
        let components: Vec<_> = path.components().rev().collect();
        assert_eq!(
            components[0],
            std::path::Component::Normal(OsStr::new("config.toml"))
        );
        assert_eq!(
            components[2],
            std::path::Component::Normal(OsStr::new("config"))
        );
        assert!(!loaded.metadata.path.as_deref().unwrap().is_empty());
        assert!(loaded.warnings.is_empty());

        // load_config should leave it untouched if it did exist.
        // Empty the cache to ensure the function is actually being tested
        assert!(env.config.cache.borrow().is_some());
        *env.config.cache.borrow_mut() = None;
        let loaded2 = env.config.load_config(&mut env.rng, &env.config_dir)?;
        assert_eq!(loaded2.config_file.unwrap(), path);
        assert_eq!(loaded2.metadata, loaded.metadata);
        assert!(loaded2.warnings.is_empty());
        Ok(())
    }

    #[test]
    fn test_migrate_legacy_config() -> TestResult {
        let mut env = TestEnv::new();

        let legacy_config = env.repo_dir.join("legacy-config.toml");
        fs::write(&legacy_config, "config")?;
        let loaded = env
            .config
            .maybe_load_config(&mut env.rng, &env.config_dir)?;
        assert!(loaded.config_file.is_some());
        assert!(!loaded.metadata.path.unwrap().is_empty());
        let config_contents = fs::read_to_string(loaded.config_file.as_deref().unwrap())?;
        assert_eq!(config_contents, "config");
        assert!(!loaded.warnings.is_empty());

        // On unix, it should be a symlink.
        if cfg!(unix) {
            fs::write(loaded.config_file.as_deref().unwrap(), "new")?;
            let legacy_contents = fs::read_to_string(&legacy_config)?;
            assert_eq!(legacy_contents, "new");
        }
        Ok(())
    }

    #[test]
    fn test_repo_moved() -> TestResult {
        let mut env = TestEnv::new();
        let loaded = env.config.load_config(&mut env.rng, &env.config_dir)?;
        let path = loaded.config_file.unwrap();

        let dest = env.repo_dir.parent().unwrap().join("moved");
        fs::rename(&env.repo_dir, &dest)?;
        let config = env.secure_config_for_dir(dest);
        let loaded2 = config.load_config(&mut env.rng, &env.config_dir)?;
        assert_eq!(loaded2.config_file.unwrap(), path);
        assert_ne!(loaded.metadata.path, loaded2.metadata.path);
        assert!(loaded2.warnings.is_empty());
        Ok(())
    }

    #[test]
    fn test_repo_copied() -> TestResult {
        let mut env = TestEnv::new();
        let loaded = env.config.load_config(&mut env.rng, &env.config_dir)?;
        let path = loaded.config_file.unwrap();
        fs::write(&path, "config")?;

        let dest = env.repo_dir.parent().unwrap().join("copied");
        fs::create_dir(&dest)?;
        fs::copy(env.repo_dir.join("config-id"), dest.join("config-id"))?;
        let config = env.secure_config_for_dir(dest);
        let loaded2 = config.load_config(&mut env.rng, &env.config_dir)?;
        let path2 = loaded2.config_file.unwrap();
        assert_ne!(path, path2);
        let path2_contents = fs::read_to_string(path2)?;
        assert_eq!(path2_contents, "config");
        assert_ne!(loaded.metadata.path, loaded2.metadata.path);
        // We should get a warning about the repo having been copied.
        assert!(!loaded2.warnings.is_empty());
        Ok(())
    }

    // This feature works on windows as well, it just isn't easy to replicate with a
    // test.
    #[cfg(unix)]
    #[test]
    fn test_repo_aliased() -> TestResult {
        let mut env = TestEnv::new();
        let loaded = env.config.load_config(&mut env.rng, &env.config_dir)?;
        let path = loaded.config_file.unwrap();

        let dest = env.repo_dir.parent().unwrap().join("copied");
        std::os::unix::fs::symlink(&env.repo_dir, &dest)?;
        let config = env.secure_config_for_dir(dest);
        let loaded2 = config.load_config(&mut env.rng, &env.config_dir)?;
        assert_eq!(loaded2.config_file.unwrap(), path);
        assert_eq!(loaded.metadata.path, loaded2.metadata.path);
        assert!(loaded2.warnings.is_empty());
        Ok(())
    }

    #[test]
    fn test_missing_config() -> TestResult {
        let mut env = TestEnv::new();
        let loaded = env.config.load_config(&mut env.rng, &env.config_dir)?;
        let path = loaded.config_file.unwrap();

        fs::remove_dir_all(path.parent().unwrap())?;
        *env.config.cache.borrow_mut() = None;

        let loaded2 = env.config.load_config(&mut env.rng, &env.config_dir)?;
        assert_eq!(loaded2.config_file.unwrap(), path);
        assert_eq!(loaded.metadata.path, loaded2.metadata.path);
        // It should have recreated the directory.
        assert!(path.parent().unwrap().is_dir());
        assert!(!loaded2.warnings.is_empty());
        Ok(())
    }
}