如何透過 trivy 掃描專案內元件授權
為確保專案符合所使用元件之授權範圍,使用者應具備獨立驗證授權的流程。建議使用者參考以下流程與範本,管理並確認專案元件所使用的授權。
注意事項,以下三種情形會導致 trivy 無法完整掃描專案元件:
- 元件安裝環境混亂:部分開發流程不佳的專案於安裝元件時,有些安裝在專案資料夾,有些安裝在整台電腦的共用空間,導致不清楚專案實際使用哪些元件,也就無法確認元件的授權。如有些元件用
npm install,有些則用npm install --global,則用npm install --global安裝的元件不會顯示專案依賴列表內。 - 網路引用但未紀錄:在開發一些網路專案時,有時會使用已經分佈在內容交付網路(CDN)上的元件,該種元件因不需安裝於專案資料夾也可能導致無法正確透過 trivy 確認其授權。
- 直接複製元件檔案:部分法律意識淡薄的專案在開發專案時會直接將其他人的部分元件檔案(如DLL檔與函式庫檔案等)複製至專案內使用,而不是透過正當管道安裝,這會無法確認元件是否允許該種使用方式,極容易造成侵權問題。
為什麼以上三種情形容易造成風險?
- 可能在不知情的情況下使用付費軟體
- 可能違法元件使用條款,面臨法律風險
- 上級單位無法準確評估專案成本和合規風險
- 元件出現安全漏洞時,無法即時更新或修補
流程簡介
- 建立專案正式環境
- 掃描元件在正式環境所使用的授權
- 結果分析
名詞介紹
- 正式環境(production environment):專案正式運作的軟體環境,包含元件, 執行檔, 動態連結庫等。為確保運作時不受到作業系統的影響,會盡可能使用環境管理工具製造虛擬環境(virtual environment)與作業系統隔離。
- 依賴文件(lock file):在不同程式碼生態系統中,依賴文件有不同的名稱,如 npm 的 package-lock.json, Cargo 的 Cargo.lock。主要功能為使不同開發環境保持依賴版本一致,防止意外升級依賴套件,提供可重現的軟體環境。總體而言,依賴文件是一種用於版本固定和依賴管理的特殊配置文件。
- 授權(license):指軟體套件的使用授權模式,規定軟體的使用、分發和修改權限。常見授權類型:
- 開源授權:可自由使用、修改、分發,需遵守特定條件
- 商業授權:需付費使用,有明確使用限制
- 免費授權:無需付費,可能有使用範圍限制
- 授權感染:因某些元件的授權要求使用該元件開發的軟體也保持開源,若不開源則違反元件授權。授權感染也具備連鎖性,若是元件的元件使用該種授權,則專案也必須必須保持開源。
建立專案正式環境
以 PETsARD 專案為例:
透過 uv 設定 python 環境
uv python install 3.11
uv venv
source .venv/bin/activate
透過 uv 與依賴文件(此處為 requirments.txt)安裝 python 元件
uv pip install -r requirments.txt
掃描元件在正式環境所使用的授權
以 trivy 掃描元件所使用的授權
trivy repo . --scanners license --license-full
掃描後的結果
2025-05-22T15:20:28+08:00 INFO [license] Full license scanning is enabled
2025-05-22T15:21:38+08:00 INFO [python] Licenses acquired from one or more METADATA files may be subject to additional terms. Use `--debug` flag to see all affected packages.
2025-05-22T15:21:38+08:00 INFO Suppressing dependencies for development and testing. To display them, try the '--include-dev-deps' flag.
Report Summary
┌───────────────────────┬──────┬──────────┐
│ Target │ Type │ Licenses │
├───────────────────────┼──────┼──────────┤
│ requirements.txt │ - │ 44 │
├───────────────────────┼──────┼──────────┤
│ Loose File License(s) │ - │ 149 │
└───────────────────────┴──────┴──────────┘
Legend:
- '-': Not scanned
- '0': Clean (no security findings detected)
requirements.txt (license)
Total: 44 (UNKNOWN: 5, LOW: 37, MEDIUM: 2, HIGH: 0, CRITICAL: 0)
┌───────────────┬──────────────────────────────────────┬────────────────┬──────────┐
│ Package │ License │ Classification │ Severity │
├───────────────┼──────────────────────────────────────┼────────────────┼──────────┤
│ anonymeter │ BSD License │ Notice │ LOW │
├───────────────┼──────────────────────────────────────┤ │ │
│ boto3 │ Apache Software License │ │ │
├───────────────┤ │ │ │
│ botocore │ │ │ │
├───────────────┼──────────────────────────────────────┼────────────────┼──────────┤
│ certifi │ Mozilla Public License 2.0 (MPL 2.0) │ Reciprocal │ MEDIUM │
├───────────────┼──────────────────────────────────────┼────────────────┼──────────┤
│ cloudpickle │ BSD License │ Notice │ LOW │
├───────────────┼──────────────────────────────────────┼────────────────┼──────────┤
│ copulas │ Free for non-commercial use │ Non Standard │ UNKNOWN │
├───────────────┤ │ │ │
│ ctgan │ │ │ │
├───────────────┤ │ │ │
│ deepecho │ │ │ │
├── ─────────────┼──────────────────────────────────────┼────────────────┼──────────┤
│ faker │ MIT License │ Notice │ LOW │
├───────────────┼──────────────────────────────────────┼────────────────┤ │
│ filelock │ Unlicense │ Unencumbered │ │
├───────────────┼──────────────────────────────────────┼────────────────┤ │
│ fsspec │ BSD License │ Notice │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ graphviz │ MIT License │ │ │
├──────── ───────┼──────────────────────────────────────┤ │ │
│ idna │ BSD License │ │ │
├───────────────┤ │ │ │
│ jinja2 │ │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ jmespath │ MIT License │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ joblib │ BSD License │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ llvmlite │ BSD │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ markupsafe │ BSD License │ │ │
├───────────────┤ │ │ │
│ mpmath │ │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ narwhals │ MIT License │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ networkx │ BSD License │ │ │
├───────────────┤ │ │ │
│ numba │ │ │ │
├───────────────┤ │ │ │
│ numpy │ │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ packaging │ Apache Software License │ │ │
│ ├──────────────────────────────────────┤ │ │
│ │ BSD License │ │ │
├───────────────┤ │ │ │
│ pandas │ │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ platformdirs │ MIT │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ plotly │ MIT License │ │ │
├───────────────┤ │ │ │
│ pytz │ │ │ │
├───────────────┤ │ │ │
│ pyyaml │ │ │ │
├───────────────┼──────────────────────────────────────┼────────────────┼──────────┤
│ rdt │ Free for non-commercial use │ Non Standard │ UNKNOWN │
├───────────────┼──────────────────────────────────────┼────────────────┼──────────┤
│ requests │ Apache Software License │ Notice │ LOW │
├───────────────┤ │ │ │
│ s3transfer │ │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ scipy │ BSD License │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ sdmetrics │ MIT License │ │ │
├───────────────┼──────────────────────────────────────┼────────────────┼──────────┤
│ sdv │ Free for non-commercial use │ Non Standard │ UNKNOWN │
├───────────────┼──────────────────────────────────────┼────────────────┼──────────┤
│ six │ MIT License │ Notice │ LOW │
├───────────────┼──────────────────────────────────────┤ │ │
│ sympy │ BSD License │ │ │
├───────────────┤ │ │ │
│ threadpoolctl │ │ │ │
├───────────────┤ │ │ │
│ torch │ │ │ │
├───────────────┼──────────────────────────────────────┤ │ │
│ tqdm │ MIT License │ │ │
│ ├──────────────────────────────────────┼────────────────┼──────────┤
│ │ Mozilla Public License 2.0 (MPL 2.0) │ Reciprocal │ MEDIUM │
├───────────────┼──────────────────────────────────────┼────────────────┼──────────┤
│ tzdata │ Apache Software License │ Notice │ LOW │
├───────────────┼ ──────────────────────────────────────┤ │ │
│ urllib3 │ MIT │ │ │
└───────────────┴──────────────────────────────────────┴────────────────┴──────────┘
Loose File License(s) (license)
Total: 149 (UNKNOWN: 21, LOW: 119, MEDIUM: 2, HIGH: 7, CRITICAL: 0)
┌────────────────┬──────────┬──────────────────────────────┬──────────────────────────────────────────────────────────────┐
│ Classification │ Severity │ License │ File Location │
├────────────────┼──────────┼──────────────────────────────┼──────────────────────────────────────────────────────────────┤
│ Restricted │ HIGH │ GPL-3.0 │ .venv/lib/python3.11/site-packages/numpy-1.26.4.dist-info/L- │
│ │ │ │ ICENSE.txt │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/scipy-1.15.2.dist-info/L- │
│ │ │ │ ICENSE.txt │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/torch-2.6.0.dist-info/LI- │
│ │ │ │ CENSE │
│ │ ├──────────────────────────────┼──────────────────────────────────────────────────────────────┤
│ │ │ GPL-3.0-with-GCC-exception │ .venv/lib/python3.11/site-packages/numpy-1.26.4.dist-info/L- │
│ │ │ │ ICENSE.txt │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/scipy-1.15.2.dist-info/L- │
│ │ │ │ ICENSE.txt │
│ │ ├──────────────────────────────┼──────────────────────────────────────────────────────────────┤
│ │ │ LGPL-2.1 │ .venv/lib/python3.11/site-packages/torch-2.6.0.dist-info/LI- │
│ │ │ │ CENSE │
│ │ ├──────────────────────────────┤ │
│ │ │ LGPL-3.0 │ │
│ │ │ │ │
├────────────────┼──────────┼──────────────────────────────┼───────────────────────────────────────────────── ─────────────┤
│ Reciprocal │ MEDIUM │ MPL-2.0 │ .venv/lib/python3.11/site-packages/certifi-2025.1.31.dist-i- │
│ │ │ │ nfo/LICENSE │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/tqdm-4.67.1.dist-info/LI- │
│ │ │ │ CENCE │
├────────────────┼──────────┼──────────────────────────────┼──────────────────────────────────────────────────────────────┤
│ Unencumbered │ LOW │ Unlicense │ .venv/lib/python3.11/site-packages/filelock-3.18.0.dist-inf- │
│ │ │ │ o/licenses/LICENSE │
├────────────────┤ ├──────────────────────────────┼──────────────────────────────────────────────────────────────┤
│ Notice │ │ Apache-2.0 │ .venv/lib/python3.11/site-packages/boto3-1.37.36.dist-info/- │
│ │ │ │ LICENSE │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/botocore-1.37.36.dist-in- │
│ │ │ │ fo/LICENSE.txt │
│ │ │ ├────────────── ────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/pandas-2.2.3.dist-info/L- │
│ │ │ │ ICENSE │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/python_dateutil-2.9.0.po- │
│ │ │ │ st0.dist-info/LICENSE │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/requests-2.32.3.dist-inf- │
│ │ │ │ o/LICENSE │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/s3transfer-0.11.5.dist-i- │
│ │ │ │ nfo/LICENSE.txt │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/torch-2.6.0.dist-info/LI- │
│ │ │ │ CENSE │
│ │ │ ├─────────────────────────────────────────────── ───────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/torch/utils/viz/_cycles.- │
│ │ │ │ py │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/tzdata-2025.2.dist-info/- │
│ │ │ │ licenses/LICENSE │
│ │ ├──────────────────────────────┼──────────────────────────────────────────────────────────────┤
│ │ │ BSD-2-Clause │ .venv/lib/python3.11/site-packages/llvmlite-0.44.0.dist-inf- │
│ │ │ │ o/LICENSE │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/numba-0.61.2.dist-info/L- │
│ │ │ │ ICENSE │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/numpy/_utils/_pep440.py │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/pandas-2.2.3.dist-info/L- │
│ │ │ │ ICENSE │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/scipy/_lib/_pep440.py │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/scipy/_lib/decorator.py │
│ │ │ ├──────────────────────────────────────────────────────────────┤
│ │ │ │ .venv/lib/python3.11/site-packages/scipy/integrate/_lebedev- │