레이블이 csp인 게시물을 표시합니다. 모든 게시물 표시
레이블이 csp인 게시물을 표시합니다. 모든 게시물 표시

k8s grafana iframe

k8s grafana 그래프 하나를 share > embed(iframe) 으로 공유할때
브라우저에서 iframe 로딩이 안된다.
이경우 grafana.ini 설정에 다음 설정을 추가해야 한다.
[security]
allow_embedding = true

추가로 로그인 없이 익명사용자로 접근하기 위해서 다음 설정도 필요하다.
요 설정이 없으면 grafana 로그인 화면으로 보인다.
[auth.anonymous]
enabled = true

위 설정으로 grafana 적용(pod재시작)했다.

로컬에 index.html 에
grafana iframe 과
youtube 동영상 하나 공유 링크(iframe) 
로 2개의 iframe을 추가했다.

브라우저로 보면 youtube iframe 은 보이지만 grafana iframe은 보이지 않는다.
x-frame-options 으로 인해 iframe 과 현재 호스트(localhost)가 달라서 콘솔에 다음과 같은 에러가 발생했다.

Refused to display 'http://grafana.ysoftman.test/' in a frame because it set 'X-Frame-Options' to 'sameorigin'.

아래와 같은 크롬 익스텐션으로 브라우저에서 x-frame-headers 을 무시하면 되긴 한다.

원인은 k8s grafana ingress 별다른 설정이 없어 ngnix 가 디폴트로 X-Frame-Options: sameorigin 로 처리되기 때문이다.

X-Frame-Options ALLOW-FROM=url 로 특정 url 요청을 허용하도록 하면 될것 같았는데,
ALLOW-FROM 는 obsolete 로 최신브라우저에선 동작하지 않는다.

ingress 설정으로 다음과 같이 X-Frame-Options 헤더를 의미없는 값으로 설정하면 보인다.
하지만 브라우져 콘솔에 invalid 에러가 발생한다.
annotations:
  nginx.ingress.kubernetes.io/configuration-snippet: |
    more_set_headers "X-Frame-Options: _for_ysoftman"

다음과 같이 하면 X-Frame-Options 헤더를 응답에서 제외할 수 있다.
annotations:
  nginx.ingress.kubernetes.io/configuration-snippet: |
    more_clear_headers "X-Frame-Options";


#####


만약 csp(Content-Security-Policy)문제인 경우
csp 의 frame-src 지시자로 iframe url 을 제어한다.
csp는 html 에서 다음과 같이 사용할 수 있지만 서버 응답헤더로 설정을 권장한다.
<meta http-equiv="Content-Security-Policy" content="frame-src http://*">

nginx 응답 헤더 추가를 위해 ingress 에 다음과 같이 설정한다.
annotations:
  nginx.ingress.kubernetes.io/configuration-snippet: |
    more_set_headers "Content-Security-Policy: frame-src http://*;";

참고로 frame-ancestors(iframe 를 호출 하는 상위) 경우는 meta 태그로 명시하지 못한다고 한다.
frame-ancestors specifies the sources that can embed the current page. This directive applies to <frame>, <iframe>, <embed>, and <applet> tags. This directive can't be used in <meta> tags and applies only to non-HTML resources.

Content Security Policy

# Content Security Policy(csp,컨텐츠 보안 정책)는
# 웹으로 서비스되는 도메인의 컨텐츠들은 같은 도메인을 명시하고 있어야(동일 출처의 원칙)
# 하는데 그렇지 않은 경우 Cross Site Scripting(XSS, 교사 사이트 스크립트)
# 공격에 취약할 수 있어 이를 방지하고자 적용되는 보안정책이다.
# 웹서버는 응답헤더에 다음과 같이 설정해주면 클라(브라우저)는 출처가
# self(현재출처) 나 https://apis.google.com 인 경우만 스크립트를 수행하게 된다.

Content-Security-Policy: script-src 'self' https://apis.google.com

# script-src 외에도 base-uri, img-src, midea-src, font-src ... 등
# 다양한 리소스 지시자를 사용할 수 있다.
# (각 리소스 지시자를 명시하지 않으면 디폴트로 모두 허용)

# default-src 로 -src 로 끝나는 지시자들의 디폴트 값을 설정하고
# report-uri 로 csp 위반 사항을 설정한 서버로 보고할 수 있다.
# 보고시 전송방식은 post 이고 내용은 json 형식으로 전달된다.

Content-Security-Policy: default-src 'self'; ...; report-uri /my_amazing_csp_report_parser;

# csp 를 적용하지 않고 위반되는 상황이 어떤것들이 있는지 파악(보고)만 하는 형태로
# 다음과 같은 헤더값을 사용할 수 도 있다.

Content-Security-Policy-Report-Only: default-src 'self'; ...; report-uri /my_amazing_csp_report_parser;

# 참고 및 세부설명
https://developers.google.com/web/fundamentals/security/csp/?hl=ko