Skip to content

Commit facae4e

Browse files
committed
JS: Accept alerts in HardcodedCredentials and add Sink tags
This query now uses the source as the primary alert location, and some old comments appeared at the sink. To make the change easier to verify, this commit migrates the test to include Sink tags. (Source/Sink tags in general are added later)
1 parent 733074c commit facae4e

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

javascript/ql/test/query-tests/Security/CWE-798/HardcodedCredentials.js

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@
168168
const base64 = require('base-64');
169169
const fetch = require("node-fetch");
170170

171-
const USER = 'sdsdag';
172-
const PASS = 'sdsdag';
171+
const USER = 'sdsdag'; // $ Alert
172+
const PASS = 'sdsdag'; // $ Alert
173173
const AUTH = base64.encode(`${USER}:${PASS}`);
174174

175175
const rsp = await fetch(ENDPOINT, {
176176
method: 'get',
177177
headers: new fetch.Headers({
178-
"Authorization": `Basic ${AUTH}`,
178+
"Authorization": `Basic ${AUTH}`, // $ Sink
179179
"Content-Type": 'application/json'
180180
})
181181
});
@@ -185,14 +185,14 @@
185185
body: JSON.stringify(body),
186186
headers: {
187187
"Content-Type": 'application/json',
188-
"Authorization": `Basic ${AUTH}`
188+
"Authorization": `Basic ${AUTH}` // $ Sink
189189
},
190190
})
191191

192192
var headers = new fetch.Headers({
193193
"Content-Type": 'application/json'
194194
});
195-
headers.append("Authorization", `Basic ${AUTH}`)
195+
headers.append("Authorization", `Basic ${AUTH}`) // $ Sink
196196
fetch(ENDPOINT, {
197197
method: 'get',
198198
headers: headers
@@ -201,7 +201,7 @@
201201
var headers2 = new fetch.Headers({
202202
"Content-Type": 'application/json'
203203
});
204-
headers2.set("Authorization", `Basic ${AUTH}`)
204+
headers2.set("Authorization", `Basic ${AUTH}`) // $ Sink
205205
fetch(ENDPOINT, {
206206
method: 'get',
207207
headers: headers2
@@ -211,14 +211,14 @@
211211
(function () {
212212
const base64 = require('base-64');
213213

214-
const USER = 'sdsdag';
215-
const PASS = 'sdsdag';
214+
const USER = 'sdsdag'; // $ Alert
215+
const PASS = 'sdsdag'; // $ Alert
216216
const AUTH = base64.encode(`${USER}:${PASS}`);
217217

218218
// browser API
219219
var headers = new Headers();
220220
headers.append("Content-Type", 'application/json');
221-
headers.append("Authorization", `Basic ${AUTH}`);
221+
headers.append("Authorization", `Basic ${AUTH}`); // $ Sink
222222
fetch(ENDPOINT, {
223223
method: 'get',
224224
headers: headers
@@ -228,25 +228,25 @@
228228
(async function () {
229229
import fetch from 'node-fetch';
230230

231-
const username = 'sdsdag';
231+
const username = 'sdsdag'; // $ Alert
232232
const password = config.get('some_actually_secrect_password');
233233
const response = await fetch(ENDPOINT, {
234234
method: 'get',
235235
headers: {
236236
'Content-Type': 'application/json',
237-
Authorization: 'Basic ' + Buffer.from(username + ':' + password).toString('base64'),
237+
Authorization: 'Basic ' + Buffer.from(username + ':' + password).toString('base64'), // $ Sink
238238
},
239239
});
240240
})
241241

242242
(function () {
243243
import jwt from "jsonwebtoken";
244244

245-
var privateKey = "myHardCodedPrivateKey";
246-
var token = jwt.sign({ foo: 'bar' }, privateKey, { algorithm: 'RS256'});
245+
var privateKey = "myHardCodedPrivateKey"; // $ Alert
246+
var token = jwt.sign({ foo: 'bar' }, privateKey, { algorithm: 'RS256'}); // $ Sink
247247

248-
var publicKey = "myHardCodedPublicKey";
249-
jwt.verify(token, publicKey, function(err, decoded) {
248+
var publicKey = "myHardCodedPublicKey"; // $ Alert
249+
jwt.verify(token, publicKey, function(err, decoded) { // $ Sink
250250
console.log(decoded);
251251
});
252252
})();
@@ -305,34 +305,34 @@
305305
(function () {
306306
const jwt_simple = require("jwt-simple");
307307

308-
var privateKey = "myHardCodedPrivateKey";
309-
jwt_simple.decode(UserToken, privateKey); // $ Alert
308+
var privateKey = "myHardCodedPrivateKey"; // $ Alert
309+
jwt_simple.decode(UserToken, privateKey); // $ Sink
310310
})();
311311

312312

313313
(async function () {
314314
const jose = require("jose");
315315

316-
var privateKey = "myHardCodedPrivateKey";
317-
jose.jwtVerify(token, new TextEncoder().encode(privateKey)) // $ Alert
316+
var privateKey = "myHardCodedPrivateKey"; // $ Alert
317+
jose.jwtVerify(token, new TextEncoder().encode(privateKey)) // $ Sink
318318

319319
const spki = `-----BEGIN PUBLIC KEY-----
320320
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwhYOFK2Ocbbpb/zVypi9...
321-
-----END PUBLIC KEY-----`
321+
-----END PUBLIC KEY-----` // $ Alert
322322
let publicKey = await jose.importSPKI(spki, 'RS256')
323-
jose.jwtVerify(token, publicKey) // $ Alert
323+
jose.jwtVerify(token, publicKey) // $ Sink
324324

325325
const alg = 'RS256'
326326
const jwk = {
327327
kty: 'RSA',
328-
n: 'whYOFK2Ocbbpb_zVypi9SeKiNUqKQH0zTKN1-6f...',
328+
n: 'whYOFK2Ocbbpb_zVypi9SeKiNUqKQH0zTKN1-6f...', // $ Alert
329329
e: 'AQAB',
330330
}
331331
publicKey = await jose.importJWK(jwk, alg)
332332
const jwt =
333333
'eyJhbGciOiJSUzI1NiJ9.eyJ1cm46ZXhhbXBsZTpjbGFpbSI6dHJ1ZSwiaWF0IjoxNjY5MDU2NDg4LCJpc3MiOiJ1cm46ZXhhbXBsZTppc3N1ZXIiLCJhdWQiOiJ1cm46ZXhhbXBsZTphdWRpZW5jZSJ9.gXrPZ3yM_60dMXGE69dusbpzYASNA-XIOwsb5D5xYnSxyj6_D6OR_uR_1vqhUm4AxZxcrH1_-XJAve9HCw8az_QzHcN-nETt-v6stCsYrn6Bv1YOc-mSJRZ8ll57KVqLbCIbjKwerNX5r2_Qg2TwmJzQdRs-AQDhy-s_DlJd8ql6wR4n-kDZpar-pwIvz4fFIN0Fj57SXpAbLrV6Eo4Byzl0xFD8qEYEpBwjrMMfxCZXTlAVhAq6KCoGlDTwWuExps342-0UErEtyIqDnDGcrfNWiUsoo8j-29IpKd-w9-C388u-ChCxoHz--H8WmMSZzx3zTXsZ5lXLZ9IKfanDKg'
334334

335-
await jose.jwtVerify(jwt, publicKey, { // $ Alert
335+
await jose.jwtVerify(jwt, publicKey, { // $ Sink
336336
issuer: 'urn:example:issuer',
337337
audience: 'urn:example:audience',
338338
})
@@ -341,12 +341,12 @@
341341
(function () {
342342
const expressjwt = require("express-jwt");
343343

344-
var secretKey = "myHardCodedPrivateKey";
344+
var secretKey = "myHardCodedPrivateKey"; // $ Alert
345345

346346
app.get(
347347
"/protected",
348348
expressjwt.expressjwt({
349-
secret: secretKey, algorithms: ["HS256"] // $ Alert
349+
secret: secretKey, algorithms: ["HS256"] // $ Sink
350350
}),
351351
function (req, res) {
352352
if (!req.auth.admin) return res.sendStatus(401);
@@ -357,7 +357,7 @@
357357
app.get(
358358
"/protected",
359359
expressjwt.expressjwt({
360-
secret: Buffer.from(secretKey, "base64"), // $ Alert
360+
secret: Buffer.from(secretKey, "base64"), // $ Sink
361361
algorithms: ["RS256"],
362362
}),
363363
function (req, res) {
@@ -372,17 +372,17 @@
372372
const JwtStrategy = require('passport-jwt').Strategy;
373373
const passport = require('passport')
374374

375-
var secretKey = "myHardCodedPrivateKey";
375+
var secretKey = "myHardCodedPrivateKey"; // $ Alert
376376

377377
const opts = {}
378-
opts.secretOrKey = secretKey; // $ Alert
378+
opts.secretOrKey = secretKey; // $ Sink
379379
passport.use(new JwtStrategy(opts, function (jwt_payload, done) {
380380
return done(null, false);
381381
}));
382382

383383
passport.use(new JwtStrategy({
384384
secretOrKeyProvider: function (request, rawJwtToken, done) {
385-
return done(null, secretKey) // $ Alert
385+
return done(null, secretKey) // $ Sink
386386
}
387387
}, function (jwt_payload, done) {
388388
return done(null, false);
@@ -393,10 +393,10 @@
393393
import NextAuth from "next-auth"
394394
import AppleProvider from "next-auth/providers/apple"
395395

396-
var secretKey = "myHardCodedPrivateKey";
396+
var secretKey = "myHardCodedPrivateKey"; // $ Alert
397397

398398
NextAuth({
399-
secret: secretKey, // $ Alert
399+
secret: secretKey, // $ Sink
400400
providers: [
401401
AppleProvider({
402402
clientId: process.env.APPLE_ID,
@@ -411,7 +411,7 @@
411411
const jwt = require('koa-jwt');
412412
const app = new Koa();
413413

414-
var secretKey = "myHardCodedPrivateKey";
414+
var secretKey = "myHardCodedPrivateKey"; // $ Alert
415415

416-
app.use(jwt({ secret: secretKey })); // $ Alert
417-
})();
416+
app.use(jwt({ secret: secretKey })); // $ Sink
417+
})();

0 commit comments

Comments
 (0)