테마 스크립트를 CSP에 자동으로 추가하는 방법

외부 JavaScript 참조를 테마에 추가하려는 사용자의 지원 주제를 자주 접하게 됩니다. 기본적으로 콘텐츠 보안 정책(CSP)은 이를 차단하며, 관리자가 CSP 사이트 설정(또는 테마 설정/수정기)에 스크립트를 수동으로 추가해야 합니다.

그러나 @Johani님이 이를 개선할 수 있는 훌륭한 제안을 해주셨습니다.

테마 개발자에게 관리자 UI에서 복사/붙여넣기를 하도록 요구하는 대신, 이 과정을 자동화할 수 있습니다. 테마의 모든 HTML을 파싱하여 외부 스크립트의 src를 추출하고 CSP에 추가하는 것입니다.

결국 컴퓨터는 인간보다 반복적인 복사/붙여넣기 작업에서 훨씬 더 능숙하니까요!

이를 구현하는 PR을 열었습니다:

I am seeing some console errors locally:

image

In production environments, I also see sources added in the format https://CDN_SERVER/theme-javascripts/31657759d037d8c06397e9965a1113169100846e.js... which is redundant, because the policy already whitelists https://CDN_SERVER/theme-javascripts. Probably limit the auto-extension to to external script sources only?

Thanks @pmusaraj, I added some more checks to make sure we don’t serve invalid urls in the CSP, and also made sure theme-javascript URLs are excluded:

https://github.com/discourse/discourse/commit/f95609ae23ce1604b5f53c9d232e66895cfc9ee7

This is awesome. @merefield isn’t this super cool?