Der beste Weg, um Query-String-Parameter sicher lesen?
-
02-07-2019 - |
Frage
Wir haben ein Projekt, das ein Code-Snippet erzeugt, die auf verschiedenen anderen Projekten verwendet werden können. Der Zweck des Codes ist zwei Parameter aus dem Abfrage-String zu lesen und sie auf das „src“ -Attribut eines iframe zuweisen.
Zum Beispiel ist die Seite unter der URL http: //oursite/Page.aspx einer = 1 & b = 2 würde JavaScript darin die "a" und "b" Parameter zu lesen. Die JavaScript würde dann das „src“ -Attribut eines iframe auf diesen Parametern basieren. Zum Beispiel: " "
Wir tun dies derzeit mit serverseitigen Code, Anti Cross-Scripting-Bibliothek von Microsoft verwendet die Parameter zu überprüfen. Allerdings hat eine neue Anforderung besagt kommen, dass wir JavaScript verwenden müssen, und dass es keine Drittanbieter-JavaScript-Tools (wie jQuery oder Prototype) verwenden kann.
Ein Weg, den ich kenne, ist alle Instanzen von „<“, Apostroph zu ersetzen, und doppelten Anführungszeichen aus den Parametern, bevor Sie sie, aber das scheint nicht sicher genug für mich.
Einer der Parameter ist immer ein „P“, gefolgt von 9 Zahlen. Der andere Parameter ist immer 15 alphanumerische Zeichen. (Danke Liam für die Annahme, ich mache das klar).
Hat jemand irgendwelche Vorschläge für uns?
Vielen Dank für Ihre Zeit.
Lösung
Don't use escape and unescape, use decodeURIComponent. E.g.
function queryParameters(query) {
var keyValuePairs = query.split(/[&?]/g);
var params = {};
for (var i = 0, n = keyValuePairs.length; i < n; ++i) {
var m = keyValuePairs[i].match(/^([^=]+)(?:=([\s\S]*))?/);
if (m) {
var key = decodeURIComponent(m[1]);
(params[key] || (params[key] = [])).push(decodeURIComponent(m[2]));
}
}
return params;
}
and pass in document.location.search.
As far as turning < into <, that is not sufficient to make sure that the content can be safely injected into HTML without allowing script to run. Make sure you escape the following <, >, &, and ".
It will not guarantee that the parameters were not spoofed. If you need to verify that one of your servers generated the URL, do a search on URL signing.
Andere Tipps
Using a whitelist-approach would be better I guess. Avoid only stripping out "bad" things. Strip out anything except for what you think is "safe".
Also I'd strongly encourage to do a HTMLEncode the Parameters. There should be plenty of Javascript functions that can this.
you can use javascript's escape() and unescape() functions.
Several things you should be doing:
- Strictly whitelist your accepted values, according to type, format, range, etc
- Explicitly blacklist certain characters (even though this is usually bypassable), IF your whitelist cannot be extremely tight.
- Encode the values before output, if youre using Anti-XSS you already know that a simple HtmlEncode is not enough
- Set the src property through the DOM - and not by generating HTML fragment
- Use the dynamic value only as a querystring parameter, and not for arbitrary sites; i.e. hardcode the name of the server, target page, etc.
- Is your site over SSL? If so, using a frame may cause inconsistencies with SSL UI...
- Using named frames in general, can allow Frame Spoofing; if on a secure site, this may be a relevant attack vector (for use with phishing etc.)
You can use regular expressions to validate that you have a P followed by 9 integers and that you have 15 alphanumeric values. I think that book that I have at my desk of RegEx has some examples in JavaScript to help you.
Limiting the charset to only ASCII values will help, and follow all the advice above (whitelist, set src through DOM, etc.)