顯示具有 Swagger 標籤的文章。 顯示所有文章
顯示具有 Swagger 標籤的文章。 顯示所有文章

2021年1月19日 星期二

SwaggerUI 的 Content Security Policy (SCP) 設定

最近工作上用到靜態代碼分析工具,並且在修正 Critical Issue 時碰到一些問題,來分享紀錄一下

Fortify Static Code Analyzer 是一個靜態應用程式安全分析工具

我這次遇到的是 Content Security Policy (CSP) is not configured.
那什麼是 CSP 呢?看 Wiki 就知道了

Content Security Policy (CSP) is a computer security standard introduced to prevent cross-site scripting (XSS), clickjacking and other code injection attacks resulting from execution of malicious content in the trusted web page context.

https://en.wikipedia.org/wiki/Content_Security_Policy




一開始以為,喔~只要把 Content-Security-Policy: default-src 'none' 這個 Header 加上去就好了吧!沒錯,加上去再做一次 Fortify Scan 就沒有 CSP Critical Issue 了,但是 SwaggerUI 卻壞了…


因為 Swagger 的 css, js, icon 資源載入,全被 Browser 擋掉了。
這是因為 default-src 'none' 的意思是任何資源都不允許,所以必須開一些白名單讓自己網站的資源可以正常載入,最後改成這樣就正常了。

default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; img-src 'self' data:;

其他相關 CSP 設定可參考這裡

2017年9月26日 星期二

Swagger v.s. apiDoc

Live Document

Swagger V.S. apidoc
Swagger Core - Java-related libraries for generating and reading Swagger definitions
Swagger UI to generate interactive web-based documentation
Swagger Codegen - Command-line tool for generating both client and server side code from a Swagger definition
Swagger Editor - Browser based editor for authoring Swagger definitions in YAML or JSON format

apiDoc creates a documentation from API annotations in your source code.

評估考量
  • 文件與程式邏輯同步性
  • 統一格式 (易用性、可讀性)
  • 學習成本 (門檻)
  • 版本修改紀錄
Swagger
優:
  • 定義明確的 OpenAPI Specification
  • 工具多 (基於 OpenAPI Specification)
  • 網路資源多
  • 同步性高
缺:
  • 學習成本高,入門不易
  • 必須是 Web Project
  • Project 必須引入 Swagger Library
apiDoc
  • Install
    npm install apidoc -g
  • Use
    apidoc -i /example -o /apidoc

優:
  • 類似 Javadoc 寫法
  • no need to inject third party library
  • have version change log
  • easy to convert apidoc to swagger [https://github.com/fsbahman/apidoc-swagger]
缺:
  • 註解行數多於程式碼
  • 註解終歸是註解,還是有與程式邏輯不同步的可能性