Rust vs. Go: About Vulnerability Management

Rust / Go のセキュリティについて考える。

依存関係の管理が重要になる。

特に、コードを流用する場合は、`Cargo.toml` の内容は注意が必要。

依存関係については、Node.js にも同じことが言える。

共通で使える scanner について調べる。

- [OSV‑Scanner](https://github.com/google/osv-scanner)
- [Trivy](https://github.com/aquasecurity/trivy)

他にもあると思うが、事例や汎用性を考えてこの 2 つについて調べる。`cargo audit` は置いておく。

## Environment
- dev container
Rust と Go が使える状態。

https://github.com/danny-yamamoto/rust-api-samples/blob/main/.devcontainer/devcontainer.json

## Installation
- OSV‑Scanner
[Docs](https://osv.dev/)
```bash
go install github.com/google/osv-scanner/cmd/osv-scanner@v1
```

- Trivy
[Docs](https://aquasecurity.github.io/trivy/v0.18.3/installation/)
```bash
sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | sudo apt-key add -
echo deb https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy
```

## Rust
- OSV‑Scanner
   - [Target](https://github.com/google/osv-scanner/blob/main/docs/usage.md#specify-lockfiles)
```bash
vscode ➜ /workspaces/rust-api-samples/main (main) $ osv-scanner --lockfile=./Cargo.lock 
Scanned /workspaces/rust-api-samples/main/Cargo.lock file and found 241 packages
╭───────────────────────────────────┬──────┬───────────┬─────────┬─────────┬────────────╮
│ OSV URL                           │ CVSS │ ECOSYSTEM │ PACKAGE │ VERSION │ SOURCE     │
├───────────────────────────────────┼──────┼───────────┼─────────┼─────────┼────────────┤
│ https://osv.dev/RUSTSEC-2021-0141 │      │ crates.io │ dotenv  │ 0.15.0  │ Cargo.lock │
╰───────────────────────────────────┴──────┴───────────┴─────────┴─────────┴────────────╯
vscode ➜ /workspaces/rust-api-samples/main (main) $ 
```

- Trivy
   - [Target](https://aquasecurity.github.io/trivy/v0.39/docs/vulnerability/languages/rust/)

丁寧にも、credentials を教えてくれる。
```bash
vscode ➜ /workspaces/rust-api-samples/main (main) $ trivy fs .
2023-11-22T05:44:00.913Z        INFO    Vulnerability scanning is enabled
2023-11-22T05:44:00.913Z        INFO    Secret scanning is enabled
2023-11-22T05:44:00.913Z        INFO    If your scanning is slow, please try '--scanners vuln' to disable secret scanning
2023-11-22T05:44:00.913Z        INFO    Please see also https://aquasecurity.github.io/trivy/v0.47/docs/scanner/secret/#recommendation for faster secret detection
2023-11-22T05:44:04.721Z        INFO    Number of language-specific files: 1
2023-11-22T05:44:04.721Z        INFO    Detecting cargo vulnerabilities...

key.json (secrets)

Total: 2 (UNKNOWN: 0, LOW: 0, MEDIUM: 0, HIGH: 1, CRITICAL: 1)

CRITICAL: Google (gcp-service-account)
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Google (GCP) Service-account
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 key.json:2
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   {
   2 [   *************************,
   3     "project_id": "hoge",
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────


HIGH: AsymmetricPrivateKey (private-key)
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Asymmetric Private Key
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 key.json:5
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   3     "project_id": "hoge",
   4     "private_key_id": "abcdefgh",
   5 [   "private_key": "-----BEGIN PRIVATE KEY-----**********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************-----END PRIVATE KEY-----\n",
   6     "client_email": "rust-app-storage@hoge.iam.gserviceaccount.com",
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────


vscode ➜ /workspaces/rust-api-samples/main (main) $
```

## Go
- OSV‑Scanner
```bash
vscode ➜ /workspaces/go-api-samples (main) $ osv-scanner --lockfile=./go.mod 
Scanned /workspaces/go-api-samples/go.mod file and found 21 packages
╭──────────────────────────────┬──────┬───────────┬─────────┬─────────┬────────╮
│ OSV URL                      │ CVSS │ ECOSYSTEM │ PACKAGE │ VERSION │ SOURCE │
├──────────────────────────────┼──────┼───────────┼─────────┼─────────┼────────┤
│ https://osv.dev/GO-2023-2185 │      │ Go        │ stdlib  │ 1.21.3  │ go.mod │
│                              │      │           │         │         │        │
│ https://osv.dev/GO-2023-2186 │      │ Go        │ stdlib  │ 1.21.3  │ go.mod │
│                              │      │           │         │         │        │
╰──────────────────────────────┴──────┴───────────┴─────────┴─────────┴────────╯
vscode ➜ /workspaces/go-api-samples (main) $
```

- Trivy
```bash
vscode ➜ /workspaces/go-api-samples (main) $ trivy fs .
2023-11-22T05:57:17.670Z        INFO    Need to update DB
2023-11-22T05:57:17.670Z        INFO    DB Repository: ghcr.io/aquasecurity/trivy-db
2023-11-22T05:57:17.670Z        INFO    Downloading DB...
40.99 MiB / 40.99 MiB [-----------------------------------------------------------------------------------------------------------------------------------------] 100.00% 17.27 MiB p/s 2.6s
2023-11-22T05:57:21.430Z        INFO    Vulnerability scanning is enabled
2023-11-22T05:57:21.431Z        INFO    Secret scanning is enabled
2023-11-22T05:57:21.431Z        INFO    If your scanning is slow, please try '--scanners vuln' to disable secret scanning
2023-11-22T05:57:21.431Z        INFO    Please see also https://aquasecurity.github.io/trivy/v0.47/docs/scanner/secret/#recommendation for faster secret detection
2023-11-22T05:57:22.013Z        INFO    Number of language-specific files: 1
2023-11-22T05:57:22.013Z        INFO    Detecting gomod vulnerabilities...

key.json (secrets)

Total: 2 (UNKNOWN: 0, LOW: 0, MEDIUM: 0, HIGH: 1, CRITICAL: 1)

CRITICAL: Google (gcp-service-account)
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Google (GCP) Service-account
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 key.json:2
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   {
   2 [     *************************,
   3       "project_id": "hoge",
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────


HIGH: AsymmetricPrivateKey (private-key)
════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Asymmetric Private Key
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 key.json:5
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   3       "project_id": "hoge",
   4       "private_key_id": "abcdefgh",
   5 [     "private_key": "-----BEGIN PRIVATE KEY-----**********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************-----END PRIVATE KEY-----\n",
   6       "client_email": "rust-app-storage@hoge.iam.gserviceaccount.com",
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────


vscode ➜ /workspaces/go-api-samples (main) $ 
```