LoginSignup
1
0

fatal: detected dubious ownership in repository at

Posted at

事象 : safe.directoryに設定してあるWSL2上のディレクトリをSourceTreeで見たらエラーになった

  • 環境
    • Windows11 Pro バージョン23H2
    • SourceTree Version 3.4.15
    • git version 2.42.0.windows.2
    • WSL2にあるディレクトリにリポジトリをクローンしている

WSL2のUbuntuにあるディレクトリをSourceTreeで開いたらエラーになりました。
しかし、このディレクトリはsafe.directoryに設定してあります。
なので、VS Codeのターミナル上ではエラーなくコマンド操作できています。

---------------------------
エラーが発生しました
---------------------------
'git log' がコード 128 で終了しました: fatal: detected dubious ownership in repository at '//wsl.localhost/Ubuntu/path/to/repository-name'
To add an exception for this directory, call:
    git config --global --add safe.directory '%(prefix)///wsl.localhost/Ubuntu/path/to/repository-name'
 (\\wsl.localhost\Ubuntu\usr\local\src\repository-name)

そもそも、このエラーが出力される経緯は・・・
複数ユーザーが使用できる環境上で悪意のある人に.gitが作成されて操作される可能性があるというGitの脆弱性がありました。

This vulnerability affects users working on multi-user machines where a malicious actor could create a .git directory in a shared location above a victim’s current working directory.
Git security vulnerability announced - The GitHub Blog

その対策として.gitを探すときに所有者を確認するようになりました。

Stricter repository ownership checks
...省略...
Beginning in Git 2.35.2, Git changed its default behavior to prevent you from executing git commands in a repository owned by a different user than the current one. This is designed to prevent git invocations from unintentionally executing commands which the repository owner configured.
Highlights from Git 2.36 - The GitHub Blog

なので、「所有者が違うよ」ってエラーになりますが、「安全なディレクトリだよ」と設定しておけばエラーにならなくなります。

safe.directory
These config entries specify Git-tracked directories that are considered safe even if they are owned by someone other than the current user.
Git - git-config Documentation

しかし!もうsafe.directoryには設定済みでした。なぜエラーに?

$ git config --global -l | grep 'safe.directory'
safe.directory=%(prefix)///wsl$/Ubuntu/path/to/repository-name

原因 : 「wsl.localhost」で設定していないから

エラーメッセージをよぉぉぉく見てみると「wsl.localhost」がエラーになっていました。
設定してあるのは「wsl$」でした。
そういえば「Error: UNC host 'wsl$' access is not allowed - Qiita」の時も「wsl$」だけじゃなく「wsl.localhost」も設定していました。

「wsl.localhost」と「wsl$」の違いというか使い分けがわからないです。

対応 : 「wsl.localhost」でsafe.directoryに設定する

無事、SourceTreeでエラーが解消しました。

# 「wsl.localhost」でsafe.directoryに設定する
$ git config --global --add safe.directory '%(prefix)///wsl.localhost/Ubuntu/path/to/repository-name'
# 設定できた
$ git config --global -l | grep 'safe.directory'
safe.directory=%(prefix)///wsl$/Ubuntu/path/to/repository-name
safe.directory=%(prefix)///wsl.localhost/Ubuntu/path/to/repository-name
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0