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
// Copyright 2023 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.

#![expect(missing_docs)]

use std::ffi::OsString;
use std::fmt::Debug;
use std::io;
use std::io::Write as _;
use std::process;
use std::process::Command;
use std::process::ExitStatus;
use std::process::Stdio;

use thiserror::Error;

use crate::config::ConfigGetError;
use crate::settings::UserSettings;
use crate::signing::SigStatus;
use crate::signing::SignError;
use crate::signing::SigningBackend;
use crate::signing::Verification;

/// Search for one of these in the output from `--status-fd=1`.
///
/// - `[GNUPG:] GOODSIG <long keyid> <primary uid..>`
/// - `[GNUPG:] EXPKEYSIG <long keyid> <primary uid..>`
/// - `[GNUPG:] NO_PUBKEY <long keyid>`
/// - `[GNUPG:] BADSIG <long keyid> <primary uid..>`
///
/// Assume signature is invalid if none of the above was found, and if there are
/// at least one line other than general program failures `[GNUPG:] FAILURE`.
///
/// https://github.com/gpg/gnupg/blob/gnupg-2.5.18/doc/DETAILS#format-of-the-status-fd-output
fn parse_gpg_verify_output(
    output: &[u8],
    allow_expired_keys: bool,
) -> Option<Result<Verification, SignError>> {
    let status_lines = || {
        output.split(|&b| b == b'\n').filter_map(|line| {
            let line = line.strip_prefix(b"[GNUPG:] ")?;
            let mut parts = line.splitn(3, |&b| b == b' ');
            Some((parts.next()?, parts))
        })
    };
    let maybe_verification = status_lines().find_map(|(name, mut args)| {
        let status = match name {
            b"GOODSIG" => SigStatus::Good,
            b"EXPKEYSIG" => {
                if allow_expired_keys {
                    SigStatus::Good
                } else {
                    SigStatus::Bad
                }
            }
            b"NO_PUBKEY" => SigStatus::Unknown,
            b"BADSIG" => SigStatus::Bad,
            b"ERROR" => match args.next()? {
                b"verify.findkey" => return Some(Verification::unknown()),
                _ => return None,
            },
            _ => return None,
        };
        let mut args = args.fuse();
        let key = args
            .next()
            .and_then(|bs| str::from_utf8(bs).ok())
            .map(|value| value.trim().to_owned());
        let display = args
            .next()
            .and_then(|bs| str::from_utf8(bs).ok())
            .map(|value| value.trim().to_owned());
        Some(Verification::new(status, key, display))
    });
    if let Some(verification) = maybe_verification {
        Some(Ok(verification))
    } else if status_lines().any(|(name, _)| name != b"FAILURE") {
        Some(Err(SignError::InvalidSignatureFormat))
    } else {
        None
    }
}

fn make_command_error(output: &process::Output) -> GpgError {
    GpgError::Command {
        exit_status: output.status,
        stderr: String::from_utf8_lossy(&output.stderr).trim_end().into(),
    }
}

fn run_sign_command(command: &mut Command, input: &[u8]) -> Result<Vec<u8>, GpgError> {
    tracing::info!(?command, "running GPG signing command");
    let process = command.stderr(Stdio::piped()).spawn()?;
    let write_result = process.stdin.as_ref().unwrap().write_all(input);
    let output = process.wait_with_output()?;
    tracing::info!(?command, ?output.status, "GPG signing command exited");
    if output.status.success() {
        write_result?;
        Ok(output.stdout)
    } else {
        Err(make_command_error(&output))
    }
}

fn run_verify_command(command: &mut Command, input: &[u8]) -> Result<process::Output, GpgError> {
    tracing::info!(?command, "running GPG signing command");
    let process = command.stderr(Stdio::piped()).spawn()?;
    let write_result = process.stdin.as_ref().unwrap().write_all(input);
    let output = process.wait_with_output()?;
    tracing::info!(?command, ?output.status, "GPG signing command exited");
    match write_result {
        Ok(()) => Ok(output),
        // If the signature format is invalid, gpg will terminate early. Writing
        // more input data will fail in that case.
        Err(err) if err.kind() == io::ErrorKind::BrokenPipe => Ok(output),
        Err(err) => Err(err.into()),
    }
}

fn write_temp_file(prefix: &str, content: &[u8]) -> io::Result<tempfile::TempPath> {
    let mut file = tempfile::Builder::new().prefix(prefix).tempfile()?;
    file.write_all(content)?;
    file.flush()?;
    Ok(file.into_temp_path())
}

#[derive(Debug)]
pub struct GpgBackend {
    program: OsString,
    allow_expired_keys: bool,
    extra_args: Vec<OsString>,
    default_key: String,
}

#[derive(Debug, Error)]
pub enum GpgError {
    #[error("GPG failed with {exit_status}:\n{stderr}")]
    Command {
        exit_status: ExitStatus,
        stderr: String,
    },
    #[error("Failed to run GPG")]
    Io(#[from] std::io::Error),
}

impl From<GpgError> for SignError {
    fn from(e: GpgError) -> Self {
        Self::Backend(Box::new(e))
    }
}

impl GpgBackend {
    pub fn new(program: OsString, allow_expired_keys: bool, default_key: String) -> Self {
        Self {
            program,
            allow_expired_keys,
            extra_args: vec![],
            default_key,
        }
    }

    /// Primarily intended for testing
    pub fn with_extra_args(mut self, args: &[OsString]) -> Self {
        self.extra_args.extend_from_slice(args);
        self
    }

    pub fn from_settings(settings: &UserSettings) -> Result<Self, ConfigGetError> {
        let program = settings.get_string("signing.backends.gpg.program")?;
        let allow_expired_keys = settings.get_bool("signing.backends.gpg.allow-expired-keys")?;
        let default_key = settings.user_email().to_owned();
        Ok(Self::new(program.into(), allow_expired_keys, default_key))
    }

    fn create_command(&self) -> Command {
        let mut command = Command::new(&self.program);
        // Hide console window on Windows (https://stackoverflow.com/a/60958956)
        #[cfg(windows)]
        {
            use std::os::windows::process::CommandExt as _;
            const CREATE_NO_WINDOW: u32 = 0x08000000;
            command.creation_flags(CREATE_NO_WINDOW);
        }

        command
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .args(&self.extra_args);
        command
    }
}

impl SigningBackend for GpgBackend {
    fn name(&self) -> &'static str {
        "gpg"
    }

    fn can_read(&self, signature: &[u8]) -> bool {
        signature.starts_with(b"-----BEGIN PGP SIGNATURE-----")
    }

    fn sign(&self, data: &[u8], key: Option<&str>) -> Result<Vec<u8>, SignError> {
        let key = key.unwrap_or(&self.default_key);
        Ok(run_sign_command(
            self.create_command().args(["-abu", key]),
            data,
        )?)
    }

    fn verify(&self, data: &[u8], signature: &[u8]) -> Result<Verification, SignError> {
        let sig_path = write_temp_file(".jj-gpg-sig-tmp-", signature).map_err(GpgError::Io)?;

        let output = run_verify_command(
            self.create_command()
                .args(["--keyid-format=long", "--status-fd=1", "--verify"])
                .arg(&sig_path)
                .arg("-"),
            data,
        )?;

        parse_gpg_verify_output(&output.stdout, self.allow_expired_keys)
            .unwrap_or_else(|| Err(make_command_error(&output).into()))
    }
}

#[derive(Debug)]
pub struct GpgsmBackend {
    program: OsString,
    allow_expired_keys: bool,
    extra_args: Vec<OsString>,
    default_key: String,
}

impl GpgsmBackend {
    pub fn new(program: OsString, allow_expired_keys: bool, default_key: String) -> Self {
        Self {
            program,
            allow_expired_keys,
            extra_args: vec![],
            default_key,
        }
    }

    /// Primarily intended for testing
    pub fn with_extra_args(mut self, args: &[OsString]) -> Self {
        self.extra_args.extend_from_slice(args);
        self
    }

    pub fn from_settings(settings: &UserSettings) -> Result<Self, ConfigGetError> {
        let program = settings.get_string("signing.backends.gpgsm.program")?;
        let allow_expired_keys = settings.get_bool("signing.backends.gpgsm.allow-expired-keys")?;
        let default_key = settings.user_email().to_owned();
        Ok(Self::new(program.into(), allow_expired_keys, default_key))
    }

    fn create_command(&self) -> Command {
        let mut command = Command::new(&self.program);
        // Hide console window on Windows (https://stackoverflow.com/a/60958956)
        #[cfg(windows)]
        {
            use std::os::windows::process::CommandExt as _;
            const CREATE_NO_WINDOW: u32 = 0x08000000;
            command.creation_flags(CREATE_NO_WINDOW);
        }

        command
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .args(&self.extra_args);
        command
    }
}

impl SigningBackend for GpgsmBackend {
    fn name(&self) -> &'static str {
        "gpgsm"
    }

    fn can_read(&self, signature: &[u8]) -> bool {
        signature.starts_with(b"-----BEGIN SIGNED MESSAGE-----")
    }

    fn sign(&self, data: &[u8], key: Option<&str>) -> Result<Vec<u8>, SignError> {
        let key = key.unwrap_or(&self.default_key);
        Ok(run_sign_command(
            self.create_command().args(["-abu", key]),
            data,
        )?)
    }

    fn verify(&self, data: &[u8], signature: &[u8]) -> Result<Verification, SignError> {
        let data_path = write_temp_file(".jj-gpgsm-data-tmp-", data).map_err(GpgError::Io)?;
        let sig_path = write_temp_file(".jj-gpgsm-sig-tmp-", signature).map_err(GpgError::Io)?;

        // gpgsm 2.5.x doesn't parse "-" as stdin
        let output = run_verify_command(
            self.create_command()
                .args(["--status-fd=1", "--verify"])
                .arg(&sig_path)
                .arg(&data_path),
            b"",
        )?;

        parse_gpg_verify_output(&output.stdout, self.allow_expired_keys)
            .unwrap_or_else(|| Err(make_command_error(&output).into()))
    }
}

#[cfg(test)]
mod tests {
    use assert_matches::assert_matches;

    use super::*;

    #[test]
    fn gpg_verify_invalid_signature_format() {
        assert_matches!(
            parse_gpg_verify_output(
                b"[GNUPG:] NODATA 4\n[GNUPG:] FAILURE gpg-exit 33554433\n",
                true
            ),
            Some(Err(SignError::InvalidSignatureFormat))
        );
    }

    #[test]
    fn gpg_verify_bad_signature() {
        assert_eq!(
            parse_gpg_verify_output(b"[GNUPG:] BADSIG 123 456", true)
                .unwrap()
                .unwrap(),
            Verification::new(SigStatus::Bad, Some("123".into()), Some("456".into()))
        );
    }

    #[test]
    fn gpg_verify_unknown_signature() {
        assert_eq!(
            parse_gpg_verify_output(b"[GNUPG:] NO_PUBKEY 123", true)
                .unwrap()
                .unwrap(),
            Verification::new(SigStatus::Unknown, Some("123".into()), None)
        );
    }

    #[test]
    fn gpg_verify_good_signature() {
        assert_eq!(
            parse_gpg_verify_output(b"[GNUPG:] GOODSIG 123 456", true)
                .unwrap()
                .unwrap(),
            Verification::new(SigStatus::Good, Some("123".into()), Some("456".into()))
        );
    }

    #[test]
    fn gpg_verify_expired_signature() {
        assert_eq!(
            parse_gpg_verify_output(b"[GNUPG:] EXPKEYSIG 123 456", true)
                .unwrap()
                .unwrap(),
            Verification::new(SigStatus::Good, Some("123".into()), Some("456".into()))
        );

        assert_eq!(
            parse_gpg_verify_output(b"[GNUPG:] EXPKEYSIG 123 456", false)
                .unwrap()
                .unwrap(),
            Verification::new(SigStatus::Bad, Some("123".into()), Some("456".into()))
        );
    }

    #[test]
    fn gpg_verify_unknown_error() {
        assert_matches!(parse_gpg_verify_output(b"", true), None);
        assert_matches!(
            parse_gpg_verify_output(b"[GNUPG:] FAILURE gpg-exit 33554433\n", true),
            None
        );
        assert_matches!(
            parse_gpg_verify_output(b"[GNUPG:] FAILURE gpgsm-exit 50331649\n", true),
            None
        );
    }

    #[test]
    fn gpgsm_verify_unknown_signature() {
        assert_eq!(
            parse_gpg_verify_output(b"[GNUPG:] ERROR verify.findkey 50331657", true)
                .unwrap()
                .unwrap(),
            Verification::unknown(),
        );
    }

    #[test]
    fn gpgsm_verify_invalid_signature_format() {
        assert_matches!(
            parse_gpg_verify_output(b"[GNUPG:] ERROR verify.leave 150995087", true),
            Some(Err(SignError::InvalidSignatureFormat))
        );
    }
}