node-auth 0.4.0

Authenticate npm, yarn, pnpm, and Bun to Google Artifact Registry using Google Cloud ADC
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//! Path resolution and isolated, atomic configuration-file replacement.

use crate::AuthError;
use std::collections::BTreeMap;
#[cfg(unix)]
use std::fs::File;
use std::fs::{self, Permissions};
use std::io::Write;
use std::path::{Path, PathBuf};
use tempfile::{Builder, NamedTempFile};

#[cfg(unix)]
mod platform_lock {
  use std::fs::File;
  use std::io;
  use std::path::Path;

  pub(super) struct Lock {
    _file: File,
  }

  impl Lock {
    pub(super) fn acquire(directory: &Path) -> io::Result<Self> {
      let file = File::open(directory)?;
      file.lock()?;
      Ok(Self { _file: file })
    }
  }
}

#[cfg(windows)]
mod platform_lock {
  use std::io;
  use std::iter;
  use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle, RawHandle};
  use std::path::Path;

  use windows_sys::Win32::Foundation::{WAIT_ABANDONED, WAIT_OBJECT_0};
  use windows_sys::Win32::System::Threading::{
    CreateMutexW, INFINITE, ReleaseMutex, WaitForSingleObject,
  };

  pub(super) struct Lock(OwnedHandle);

  impl Lock {
    pub(super) fn acquire(directory: &Path) -> io::Result<Self> {
      // Use a stable, case-insensitive hash so every node-auth process in
      // this login session addresses the same kernel object.
      let mut hash = 0xcbf29ce484222325_u64;
      for unit in directory
        .as_os_str()
        .to_string_lossy()
        .to_lowercase()
        .encode_utf16()
      {
        hash ^= u64::from(unit);
        hash = hash.wrapping_mul(0x100000001b3);
      }
      let name = format!("Local\\node-auth-directory-{hash:016x}")
        .encode_utf16()
        .chain(iter::once(0))
        .collect::<Vec<_>>();
      let handle = unsafe { CreateMutexW(std::ptr::null(), 0, name.as_ptr()) };
      if handle.is_null() {
        return Err(io::Error::last_os_error());
      }
      let handle = unsafe { OwnedHandle::from_raw_handle(handle as RawHandle) };
      match unsafe { WaitForSingleObject(handle.as_raw_handle() as _, INFINITE) } {
        WAIT_OBJECT_0 | WAIT_ABANDONED => Ok(Self(handle)),
        _ => Err(io::Error::last_os_error()),
      }
    }
  }

  impl Drop for Lock {
    fn drop(&mut self) {
      unsafe {
        ReleaseMutex(self.0.as_raw_handle() as _);
      }
    }
  }
}

#[cfg(windows)]
mod windows_security {
  use std::ffi::c_void;
  use std::fs::File;
  use std::io;
  use std::iter;
  use std::mem::size_of;
  use std::os::windows::ffi::OsStrExt;
  use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle, RawHandle};
  use std::path::Path;

  use windows_sys::Win32::Foundation::{
    GENERIC_READ, GENERIC_WRITE, INVALID_HANDLE_VALUE, LocalFree,
  };
  #[cfg(test)]
  use windows_sys::Win32::Security::Authorization::ConvertSecurityDescriptorToStringSecurityDescriptorW;
  use windows_sys::Win32::Security::Authorization::{
    ConvertSidToStringSidW, ConvertStringSecurityDescriptorToSecurityDescriptorW, SDDL_REVISION_1,
  };
  use windows_sys::Win32::Security::{
    DACL_SECURITY_INFORMATION, GetFileSecurityW, GetSecurityDescriptorControl, GetTokenInformation,
    PROTECTED_DACL_SECURITY_INFORMATION, PSECURITY_DESCRIPTOR, SE_DACL_PROTECTED,
    SECURITY_ATTRIBUTES, SetFileSecurityW, TOKEN_QUERY, TOKEN_USER, TokenUser,
    UNPROTECTED_DACL_SECURITY_INFORMATION,
  };
  use windows_sys::Win32::Storage::FileSystem::{
    CREATE_NEW, CreateFileW, DELETE, FILE_ATTRIBUTE_TEMPORARY, FILE_SHARE_DELETE, FILE_SHARE_READ,
    FILE_SHARE_WRITE,
  };
  use windows_sys::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken};

  #[derive(Clone)]
  pub(super) struct SecurityDescriptor {
    storage: Vec<usize>,
  }

  impl SecurityDescriptor {
    fn as_ptr(&self) -> PSECURITY_DESCRIPTOR {
      self.storage.as_ptr().cast_mut().cast()
    }
  }

  struct LocalAllocation(*mut c_void);

  impl Drop for LocalAllocation {
    fn drop(&mut self) {
      if !self.0.is_null() {
        unsafe {
          LocalFree(self.0);
        }
      }
    }
  }

  fn wide(path: &Path) -> Vec<u16> {
    path
      .as_os_str()
      .encode_wide()
      .chain(iter::once(0))
      .collect()
  }

  fn current_user_sid() -> io::Result<String> {
    unsafe {
      let mut token = std::ptr::null_mut();
      if OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut token) == 0 {
        return Err(io::Error::last_os_error());
      }
      let token = OwnedHandle::from_raw_handle(token as RawHandle);

      let mut required = 0;
      GetTokenInformation(
        token.as_raw_handle() as _,
        TokenUser,
        std::ptr::null_mut(),
        0,
        &mut required,
      );
      if required == 0 {
        return Err(io::Error::last_os_error());
      }
      let words = (required as usize).div_ceil(size_of::<usize>());
      let mut token_info = vec![0usize; words];
      if GetTokenInformation(
        token.as_raw_handle() as _,
        TokenUser,
        token_info.as_mut_ptr().cast(),
        required,
        &mut required,
      ) == 0
      {
        return Err(io::Error::last_os_error());
      }
      let token_user = &*token_info.as_ptr().cast::<TOKEN_USER>();
      let mut sid_string = std::ptr::null_mut();
      if ConvertSidToStringSidW(token_user.User.Sid, &mut sid_string) == 0 {
        return Err(io::Error::last_os_error());
      }
      let allocation = LocalAllocation(sid_string.cast());
      let len = (0..).take_while(|&i| *sid_string.add(i) != 0).count();
      let result = String::from_utf16(std::slice::from_raw_parts(sid_string, len))
        .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error));
      drop(allocation);
      result
    }
  }

  fn private_descriptor() -> io::Result<LocalAllocation> {
    let sddl = format!("D:P(A;;FA;;;{})", current_user_sid()?);
    let encoded = sddl.encode_utf16().chain(iter::once(0)).collect::<Vec<_>>();
    let mut descriptor = std::ptr::null_mut();
    unsafe {
      if ConvertStringSecurityDescriptorToSecurityDescriptorW(
        encoded.as_ptr(),
        SDDL_REVISION_1,
        &mut descriptor,
        std::ptr::null_mut(),
      ) == 0
      {
        return Err(io::Error::last_os_error());
      }
    }
    Ok(LocalAllocation(descriptor))
  }

  /// Create a file whose DACL grants access only to the current user.
  pub(super) fn create_private_file(path: &Path) -> io::Result<File> {
    let descriptor = private_descriptor()?;
    let attributes = SECURITY_ATTRIBUTES {
      nLength: size_of::<SECURITY_ATTRIBUTES>() as u32,
      lpSecurityDescriptor: descriptor.0,
      bInheritHandle: 0,
    };
    let path = wide(path);
    let handle = unsafe {
      CreateFileW(
        path.as_ptr(),
        GENERIC_READ | GENERIC_WRITE | DELETE,
        FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
        &attributes,
        CREATE_NEW,
        FILE_ATTRIBUTE_TEMPORARY,
        std::ptr::null_mut(),
      )
    };
    if handle == INVALID_HANDLE_VALUE {
      Err(io::Error::last_os_error())
    } else {
      // The returned handle is uniquely owned and becomes owned by File.
      Ok(unsafe { File::from_raw_handle(handle as RawHandle) })
    }
  }

  /// Capture the target DACL, including whether inheritance is protected.
  pub(super) fn read_dacl(path: &Path) -> io::Result<SecurityDescriptor> {
    let path = wide(path);
    let mut required = 0;
    unsafe {
      GetFileSecurityW(
        path.as_ptr(),
        DACL_SECURITY_INFORMATION,
        std::ptr::null_mut(),
        0,
        &mut required,
      );
    }
    if required == 0 {
      return Err(io::Error::last_os_error());
    }
    let words = (required as usize).div_ceil(size_of::<usize>());
    let mut storage = vec![0usize; words];
    if unsafe {
      GetFileSecurityW(
        path.as_ptr(),
        DACL_SECURITY_INFORMATION,
        storage.as_mut_ptr().cast(),
        required,
        &mut required,
      )
    } == 0
    {
      return Err(io::Error::last_os_error());
    }
    Ok(SecurityDescriptor { storage })
  }

  /// Apply a captured DACL without changing its inheritance protection state.
  pub(super) fn apply_dacl(path: &Path, descriptor: &SecurityDescriptor) -> io::Result<()> {
    let mut control = 0;
    let mut revision = 0;
    if unsafe { GetSecurityDescriptorControl(descriptor.as_ptr(), &mut control, &mut revision) }
      == 0
    {
      return Err(io::Error::last_os_error());
    }
    let protection = if control & SE_DACL_PROTECTED != 0 {
      PROTECTED_DACL_SECURITY_INFORMATION
    } else {
      UNPROTECTED_DACL_SECURITY_INFORMATION
    };
    let path = wide(path);
    if unsafe {
      SetFileSecurityW(
        path.as_ptr(),
        DACL_SECURITY_INFORMATION | protection,
        descriptor.as_ptr(),
      )
    } == 0
    {
      Err(io::Error::last_os_error())
    } else {
      Ok(())
    }
  }

  #[cfg(test)]
  unsafe fn descriptor_to_sddl(descriptor: PSECURITY_DESCRIPTOR) -> io::Result<String> {
    let mut text = std::ptr::null_mut();
    if unsafe {
      ConvertSecurityDescriptorToStringSecurityDescriptorW(
        descriptor,
        SDDL_REVISION_1,
        DACL_SECURITY_INFORMATION,
        &mut text,
        std::ptr::null_mut(),
      )
    } == 0
    {
      return Err(io::Error::last_os_error());
    }
    let allocation = LocalAllocation(text.cast());
    let len = (0..).take_while(|&i| unsafe { *text.add(i) != 0 }).count();
    let result = String::from_utf16(unsafe { std::slice::from_raw_parts(text, len) })
      .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error));
    drop(allocation);
    result
  }

  #[cfg(test)]
  pub(super) fn dacl_sddl(path: &Path) -> io::Result<String> {
    let descriptor = read_dacl(path)?;
    unsafe { descriptor_to_sddl(descriptor.as_ptr()) }
  }

  #[cfg(test)]
  pub(super) fn private_dacl_sddl() -> io::Result<String> {
    let descriptor = private_descriptor()?;
    unsafe { descriptor_to_sddl(descriptor.0) }
  }
}

/// Resolve an existing path through symlinks, or resolve its nearest existing ancestor.
pub fn resolve(path: &Path) -> Result<PathBuf, AuthError> {
  let absolute = if path.is_absolute() {
    path.to_path_buf()
  } else {
    std::env::current_dir()?.join(path)
  };
  match fs::symlink_metadata(&absolute) {
    Ok(_) => Ok(absolute.canonicalize()?),
    Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
      let parent = absolute
        .parent()
        .ok_or_else(|| AuthError::Config(format!("Path has no parent: {}", absolute.display())))?;
      let name = absolute.file_name().ok_or_else(|| {
        AuthError::Config(format!("Path has no file name: {}", absolute.display()))
      })?;
      Ok(resolve(parent)?.join(name))
    }
    Err(e) => Err(e.into()),
  }
}

#[derive(Clone)]
struct Snapshot {
  contents: Option<String>,
  permissions: Option<Permissions>,
  #[cfg(windows)]
  dacl: Option<windows_security::SecurityDescriptor>,
}

/// Holds cooperative locks from the initial reads through all replacements.
///
/// Every output is staged before the first rename. Each rename is atomic, although
/// replacing several different files cannot be a single filesystem transaction.
pub struct FileTransaction {
  snapshots: BTreeMap<PathBuf, Snapshot>,
  staged: BTreeMap<PathBuf, NamedTempFile>,
  _locks: Vec<platform_lock::Lock>,
}

impl FileTransaction {
  /// Resolve and lock every path in sorted order, then read their contents.
  pub fn new(paths: &[PathBuf]) -> Result<Self, AuthError> {
    let mut resolved = paths
      .iter()
      .map(|p| resolve(p))
      .collect::<Result<Vec<_>, _>>()?;
    resolved.sort();
    resolved.dedup();

    let mut directories = resolved
      .iter()
      .map(|path| {
        path
          .parent()
          .map(Path::to_path_buf)
          .ok_or_else(|| AuthError::Config(format!("Path has no parent: {}", path.display())))
      })
      .collect::<Result<Vec<_>, _>>()?;
    directories.sort();
    directories.dedup();

    let mut locks = Vec::with_capacity(directories.len());
    for parent in directories {
      fs::create_dir_all(&parent)?;
      locks.push(platform_lock::Lock::acquire(&parent)?);
    }

    let mut snapshots = BTreeMap::new();
    for path in resolved {
      let snapshot = match fs::read_to_string(&path) {
        Ok(contents) => Snapshot {
          contents: Some(contents),
          permissions: Some(fs::metadata(&path)?.permissions()),
          #[cfg(windows)]
          dacl: Some(windows_security::read_dacl(&path)?),
        },
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Snapshot {
          contents: None,
          permissions: None,
          #[cfg(windows)]
          dacl: None,
        },
        Err(e) => return Err(e.into()),
      };
      snapshots.insert(path, snapshot);
    }
    Ok(Self {
      snapshots,
      staged: BTreeMap::new(),
      _locks: locks,
    })
  }

  /// Return the content read while the transaction held all requested locks.
  pub fn contents(&self, path: &Path) -> Result<&str, AuthError> {
    self
      .snapshots
      .get(path)
      .map(|s| s.contents.as_deref().unwrap_or(""))
      .ok_or_else(|| {
        AuthError::Config(format!(
          "Path was not included in the transaction: {}",
          path.display()
        ))
      })
  }

  /// Whether the path existed when the transaction read it.
  pub fn existed(&self, path: &Path) -> Result<bool, AuthError> {
    self
      .snapshots
      .get(path)
      .map(|s| s.contents.is_some())
      .ok_or_else(|| {
        AuthError::Config(format!(
          "Path was not included in the transaction: {}",
          path.display()
        ))
      })
  }

  /// Stage an output beside its destination without replacing that destination yet.
  pub fn stage(&mut self, path: &Path, content: &str) -> Result<(), AuthError> {
    let snapshot = self.snapshots.get(path).ok_or_else(|| {
      AuthError::Config(format!(
        "Path was not included in the transaction: {}",
        path.display()
      ))
    })?;
    if self.staged.contains_key(path) {
      return Err(AuthError::Config(format!(
        "File staged twice: {}",
        path.display()
      )));
    }
    let parent = path
      .parent()
      .ok_or_else(|| AuthError::Config(format!("Path has no parent: {}", path.display())))?;
    #[cfg(windows)]
    let mut temp = Builder::new()
      .prefix(".node-auth-")
      .make_in(parent, windows_security::create_private_file)?;
    #[cfg(not(windows))]
    let mut temp = Builder::new().prefix(".node-auth-").tempfile_in(parent)?;
    #[cfg(windows)]
    if let Some(dacl) = &snapshot.dacl {
      windows_security::apply_dacl(temp.path(), dacl)?;
    }
    temp.write_all(content.as_bytes())?;
    if let Some(permissions) = &snapshot.permissions {
      temp.as_file().set_permissions(permissions.clone())?;
    }
    temp.as_file().sync_all()?;
    self.staged.insert(path.to_path_buf(), temp);
    Ok(())
  }

  /// Atomically replace each staged file, rejecting changes made since the initial read.
  pub fn commit(mut self) -> Result<Vec<PathBuf>, AuthError> {
    for (path, snapshot) in &self.snapshots {
      let current = match fs::read_to_string(path) {
        Ok(contents) => Some(contents),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
        Err(e) => return Err(e.into()),
      };
      if current != snapshot.contents {
        return Err(AuthError::Config(format!(
          "Configuration changed during update: {}",
          path.display()
        )));
      }
    }

    let mut written = Vec::new();
    for (path, temp) in std::mem::take(&mut self.staged) {
      temp.persist(&path).map_err(|e| AuthError::Io(e.error))?;
      #[cfg(unix)]
      File::open(path.parent().unwrap())?.sync_all()?;
      written.push(path);
    }
    Ok(written)
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use tempfile::tempdir;

  #[test]
  fn resolves_aliases_and_replaces_atomically() {
    let dir = tempdir().unwrap();
    let path = dir.path().join(".npmrc");
    fs::write(&path, "old").unwrap();
    let alias = dir.path().join("./.npmrc");
    assert_eq!(resolve(&path).unwrap(), resolve(&alias).unwrap());
    let mut tx = FileTransaction::new(&[path.clone(), alias]).unwrap();
    let resolved = resolve(&path).unwrap();
    assert_eq!(tx.contents(&resolved).unwrap(), "old");
    tx.stage(&resolved, "new").unwrap();
    tx.commit().unwrap();
    assert_eq!(fs::read_to_string(path).unwrap(), "new");
    assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 1);
  }

  #[cfg(unix)]
  #[test]
  fn new_file_is_private_and_existing_mode_is_preserved() {
    use std::os::unix::fs::PermissionsExt;
    let dir = tempdir().unwrap();
    let new_path = dir.path().join("new.npmrc");
    let mut tx = FileTransaction::new(std::slice::from_ref(&new_path)).unwrap();
    tx.stage(&resolve(&new_path).unwrap(), "token").unwrap();
    tx.commit().unwrap();
    assert_eq!(
      fs::metadata(&new_path).unwrap().permissions().mode() & 0o777,
      0o600
    );

    fs::set_permissions(&new_path, Permissions::from_mode(0o640)).unwrap();
    let mut tx = FileTransaction::new(std::slice::from_ref(&new_path)).unwrap();
    tx.stage(&resolve(&new_path).unwrap(), "next").unwrap();
    tx.commit().unwrap();
    assert_eq!(
      fs::metadata(&new_path).unwrap().permissions().mode() & 0o777,
      0o640
    );
  }

  #[cfg(windows)]
  #[test]
  fn new_file_is_private_and_existing_dacl_is_preserved() {
    let dir = tempdir().unwrap();
    let new_path = dir.path().join("new.npmrc");
    let mut tx = FileTransaction::new(std::slice::from_ref(&new_path)).unwrap();
    tx.stage(&resolve(&new_path).unwrap(), "token").unwrap();
    tx.commit().unwrap();
    assert_eq!(
      windows_security::dacl_sddl(&new_path).unwrap(),
      windows_security::private_dacl_sddl().unwrap()
    );

    let existing_path = dir.path().join("existing.npmrc");
    fs::write(&existing_path, "old").unwrap();
    let original_dacl = windows_security::dacl_sddl(&existing_path).unwrap();
    let mut tx = FileTransaction::new(std::slice::from_ref(&existing_path)).unwrap();
    tx.stage(&resolve(&existing_path).unwrap(), "next").unwrap();
    tx.commit().unwrap();
    assert_eq!(
      windows_security::dacl_sddl(&existing_path).unwrap(),
      original_dacl
    );
  }

  #[test]
  fn transactions_serialize_reads_and_writes() {
    use std::sync::mpsc;
    use std::time::Duration;
    let dir = tempdir().unwrap();
    let path = dir.path().join("shared.npmrc");
    fs::write(&path, "old").unwrap();
    let mut first = FileTransaction::new(std::slice::from_ref(&path)).unwrap();
    let (started_tx, started_rx) = mpsc::channel();
    let (content_tx, content_rx) = mpsc::channel();
    let second_path = path.clone();
    let worker = std::thread::spawn(move || {
      started_tx.send(()).unwrap();
      let second = FileTransaction::new(std::slice::from_ref(&second_path)).unwrap();
      content_tx
        .send(
          second
            .contents(&resolve(&second_path).unwrap())
            .unwrap()
            .to_string(),
        )
        .unwrap();
    });
    started_rx.recv().unwrap();
    assert!(content_rx.recv_timeout(Duration::from_millis(50)).is_err());
    first.stage(&resolve(&path).unwrap(), "new").unwrap();
    first.commit().unwrap();
    assert_eq!(
      content_rx.recv_timeout(Duration::from_secs(2)).unwrap(),
      "new"
    );
    worker.join().unwrap();
  }
}