Skip to content

Commit

Permalink
Use proxy to load custom maps (to bypass CSP)
Browse files Browse the repository at this point in the history
  • Loading branch information
kravets-levko committed Feb 11, 2020
1 parent b331c4c commit 813d97a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion client/app/visualizations/choropleth/hooks/useLoadGeoJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import createReferenceCountingCache from "@/lib/referenceCountingCache";

const cache = createReferenceCountingCache();

function withProxy(url) {
return `/resource-proxy?url=${encodeURIComponent(url)}`;
}

export default function useLoadGeoJson(url) {
const [geoJson, setGeoJson] = useState(null);
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -14,7 +18,7 @@ export default function useLoadGeoJson(url) {
setIsLoading(true);
let cancelled = false;

const promise = cache.get(url, () => axios.get(url).catch(() => null));
const promise = cache.get(url, () => axios.get(withProxy(url)).catch(() => null));
promise.then(data => {
if (!cancelled) {
setGeoJson(isObject(data) ? data : null);
Expand Down
11 changes: 10 additions & 1 deletion redash/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import jsonify
from flask import jsonify, request, Response
from flask_login import login_required
import requests

from redash.handlers.api import api
from redash.handlers.base import routes
Expand All @@ -22,6 +23,14 @@ def status_api():
return jsonify(status)


@routes.route("/resource-proxy", methods=["GET"])
def resource_proxy():
response = requests.get(request.args.get('url'))
allow_headers = ['content-type']
headers = [(name, value) for (name, value) in response.raw.headers.items() if name.lower() in allow_headers]
return Response(response.content, response.status_code, headers)


def init_app(app):
from redash.handlers import (
embed,
Expand Down

0 comments on commit 813d97a

Please sign in to comment.