UNPKG

eslint-plugin-sonarjs

Version:
164 lines (163 loc) 6.71 kB
"use strict"; /* * SonarQube JavaScript Plugin * Copyright (C) 2011-2025 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the Sonar Source-Available License for more details. * * You should have received a copy of the Sonar Source-Available License * along with this program; if not, see https://sonarsource.com/license/ssal/ */ // https://sonarsource.github.io/rspec/#/rspec/S5689/javascript var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.rule = void 0; const index_js_1 = require("../helpers/index.js"); const meta = __importStar(require("./generated-meta.js")); const HELMET = 'helmet'; const HIDE_POWERED_BY = 'hide-powered-by'; const HEADER_X_POWERED_BY = 'X-Powered-By'.toLowerCase(); const PROTECTING_MIDDLEWARES = [HELMET, HIDE_POWERED_BY]; /** Expected number of arguments in `app.set`. */ const APP_SET_NUM_ARGS = 2; exports.rule = { meta: (0, index_js_1.generateMeta)(meta, { messages: { headerSet: 'Make sure disclosing the fingerprinting of this web technology is safe here.', headerDefault: 'This framework implicitly discloses version information by default. Make sure it is safe here.', }, }), create(context) { let appInstantiation = null; let isSafe = false; let isExplicitelyUnsafe = false; return { Program() { appInstantiation = null; isSafe = false; isExplicitelyUnsafe = true; }, CallExpression: (node) => { if (!isSafe && appInstantiation) { const callExpr = node; isSafe = index_js_1.Express.isUsingMiddleware(context, callExpr, appInstantiation, isProtecting(context)) || isDisabledXPoweredBy(callExpr, appInstantiation) || isSetFalseXPoweredBy(callExpr, appInstantiation) || isAppEscaping(callExpr, appInstantiation); isExplicitelyUnsafe = isSetTrueXPoweredBy(callExpr, appInstantiation); } }, VariableDeclarator: (node) => { if (!isSafe && !appInstantiation) { const varDecl = node; const app = index_js_1.Express.attemptFindAppInstantiation(varDecl, context); if (app) { appInstantiation = app; } } }, ReturnStatement: (node) => { if (!isSafe && appInstantiation) { const ret = node; isSafe = isAppEscapingThroughReturn(ret, appInstantiation); } }, 'Program:exit'() { if (!isSafe && appInstantiation) { let messageId = 'headerDefault'; if (isExplicitelyUnsafe) { messageId = 'headerSet'; } context.report({ node: appInstantiation, messageId, }); } }, }; }, }; /** * Checks whether node looks like `helmet.hidePoweredBy()`. */ function isHidePoweredByFromHelmet(context, n) { if (n.type === 'CallExpression') { return (0, index_js_1.getFullyQualifiedName)(context, n) === `${HELMET}.hidePoweredBy`; } return false; } function isProtecting(context) { return (n) => index_js_1.Express.isMiddlewareInstance(context, PROTECTING_MIDDLEWARES, n) || isHidePoweredByFromHelmet(context, n); } function isDisabledXPoweredBy(callExpression, app) { if ((0, index_js_1.isMethodInvocation)(callExpression, app.name, 'disable', 1)) { const arg0 = callExpression.arguments[0]; return arg0.type === 'Literal' && String(arg0.value).toLowerCase() === HEADER_X_POWERED_BY; } return false; } function isSetFalseXPoweredBy(callExpression, app) { return getSetTrueXPoweredByValue(callExpression, app) === false; } function isSetTrueXPoweredBy(callExpression, app) { return getSetTrueXPoweredByValue(callExpression, app) === true; } function getSetTrueXPoweredByValue(callExpression, app) { if ((0, index_js_1.isMethodInvocation)(callExpression, app.name, 'set', APP_SET_NUM_ARGS)) { const [headerName, onOff] = callExpression.arguments; if (headerName.type === 'Literal' && String(headerName.value).toLowerCase() === HEADER_X_POWERED_BY && onOff.type === 'Literal') { return onOff.value; } } return undefined; } function isAppEscaping(callExpr, app) { return Boolean(callExpr.arguments.find(arg => arg.type === 'Identifier' && arg.name === app.name)); } function isAppEscapingThroughReturn(ret, app) { const arg = ret.argument; return Boolean(arg && arg.type === 'Identifier' && arg.name === app.name); }