[参考]删除已经上传到GitHub上的敏感信息

删除已经上传到GitHub上的敏感信息

偶尔会有失手的时候把诸如用户信息、accesskey啥的传到GitHub的代码页中。一次commit可能会导致自己的信息泄露。为了杜绝这种问题,可以使用BFG工具删除敏感文件。

保护信息安全,从commit做起。

Github官方指引:从存储库中删除敏感数据 - GitHub Docs

准备工作

  1. 保存现有的所有代码,最好先进行一次commit确保本地的所有代码已经同步到云端。然后在本地做好备份,例如将repo文件夹打包成zip妥善保存防止代码丢失。

  2. 删除整个repo(这一步是为了清洁起见,也可以不删除)

  3. 在任意位置创建一个新文件夹,用于存放从GitHub上git clone下来的mirror文件

    mkdir {new_folder}
    
  4. 使用下述指令
    注意--mirror不能掉

    cd {new_folder}
    git clone --mirror https://github.com/{your_name}/{your_repo.git}
    

    等待git从服务器获取repo的数据。这里获取的数据不是代码页,也不是整个项目的项目文件,而是git的数据库。可以对生成的文件夹进行备份。

    这一步如果出现诸如

     Failed to connect to github.com port 443 after 21082 ms: Timed out
     error: RPC failed; curl 28 OpenSSL SSL_read: Connection was reset, errno 10054 fatal: expected flush after ref listing
    

    的错误,请检查你的代理设置。注意让git也走代理就可以了。

删除敏感信息

BFG Repo-Cleaner by rtyley下载BFG,下载将得到一个jar文件。将此jar文件存放在任何位置。推荐存放在创建的新文件夹{new_folder}下,便于执行命令。

  1. 删除敏感文件:

    存在一个authentication.txt的明文密码存储文件要删除,则使用指令(此指令的前提是{your_repo}.git文件夹和bfg.jar在同级目录

    java -jar bfg.jar --delete-files authentication.txt {your_repo.git}
    

    指令也可以使用正则表达式。下述指令将删除authentication.txt、authentication.key....等一系列文件

    java -jar bfg.jar --delete-files authentication.{txt,key,db,anything} {your_repo.git}
    

    执行将显示

    Found 15 objects to protect
    Found 2 commit-pointing refs : HEAD, refs/heads/master
    
    Protected commits
    -----------------
    
    These are your protected commits, and so their contents will NOT be altered:
    
     * commit abcdef01 (protected by 'HEAD')
    
    Cleaning
    --------
    
    Found 7 commits
    Cleaning commits:       100% (7/7)
    Cleaning commits completed in 138 ms.
    
    Updating 1 Ref
    --------------
    
            Ref                 Before     After
            ---------------------------------------
            refs/heads/master | abcdef01 | abcdef02
    
    Updating references:    100% (1/1)
    ...Ref update completed in 15 ms.
    
    Commit Tree-Dirt History
    ------------------------
    
            Earliest      Latest
            |                  |
             .  D  D m  m  m  m
    
            D = dirty commits (file tree fixed)
            m = modified commits (commit message or parents changed)
            . = clean commits (no changes to file tree)
    
                                    Before     After
            -------------------------------------------
            First modified commit | abcdef01 | abcdef02
            Last dirty commit     | abcdef03 | abcdef04
    
    Deleted files
    -------------
    
            Filename      Git id
            --------------------------------
            authentication.txt | 01010101 (44.3 KB)
    
    
    In total, 9 object ids were changed. Full details are logged here:
    
           ...\2022-09-13\21-04-40
    
    BFG run is complete! When ready, run: git reflog expire --expire=now --all && git gc --prune=now --aggressive
    

    即删除成功

  2. 替换敏感字段

    存在一些密码被散落在各种源码中,现在存在password.txt里。只需执行

    java -jar bfg.jar --replace-text passwords.txt  {your_repo.git}
    

    即可将此文件出现的密码全部置换为***REMOVED***

  3. 删除文件夹

    删除所有命名为.git的文件夹

    java -jar bfg.jar --delete-folders .git --delete-files .git  --no-blob-protection  {your_repo.git}
    
  4. 删除大文件

    删除超过50MB的大文件

    java -jar bfg.jar --strip-blobs-bigger-than 50M {your_repo.git}
    

更新服务器

只需要执行就能更新云端的commit,敏感信息一去不返。

git reflog expire --expire=now --all && git gc --prune=now --aggressive

我的习惯是这之后再push一下

git push

一切就绪!

posted @ 2022-09-13 21:35  二氢茉莉酮酸甲酯  阅读(133)  评论(0编辑  收藏  举报