Skip to content

Commit

Permalink
JS: Post-processing query for inline test expectations
Browse files Browse the repository at this point in the history
  • Loading branch information
hvitved committed Oct 7, 2024
1 parent 3f1e155 commit 9c53960
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
3 changes: 2 additions & 1 deletion javascript/ql/test/query-tests/Security/CWE-611/Xxe.qlref
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Security/CWE-611/Xxe.ql
query: Security/CWE-611/Xxe.ql
postprocess: TestUtilities/InlineExpectationsTestQuery.ql
4 changes: 2 additions & 2 deletions javascript/ql/test/query-tests/Security/CWE-611/domparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ function test() {
var parser;
try {
// NOT OK: XMLDOM expands external entities by default
(new ActiveXObject("Microsoft.XMLDOM")).loadXML(src);
(new ActiveXObject("Microsoft.XMLDOM")).loadXML(src); // $ BAD
} catch (e) {
// NOT OK: MSXML expands external entities by default
(new ActiveXObject("Msxml2.DOMDocument")).loadXML(src);
(new ActiveXObject("Msxml2.DOMDocument")).loadXML(src); // $ BAD
}
}
}
16 changes: 8 additions & 8 deletions javascript/ql/test/query-tests/Security/CWE-611/libxml.noent.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const express = require('express');
const libxmljs = require('libxmljs');

express().get('/some/path', function(req) {
express().get('/some/path', function (req) {
// NOT OK: unguarded entity expansion
libxmljs.parseXml(req.param("some-xml"), { noent: true });
libxmljs.parseXml(req.param("some-xml"), { noent: true }); // $ BAD
});

express().post('/some/path', function(req, res) {
express().post('/some/path', function (req, res) {
// NOT OK: unguarded entity expansion
libxmljs.parseXml(req.param("some-xml"), { noent: true });
libxmljs.parseXml(req.param("some-xml"), { noent: true }); // $ BAD

// NOT OK: unguarded entity expansion
libxmljs.parseXmlString(req.param("some-xml"), {noent:true})
libxmljs.parseXmlString(req.param("some-xml"), { noent: true }) // $ BAD
// NOT OK: unguarded entity expansion
libxmljs.parseXmlString(req.files.products.data.toString('utf8'), {noent:true})
libxmljs.parseXmlString(req.files.products.data.toString('utf8'), { noent: true }) // $ BAD

// OK - no entity expansion
libxmljs.parseXmlString(req.files.products.data.toString('utf8'), {noent:false})
libxmljs.parseXmlString(req.files.products.data.toString('utf8'), { noent: false })
});
4 changes: 2 additions & 2 deletions javascript/ql/test/query-tests/Security/CWE-611/libxml.sax.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require('express');
const libxmljs = require('libxmljs');

express().get('/some/path', function(req) {
express().get('/some/path', function (req) {
const parser = new libxmljs.SaxParser();
parser.parseString(req.param("some-xml")); // NOT OK: the SAX parser expands external entities by default
parser.parseString(req.param("some-xml")); // $ BAD: the SAX parser expands external entities by default
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const express = require('express');
const libxmljs = require('libxmljs');

express().get('/some/path', function(req) {
express().get('/some/path', function (req) {
const parser = new libxmljs.SaxPushParser();
parser.push(req.param("some-xml")); // NOT OK: the SAX parser expands external entities by default
parser.push(req.param("some-xml")); // $ BAD: the SAX parser expands external entities by default
});
21 changes: 21 additions & 0 deletions javascript/ql/test/testUtilities/InlineExpectationsTestQuery.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @kind test-postprocess
*/

private import javascript
private import codeql.util.test.InlineExpectationsTest as T
private import internal.InlineExpectationsTestImpl
import T::TestPostProcessing
import T::TestPostProcessing::Make<Impl, Input>

private module Input implements T::TestPostProcessing::InputSig<Impl> {
string getRelativeUrl(Location location) {
exists(File f, int startline, int startcolumn, int endline, int endcolumn |
location.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and
f = location.getFile()
|
result =
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
)
}
}

0 comments on commit 9c53960

Please sign in to comment.