geido commented on code in PR #28260:
URL: https://github.com/apache/superset/pull/28260#discussion_r1583411728


##########
superset/views/img_proxy.py:
##########
@@ -0,0 +1,105 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from io import BytesIO
+from typing import Any
+from urllib.parse import urlparse
+
+import requests
+from flask import abort, request, Response
+from flask_appbuilder.api import expose
+from flask_wtf.csrf import same_origin
+from PIL import Image
+
+from superset import event_logger
+from superset.superset_typing import FlaskResponse
+from superset.utils.core import get_user_id
+
+from .base import BaseSupersetView
+
+
+class ImgProxyView(BaseSupersetView):
+    route_base = "/img_proxy"
+
+    @expose("/")
+    @event_logger.log_this
+    def img_proxy(self) -> FlaskResponse:
+        """
+        Proxy to an external URL, to overcome CORS restrictions.
+        Returns a HTTP response containing the resource fetched from the 
external URL.
+        """
+        if not get_user_id():
+            abort(403)
+
+        url = request.args.get("url")
+        allowed_content_types = ["image/"]
+
+        if not url:
+            abort(400)
+
+        parsed_url = urlparse(url)
+        if parsed_url.scheme not in ["http", "https"]:
+            abort(400)
+
+        if not request.referrer or (
+            request.referrer and not same_origin(request.referrer, 
request.url_root)
+        ):
+            abort(403)
+
+        response = self.fetch_resource(url)

Review Comment:
   The request needs to be authenticated but I agree there is potential risk 
here. We could limit the requests to certain allowed domains, however, that 
would require that the users of a dashboard know upfront the domains that will 
host images that they might want to attach to a dashboard via markdown. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to