Try   HackMD

新人資訊

技術-後台類-azure function #6-搞懂授權機制

記得在新增 HttpTrigger 時的一系列選項有出現這一個「授權等級」,當初為了快速測試就先選最寬鬆無管制的 Anonymous,現在我先把他改成 Function 試試。只要修改 function.json 就行,不須整個重來。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

此設定一改,並不影響本機的測試,故直接 deploy to azure 試試。果然回應 Http Error 401 錯誤。透過閱讀文件我了解到,在 azure console 可以找到系統預設已經生成一個 Key 可以直接取得,在網址列直接多傳一個 code={key value} 即可,經測試果然恢復正常。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

另外在 function app 的整個專案層級還有兩個 key,試著傳入此層級的 default key 照樣可以成功存取,因此專案層級的授權是更大的,可執行該專案所有的 function。至於還有一個預設的 _master,則是當 function 指定 admin 存取層級,就只能允許這個 _master key 了。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

但仔細想想,有了這個 key 似乎沒達到甚麼保護作用!因為我若授權一個前端程式透過 browser 來呼叫,所有使用者可能直接在網址列就看到 key,即使這個呼叫是包裝在 javascript 隱密進行的,在如 chrome 這種強大的 browser 之下,其提供的開發者工具也可以輕易的揪出,這個 key 也就形同向全世界公開,根本不需要駭客就可以把我的後台呼叫到爛,這可不是好玩的事!這該如何解決?

原廠文件說此 app key 只能稍微增加一點呼叫的難度,正規用法是建議打開 function app 層級的 Authentication,點進去有許多 identity provider 可選,透過認證程序掌握執行者的身分。但是這不妙,在文件堆中很快地就迷失了方向,關於認證和授權的知識簡直是一座大山,還不到去挑戰它的時候,畢竟目前根本沒有人要用我的系統阿,哈哈。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

所以目前就先用 app key 擋一下,至少可以確保閒雜人等不會隨便呼叫,下一步需要用 web page 呼叫它時再來研究。待續。

By Newman Chen 2022/3/7

參考資料

https://docs.microsoft.com/zh-tw/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp#authorization-keys