Skip to content

Commit

Permalink
XSS: sanitize more GFI content
Browse files Browse the repository at this point in the history
  • Loading branch information
nboisteault committed Nov 7, 2024
1 parent 4d5bbf1 commit db36c55
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
7 changes: 3 additions & 4 deletions assets/src/legacy/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -2950,9 +2950,8 @@ window.lizMap = function() {

// Query the server
$.post(globalThis['lizUrls'].service, wmsOptions, function(data) {
aCallback(data);
aCallback(Utils.sanitizeGFIContent(data));
});

}

// Get the popup content for a layer given a feature
Expand Down Expand Up @@ -3024,8 +3023,8 @@ window.lizMap = function() {

// Query the server
$.post(globalThis['lizUrls'].service, wmsOptions, function(data) {
if(aCallback){
aCallback(globalThis['lizUrls'].service, wmsOptions, data);
if (aCallback) {
aCallback(globalThis['lizUrls'].service, wmsOptions, Utils.sanitizeGFIContent(data));
}
});
}
Expand Down
17 changes: 2 additions & 15 deletions assets/src/modules/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { mainLizmap } from '../modules/Globals.js';
import Overlay from 'ol/Overlay.js';
import WMS from '../modules/WMS.js';
import DOMPurify from 'dompurify';
import Utils from '../modules/Utils.js';

/**
* @class
Expand Down Expand Up @@ -178,20 +178,7 @@ export default class Popup {
document.getElementById('newOlMap').style.cursor = 'wait';

wms.getFeatureInfo(wmsParams).then(response => {
DOMPurify.addHook('afterSanitizeAttributes', node => {
if (node.nodeName === 'IFRAME') {
node.setAttribute('sandbox','allow-scripts allow-forms');
}
});
const sanitizedResponse = DOMPurify.sanitize(response, {
ADD_TAGS: ['iframe'],
ADD_ATTR: ['target'],
CUSTOM_ELEMENT_HANDLING: {
tagNameCheck: /^lizmap-/,
attributeNameCheck: /crs|bbox|edition-restricted|layerid|layertitle|uniquefield|expressionfilter|withgeometry|sortingfield|sortingorder|draggable/,
}
});
lizMap.displayGetFeatureInfo(sanitizedResponse, { x: xCoord, y: yCoord }, evt?.coordinate);
lizMap.displayGetFeatureInfo(Utils.sanitizeGFIContent(response), { x: xCoord, y: yCoord }, evt?.coordinate);
}).finally(() => {
document.getElementById('newOlMap').style.cursor = 'auto';
});
Expand Down
17 changes: 17 additions & 0 deletions assets/src/modules/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { NetworkError, HttpError, ResponseError } from './Errors.js';
import DOMPurify from 'dompurify';

/**
* The main utils methods
Expand Down Expand Up @@ -206,4 +207,20 @@ export default class Utils {
const scale = resolution * inchesPerMeter * DPI * metersPerUnit;
return scale;
}

static sanitizeGFIContent(content) {
DOMPurify.addHook('afterSanitizeAttributes', node => {
if (node.nodeName === 'IFRAME') {
node.setAttribute('sandbox','allow-scripts allow-forms');
}
});
return DOMPurify.sanitize(content, {
ADD_TAGS: ['iframe'],
ADD_ATTR: ['target'],
CUSTOM_ELEMENT_HANDLING: {
tagNameCheck: /^lizmap-/,
attributeNameCheck: /crs|bbox|edition-restricted|layerid|layertitle|uniquefield|expressionfilter|withgeometry|sortingfield|sortingorder|draggable/,
}
});
}
}
2 changes: 1 addition & 1 deletion tests/qgis-projects/tests/xss.qgs.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"atlasFeatureLabel": "description",
"atlasSortField": "id",
"atlasHighlightGeometry": "False",
"atlasDisplayPopup": "False",
"atlasDisplayPopup": "True",
"atlasTriggerFilter": "False",
"atlasDuration": 5,
"atlasEnabled": "True",
Expand Down

0 comments on commit db36c55

Please sign in to comment.