Fix: add all global layers to CSP

This commit is contained in:
Pieter Vander Vennet 2023-10-08 17:38:28 +02:00
parent 2736740cd0
commit 93630bd1db
2 changed files with 336 additions and 337 deletions

View file

@ -1,24 +1,21 @@
import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFile, writeFileSync } from "fs" import { appendFileSync, existsSync, mkdirSync, readFileSync, writeFile, writeFileSync } from "fs";
import Locale from "../src/UI/i18n/Locale" import Locale from "../src/UI/i18n/Locale";
import Translations from "../src/UI/i18n/Translations" import Translations from "../src/UI/i18n/Translations";
import { Translation } from "../src/UI/i18n/Translation" import { Translation } from "../src/UI/i18n/Translation";
import all_known_layouts from "../src/assets/generated/known_themes.json" import all_known_layouts from "../src/assets/generated/known_themes.json";
import { LayoutConfigJson } from "../src/Models/ThemeConfig/Json/LayoutConfigJson" import { LayoutConfigJson } from "../src/Models/ThemeConfig/Json/LayoutConfigJson";
import LayoutConfig from "../src/Models/ThemeConfig/LayoutConfig" import LayoutConfig from "../src/Models/ThemeConfig/LayoutConfig";
import xml2js from "xml2js" import xml2js from "xml2js";
import ScriptUtils from "./ScriptUtils" import ScriptUtils from "./ScriptUtils";
import { Utils } from "../src/Utils" import { Utils } from "../src/Utils";
import SpecialVisualizations from "../src/UI/SpecialVisualizations" import SpecialVisualizations from "../src/UI/SpecialVisualizations";
import Constants from "../src/Models/Constants" import Constants from "../src/Models/Constants";
import { import { AvailableRasterLayers, RasterLayerPolygon } from "../src/Models/RasterLayers";
AvailableRasterLayers, import { ImmutableStore } from "../src/Logic/UIEventSource";
EditorLayerIndexProperties, import * as crypto from "crypto";
RasterLayerPolygon, import * as eli from "../src/assets/editor-layer-index.json";
} from "../src/Models/RasterLayers" import * as eli_global from "../src/assets/global-raster-layers.json";
import { ImmutableStore } from "../src/Logic/UIEventSource"
import * as crypto from "crypto"
import * as eli from "../src/assets/editor-layer-index.json"
import dom from "svelte/types/compiler/compile/render_dom"
const sharp = require("sharp") const sharp = require("sharp")
const template = readFileSync("theme.html", "utf8") const template = readFileSync("theme.html", "utf8")
const codeTemplate = readFileSync("src/index_theme.ts.template", "utf8") const codeTemplate = readFileSync("src/index_theme.ts.template", "utf8")
@ -219,7 +216,8 @@ function eliUrls(): string[] {
} }
const urls: string[] = [] const urls: string[] = []
const regex = /{switch:([^}]+)}/ const regex = /{switch:([^}]+)}/
for (const feature of eli.features) { const rasterLayers = [...AvailableRasterLayers.vectorLayers, ...eli.features, ...eli_global.layers.map(properties => ({properties})) ]
for (const feature of rasterLayers) {
const url = (<RasterLayerPolygon>feature).properties.url const url = (<RasterLayerPolygon>feature).properties.url
const match = url.match(regex) const match = url.match(regex)
if (match) { if (match) {
@ -245,7 +243,6 @@ function generateCsp(
...Constants.defaultOverpassUrls, ...Constants.defaultOverpassUrls,
Constants.countryCoderEndpoint, Constants.countryCoderEndpoint,
Constants.nominatimEndpoint, Constants.nominatimEndpoint,
...AvailableRasterLayers.vectorLayers.map(l => l.properties.url),
"https://api.openstreetmap.org", "https://api.openstreetmap.org",
"https://pietervdvn.goatcounter.com", "https://pietervdvn.goatcounter.com",
] ]

View file

@ -1,172 +1,174 @@
import { Feature, Polygon } from "geojson" import { Feature, Polygon } from "geojson";
import * as editorlayerindex from "../assets/editor-layer-index.json" import * as editorlayerindex from "../assets/editor-layer-index.json";
import * as globallayers from "../assets/global-raster-layers.json" import * as globallayers from "../assets/global-raster-layers.json";
import { BBox } from "../Logic/BBox" import { BBox } from "../Logic/BBox";
import { Store, Stores } from "../Logic/UIEventSource" import { Store, Stores } from "../Logic/UIEventSource";
import { GeoOperations } from "../Logic/GeoOperations" import { GeoOperations } from "../Logic/GeoOperations";
import { RasterLayerProperties } from "./RasterLayerProperties" import { RasterLayerProperties } from "./RasterLayerProperties";
export class AvailableRasterLayers { export class AvailableRasterLayers {
public static EditorLayerIndex: (Feature<Polygon, EditorLayerIndexProperties> & public static EditorLayerIndex: (Feature<Polygon, EditorLayerIndexProperties> &
RasterLayerPolygon)[] = <any>editorlayerindex.features RasterLayerPolygon)[] = <any>editorlayerindex.features;
public static globalLayers: RasterLayerPolygon[] = globallayers.layers.map( public static globalLayers: RasterLayerPolygon[] = globallayers.layers.map(
(properties) => (properties) =>
<RasterLayerPolygon>{ <RasterLayerPolygon>{
type: "Feature",
properties,
geometry: BBox.global.asGeometry(),
}
)
public static readonly osmCartoProperties: RasterLayerProperties = {
id: "osm",
name: "OpenStreetMap",
url: "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
attribution: {
text: "OpenStreetMap",
url: "https://openStreetMap.org/copyright",
},
best: true,
max_zoom: 19,
min_zoom: 0,
category: "osmbasedmap",
}
public static readonly osmCarto: RasterLayerPolygon = {
type: "Feature", type: "Feature",
properties: AvailableRasterLayers.osmCartoProperties, properties,
geometry: BBox.global.asGeometry(), geometry: BBox.global.asGeometry()
} }
);
public static readonly osmCartoProperties: RasterLayerProperties = {
id: "osm",
name: "OpenStreetMap",
url: "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
attribution: {
text: "OpenStreetMap",
url: "https://openStreetMap.org/copyright"
},
best: true,
max_zoom: 19,
min_zoom: 0,
category: "osmbasedmap"
};
public static readonly maptilerDefaultLayer: RasterLayerPolygon = { public static readonly osmCarto: RasterLayerPolygon = {
type: "Feature", type: "Feature",
properties: { properties: AvailableRasterLayers.osmCartoProperties,
name: "MapTiler", geometry: BBox.global.asGeometry()
url: "https://api.maptiler.com/maps/15cc8f61-0353-4be6-b8da-13daea5f7432/style.json?key=GvoVAJgu46I5rZapJuAy", };
category: "osmbasedmap",
id: "maptiler",
type: "vector",
attribution: {
text: "Maptiler",
url: "https://www.maptiler.com/copyright/",
},
},
geometry: BBox.global.asGeometry(),
}
public static readonly maptilerCarto: RasterLayerPolygon = { public static readonly maptilerDefaultLayer: RasterLayerPolygon = {
type: "Feature", type: "Feature",
properties: { properties: {
name: "MapTiler Carto", name: "MapTiler",
url: "https://api.maptiler.com/maps/openstreetmap/style.json?key=GvoVAJgu46I5rZapJuAy", url: "https://api.maptiler.com/maps/15cc8f61-0353-4be6-b8da-13daea5f7432/style.json?key=GvoVAJgu46I5rZapJuAy",
category: "osmbasedmap", category: "osmbasedmap",
id: "maptiler.carto", id: "maptiler",
type: "vector", type: "vector",
attribution: { attribution: {
text: "Maptiler", text: "Maptiler",
url: "https://www.maptiler.com/copyright/", url: "https://www.maptiler.com/copyright/"
}, }
}, },
geometry: BBox.global.asGeometry(), geometry: BBox.global.asGeometry()
} };
public static readonly maptilerBackdrop: RasterLayerPolygon = { public static readonly maptilerCarto: RasterLayerPolygon = {
type: "Feature", type: "Feature",
properties: { properties: {
name: "MapTiler Backdrop", name: "MapTiler Carto",
url: "https://api.maptiler.com/maps/backdrop/style.json?key=GvoVAJgu46I5rZapJuAy", url: "https://api.maptiler.com/maps/openstreetmap/style.json?key=GvoVAJgu46I5rZapJuAy",
category: "osmbasedmap", category: "osmbasedmap",
id: "maptiler.backdrop", id: "maptiler.carto",
type: "vector", type: "vector",
attribution: { attribution: {
text: "Maptiler", text: "Maptiler",
url: "https://www.maptiler.com/copyright/", url: "https://www.maptiler.com/copyright/"
}, }
}, },
geometry: BBox.global.asGeometry(), geometry: BBox.global.asGeometry()
} };
public static readonly americana: RasterLayerPolygon = {
type: "Feature",
properties: {
name: "Americana",
url: "https://zelonewolf.github.io/openstreetmap-americana/style.json",
category: "osmbasedmap",
id: "americana",
type: "vector",
attribution: {
text: "Americana",
url: "https://github.com/ZeLonewolf/openstreetmap-americana/",
},
},
geometry: BBox.global.asGeometry(),
}
public static readonly vectorLayers = [ AvailableRasterLayers.maptilerDefaultLayer, public static readonly maptilerBackdrop: RasterLayerPolygon = {
AvailableRasterLayers.osmCarto, type: "Feature",
AvailableRasterLayers.maptilerCarto, properties: {
AvailableRasterLayers.maptilerBackdrop, name: "MapTiler Backdrop",
AvailableRasterLayers.americana] url: "https://api.maptiler.com/maps/backdrop/style.json?key=GvoVAJgu46I5rZapJuAy",
category: "osmbasedmap",
id: "maptiler.backdrop",
type: "vector",
attribution: {
text: "Maptiler",
url: "https://www.maptiler.com/copyright/"
}
},
geometry: BBox.global.asGeometry()
};
public static readonly americana: RasterLayerPolygon = {
type: "Feature",
properties: {
name: "Americana",
url: "https://zelonewolf.github.io/openstreetmap-americana/style.json",
category: "osmbasedmap",
id: "americana",
type: "vector",
attribution: {
text: "Americana",
url: "https://github.com/ZeLonewolf/openstreetmap-americana/"
}
},
geometry: BBox.global.asGeometry()
};
public static layersAvailableAt( public static readonly vectorLayers = [
location: Store<{ lon: number; lat: number }> AvailableRasterLayers.maptilerDefaultLayer,
): Store<RasterLayerPolygon[]> { AvailableRasterLayers.osmCarto,
const availableLayersBboxes = Stores.ListStabilized( AvailableRasterLayers.maptilerCarto,
location.mapD((loc) => { AvailableRasterLayers.maptilerBackdrop,
const lonlat: [number, number] = [loc.lon, loc.lat] AvailableRasterLayers.americana
return AvailableRasterLayers.EditorLayerIndex.filter((eliPolygon) => ];
BBox.get(eliPolygon).contains(lonlat)
) public static layersAvailableAt(
}) location: Store<{ lon: number; lat: number }>
) ): Store<RasterLayerPolygon[]> {
const available = Stores.ListStabilized( const availableLayersBboxes = Stores.ListStabilized(
availableLayersBboxes.map((eliPolygons) => { location.mapD((loc) => {
const loc = location.data const lonlat: [number, number] = [loc.lon, loc.lat];
const lonlat: [number, number] = [loc.lon, loc.lat] return AvailableRasterLayers.EditorLayerIndex.filter((eliPolygon) =>
const matching: RasterLayerPolygon[] = eliPolygons.filter((eliPolygon) => { BBox.get(eliPolygon).contains(lonlat)
if (eliPolygon.geometry === null) { );
return true // global ELI-layer })
} );
return GeoOperations.inside(lonlat, eliPolygon) const available = Stores.ListStabilized(
}) availableLayersBboxes.map((eliPolygons) => {
matching.push(...AvailableRasterLayers.globalLayers) const loc = location.data;
matching.unshift(...AvailableRasterLayers.vectorLayers) const lonlat: [number, number] = [loc.lon, loc.lat];
return matching const matching: RasterLayerPolygon[] = eliPolygons.filter((eliPolygon) => {
}) if (eliPolygon.geometry === null) {
) return true; // global ELI-layer
return available }
} return GeoOperations.inside(lonlat, eliPolygon);
});
matching.push(...AvailableRasterLayers.globalLayers);
matching.unshift(...AvailableRasterLayers.vectorLayers);
return matching;
})
);
return available;
}
} }
export class RasterLayerUtils { export class RasterLayerUtils {
/** /**
* Selects, from the given list of available rasterLayerPolygons, a rasterLayer. * Selects, from the given list of available rasterLayerPolygons, a rasterLayer.
* This rasterlayer will be of type 'preferredCategory' and will be of the 'best'-layer (if available). * This rasterlayer will be of type 'preferredCategory' and will be of the 'best'-layer (if available).
* Returns 'undefined' if no such layer is available * Returns 'undefined' if no such layer is available
* @param available * @param available
* @param preferredCategory * @param preferredCategory
* @param ignoreLayer * @param ignoreLayer
*/ */
public static SelectBestLayerAccordingTo( public static SelectBestLayerAccordingTo(
available: RasterLayerPolygon[], available: RasterLayerPolygon[],
preferredCategory: string, preferredCategory: string,
ignoreLayer?: RasterLayerPolygon ignoreLayer?: RasterLayerPolygon
): RasterLayerPolygon { ): RasterLayerPolygon {
let secondBest: RasterLayerPolygon = undefined let secondBest: RasterLayerPolygon = undefined;
for (const rasterLayer of available) { for (const rasterLayer of available) {
if (rasterLayer === ignoreLayer) { if (rasterLayer === ignoreLayer) {
continue continue;
} }
const p = rasterLayer.properties const p = rasterLayer.properties;
if (p.category === preferredCategory) { if (p.category === preferredCategory) {
if (p.best) { if (p.best) {
return rasterLayer return rasterLayer;
}
if (!secondBest) {
secondBest = rasterLayer
}
}
} }
return secondBest if (!secondBest) {
secondBest = rasterLayer;
}
}
} }
return secondBest;
}
} }
export type RasterLayerPolygon = Feature<Polygon, RasterLayerProperties> export type RasterLayerPolygon = Feature<Polygon, RasterLayerProperties>
@ -178,165 +180,165 @@ export type RasterLayerPolygon = Feature<Polygon, RasterLayerProperties>
* which was then converted with http://borischerny.com/json-schema-to-typescript-browser/ * which was then converted with http://borischerny.com/json-schema-to-typescript-browser/
*/ */
export interface EditorLayerIndexProperties extends RasterLayerProperties { export interface EditorLayerIndexProperties extends RasterLayerProperties {
/**
* The name of the imagery source
*/
readonly name: string;
/**
* Whether the imagery name should be translated
*/
readonly i18n?: boolean;
readonly type:
| "tms"
| "wms"
| "bing"
| "scanex"
| "wms_endpoint"
| "wmts"
| "vector"; /* Vector is not actually part of the ELI-spec, we add it for vector layers */
/**
* A rough categorisation of different types of layers. See https://github.com/osmlab/editor-layer-index/blob/gh-pages/CONTRIBUTING.md#categories for a description of the individual categories.
*/
readonly category?:
| "photo"
| "map"
| "historicmap"
| "osmbasedmap"
| "historicphoto"
| "qa"
| "elevation"
| "other";
/**
* A URL template for imagery tiles
*/
readonly url: string;
readonly min_zoom?: number;
readonly max_zoom?: number;
/**
* explicit/implicit permission by the owner for use in OSM
*/
readonly permission_osm?: "explicit" | "implicit" | "no";
/**
* A URL for the license or permissions for the imagery
*/
readonly license_url?: string;
/**
* A URL for the privacy policy of the operator or false if there is no existing privacy policy for tis imagery.
*/
readonly privacy_policy_url?: string | boolean;
/**
* A unique identifier for the source; used in imagery_used changeset tag
*/
readonly id: string;
/**
* A short English-language description of the source
*/
readonly description?: string;
/**
* The ISO 3166-1 alpha-2 two letter country code in upper case. Use ZZ for unknown or multiple.
*/
readonly country_code?: string;
/**
* Whether this imagery should be shown in the default world-wide menu
*/
readonly default?: boolean;
/**
* Whether this imagery is the best source for the region
*/
readonly best?: boolean;
/**
* The age of the oldest imagery or data in the source, as an RFC3339 date or leading portion of one
*/
readonly start_date?: string;
/**
* The age of the newest imagery or data in the source, as an RFC3339 date or leading portion of one
*/
readonly end_date?: string;
/**
* HTTP header to check for information if the tile is invalid
*/
readonly no_tile_header?: {
/** /**
* The name of the imagery source * This interface was referenced by `undefined`'s JSON-Schema definition
* via the `patternProperty` "^.*$".
*/ */
readonly name: string [k: string]: string[] | null
/** };
* Whether the imagery name should be translated /**
*/ * 'true' if tiles are transparent and can be overlaid on another source
readonly i18n?: boolean */
readonly type: readonly overlay?: boolean & string;
| "tms" readonly available_projections?: string[];
| "wms" readonly attribution?: {
| "bing" readonly url?: string
| "scanex" readonly text?: string
| "wms_endpoint" readonly html?: string
| "wmts" readonly required?: boolean
| "vector" /* Vector is not actually part of the ELI-spec, we add it for vector layers */ };
/** /**
* A rough categorisation of different types of layers. See https://github.com/osmlab/editor-layer-index/blob/gh-pages/CONTRIBUTING.md#categories for a description of the individual categories. * A URL for an image, that can be displayed in the list of imagery layers next to the name
*/ */
readonly category?: readonly icon?: string;
| "photo" /**
| "map" * A link to an EULA text that has to be accepted by the user, before the imagery source is added. Can contain {lang} to be replaced by a current user language wiki code (like FR:) or an empty string for the default English text.
| "historicmap" */
| "osmbasedmap" readonly eula?: string;
| "historicphoto" /**
| "qa" * A URL for an image, that is displayed in the mapview for attribution
| "elevation" */
| "other" readonly "logo-image"?: string;
/** /**
* A URL template for imagery tiles * Customized text for the terms of use link (default is "Background Terms of Use")
*/ */
readonly url: string readonly "terms-of-use-text"?: string;
readonly min_zoom?: number /**
readonly max_zoom?: number * Specify a checksum for tiles, which aren't real tiles. `type` is the digest type and can be MD5, SHA-1, SHA-256, SHA-384 and SHA-512, value is the hex encoded checksum in lower case. To create a checksum save the tile as file and upload it to e.g. https://defuse.ca/checksums.htm.
/** */
* explicit/implicit permission by the owner for use in OSM readonly "no-tile-checksum"?: string;
*/ /**
readonly permission_osm?: "explicit" | "implicit" | "no" * header-name attribute specifies a header returned by tile server, that will be shown as `metadata-key` attribute in Show Tile Info dialog
/** */
* A URL for the license or permissions for the imagery readonly "metadata-header"?: string;
*/ /**
readonly license_url?: string * Set to `true` if imagery source is properly aligned and does not need imagery offset adjustments. This is used for OSM based sources too.
/** */
* A URL for the privacy policy of the operator or false if there is no existing privacy policy for tis imagery. readonly "valid-georeference"?: boolean;
*/ /**
readonly privacy_policy_url?: string | boolean * Size of individual tiles delivered by a TMS service
/** */
* A unique identifier for the source; used in imagery_used changeset tag readonly "tile-size"?: number;
*/ /**
readonly id: string * Whether tiles status can be accessed by appending /status to the tile URL and can be submitted for re-rendering by appending /dirty.
/** */
* A short English-language description of the source readonly "mod-tile-features"?: string;
*/ /**
readonly description?: string * HTTP headers to be sent to server. It has two attributes header-name and header-value. May be specified multiple times.
/** */
* The ISO 3166-1 alpha-2 two letter country code in upper case. Use ZZ for unknown or multiple. readonly "custom-http-headers"?: {
*/ readonly "header-name"?: string
readonly country_code?: string readonly "header-value"?: string
/** };
* Whether this imagery should be shown in the default world-wide menu /**
*/ * Default layer to open (when using WMS_ENDPOINT type). Contains list of layer tag with two attributes - name and style, e.g. `"default-layers": ["layer": { name="Basisdata_NP_Basiskart_JanMayen_WMTS_25829" "style":"default" } ]` (not allowed in `mirror` attribute)
readonly default?: boolean */
/** readonly "default-layers"?: {
* Whether this imagery is the best source for the region layer?: {
*/ "layer-name"?: string
readonly best?: boolean "layer-style"?: string
/** [k: string]: unknown
* The age of the oldest imagery or data in the source, as an RFC3339 date or leading portion of one
*/
readonly start_date?: string
/**
* The age of the newest imagery or data in the source, as an RFC3339 date or leading portion of one
*/
readonly end_date?: string
/**
* HTTP header to check for information if the tile is invalid
*/
readonly no_tile_header?: {
/**
* This interface was referenced by `undefined`'s JSON-Schema definition
* via the `patternProperty` "^.*$".
*/
[k: string]: string[] | null
} }
/** [k: string]: unknown
* 'true' if tiles are transparent and can be overlaid on another source }[];
*/ /**
readonly overlay?: boolean & string * format to use when connecting tile server (when using WMS_ENDPOINT type)
readonly available_projections?: string[] */
readonly attribution?: { readonly format?: string;
readonly url?: string /**
readonly text?: string * If `true` transparent tiles will be requested from WMS server
readonly html?: string */
readonly required?: boolean readonly transparent?: boolean & string;
} /**
/** * minimum expiry time for tiles in seconds. The larger the value, the longer entry in cache will be considered valid
* A URL for an image, that can be displayed in the list of imagery layers next to the name */
*/ readonly "minimum-tile-expire"?: number;
readonly icon?: string
/**
* A link to an EULA text that has to be accepted by the user, before the imagery source is added. Can contain {lang} to be replaced by a current user language wiki code (like FR:) or an empty string for the default English text.
*/
readonly eula?: string
/**
* A URL for an image, that is displayed in the mapview for attribution
*/
readonly "logo-image"?: string
/**
* Customized text for the terms of use link (default is "Background Terms of Use")
*/
readonly "terms-of-use-text"?: string
/**
* Specify a checksum for tiles, which aren't real tiles. `type` is the digest type and can be MD5, SHA-1, SHA-256, SHA-384 and SHA-512, value is the hex encoded checksum in lower case. To create a checksum save the tile as file and upload it to e.g. https://defuse.ca/checksums.htm.
*/
readonly "no-tile-checksum"?: string
/**
* header-name attribute specifies a header returned by tile server, that will be shown as `metadata-key` attribute in Show Tile Info dialog
*/
readonly "metadata-header"?: string
/**
* Set to `true` if imagery source is properly aligned and does not need imagery offset adjustments. This is used for OSM based sources too.
*/
readonly "valid-georeference"?: boolean
/**
* Size of individual tiles delivered by a TMS service
*/
readonly "tile-size"?: number
/**
* Whether tiles status can be accessed by appending /status to the tile URL and can be submitted for re-rendering by appending /dirty.
*/
readonly "mod-tile-features"?: string
/**
* HTTP headers to be sent to server. It has two attributes header-name and header-value. May be specified multiple times.
*/
readonly "custom-http-headers"?: {
readonly "header-name"?: string
readonly "header-value"?: string
}
/**
* Default layer to open (when using WMS_ENDPOINT type). Contains list of layer tag with two attributes - name and style, e.g. `"default-layers": ["layer": { name="Basisdata_NP_Basiskart_JanMayen_WMTS_25829" "style":"default" } ]` (not allowed in `mirror` attribute)
*/
readonly "default-layers"?: {
layer?: {
"layer-name"?: string
"layer-style"?: string
[k: string]: unknown
}
[k: string]: unknown
}[]
/**
* format to use when connecting tile server (when using WMS_ENDPOINT type)
*/
readonly format?: string
/**
* If `true` transparent tiles will be requested from WMS server
*/
readonly transparent?: boolean & string
/**
* minimum expiry time for tiles in seconds. The larger the value, the longer entry in cache will be considered valid
*/
readonly "minimum-tile-expire"?: number
} }