Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove getObjectExpressionProperty in favor of getProperty #4601

Merged
merged 8 commits into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/jsts/src/rules/S2598/rule.ts
Expand Up @@ -25,10 +25,10 @@ import * as estree from 'estree';
import {
getLhsVariable,
getValueOfExpression,
getObjectExpressionProperty,
getVariableFromName,
toEncodedMessage,
getFullyQualifiedName,
getProperty,
} from '../helpers';
import { SONAR_RUNTIME } from '../../linter/parameters';

Expand Down Expand Up @@ -115,8 +115,8 @@ function checkFormidable(context: Rule.RuleContext, callExpression: estree.CallE
if (options) {
report(
context,
!!getObjectExpressionProperty(options, UPLOAD_DIR),
keepExtensionsValue(getObjectExpressionProperty(options, KEEP_EXTENSIONS)?.value),
!!getProperty(options, UPLOAD_DIR, context),
keepExtensionsValue(getProperty(options, KEEP_EXTENSIONS, context)?.value),
callExpression,
);
}
Expand All @@ -136,7 +136,7 @@ function checkMulter(context: Rule.RuleContext, callExpression: estree.CallExpre
return;
}

const storagePropertyValue = getObjectExpressionProperty(multerOptions, STORAGE_OPTION)?.value;
const storagePropertyValue = getProperty(multerOptions, STORAGE_OPTION, context)?.value;
if (storagePropertyValue) {
const storageValue = getValueOfExpression(context, storagePropertyValue, 'CallExpression');

Expand All @@ -159,7 +159,7 @@ function getDiskStorageCalleeIfUnsafeStorage(
const { arguments: args, callee } = storageCreation;
if (args.length > 0 && isMemberWithProperty(callee, 'diskStorage')) {
const storageOptions = getValueOfExpression(context, args[0], 'ObjectExpression');
if (storageOptions && !getObjectExpressionProperty(storageOptions, DESTINATION_OPTION)) {
if (storageOptions && !getProperty(storageOptions, DESTINATION_OPTION, context)) {
return callee;
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/jsts/src/rules/S2755/rule.ts
Expand Up @@ -22,7 +22,7 @@
import { TSESTree } from '@typescript-eslint/utils';
import { Rule } from 'eslint';
import * as estree from 'estree';
import { getObjectExpressionProperty, toEncodedMessage, getFullyQualifiedName } from '../helpers';
import { getProperty, toEncodedMessage, getFullyQualifiedName } from '../helpers';
import { SONAR_RUNTIME } from '../../linter/parameters';

const XML_LIBRARY = 'libxmljs';
Expand Down Expand Up @@ -51,7 +51,7 @@ export const rule: Rule.RuleModule = {
CallExpression: (node: estree.Node) => {
const call = node as estree.CallExpression;
if (isXmlParserCall(call)) {
const noent = getObjectExpressionProperty(call.arguments[1], 'noent');
const noent = getProperty(call.arguments[1], 'noent', context);
if (noent && isNoEntSet(noent)) {
context.report({
message: toEncodedMessage('Disable access to external entities in XML parsing.', [
Expand Down
11 changes: 4 additions & 7 deletions packages/jsts/src/rules/S4423/rule.lib.ts
Expand Up @@ -21,11 +21,7 @@

import { Rule } from 'eslint';
import * as estree from 'estree';
import {
getObjectExpressionProperty,
getValueOfExpression,
getFullyQualifiedName,
} from '../helpers';
import { getProperty, getValueOfExpression, getFullyQualifiedName } from '../helpers';

const SECURE_PROTOCOL_ALLOWED_VALUES = [
'TLSv1_2_method',
Expand All @@ -51,7 +47,8 @@ export const rule: Rule.RuleModule = {
objectExpression: estree.ObjectExpression | undefined,
propertyName: string,
) {
const unsafeProperty = getObjectExpressionProperty(objectExpression, propertyName);
const unsafeProperty =
objectExpression && getProperty(objectExpression, propertyName, context);
if (unsafeProperty) {
return getValueOfExpression(context, unsafeProperty.value, 'Literal');
}
Expand Down Expand Up @@ -89,7 +86,7 @@ export const rule: Rule.RuleModule = {
});
}

const secureOptions = getObjectExpressionProperty(options, 'secureOptions');
const secureOptions = getProperty(options, 'secureOptions', context);
if (secureOptions && !isValidSecureOptions(secureOptions.value)) {
context.report({
node: secureOptions,
Expand Down
8 changes: 4 additions & 4 deletions packages/jsts/src/rules/S4426/rule.ts
Expand Up @@ -21,7 +21,7 @@

import { Rule } from 'eslint';
import * as estree from 'estree';
import { isIdentifier, getValueOfExpression, getObjectExpressionProperty } from '../helpers';
import { isIdentifier, getValueOfExpression, getProperty } from '../helpers';

const MINIMAL_MODULUS_LENGTH = 2048;
const MINIMAL_DIVISOR_LENGTH = 224;
Expand Down Expand Up @@ -72,7 +72,7 @@ export const rule: Rule.RuleModule = {
}

function checkRsaAndDsaOptions(algorithm: string, options: estree.Node) {
const modulusProperty = getObjectExpressionProperty(options, 'modulusLength');
const modulusProperty = getProperty(options, 'modulusLength', context);
const modulusLength = getNumericValue(modulusProperty?.value);
if (modulusProperty && modulusLength && modulusLength < MINIMAL_MODULUS_LENGTH) {
context.report({
Expand All @@ -84,7 +84,7 @@ export const rule: Rule.RuleModule = {
},
});
}
const divisorProperty = getObjectExpressionProperty(options, 'divisorLength');
const divisorProperty = getProperty(options, 'divisorLength', context);
const divisorLength = getNumericValue(divisorProperty?.value);
if (divisorProperty && divisorLength && divisorLength < MINIMAL_DIVISOR_LENGTH) {
context.report({
Expand All @@ -99,7 +99,7 @@ export const rule: Rule.RuleModule = {
}

function checkEcCurve(options: estree.Node) {
const namedCurveProperty = getObjectExpressionProperty(options, 'namedCurve');
const namedCurveProperty = getProperty(options, 'namedCurve', context);
const namedCurve = getValueOfExpression(
context,
namedCurveProperty?.value,
Expand Down
4 changes: 2 additions & 2 deletions packages/jsts/src/rules/S4502/rule.ts
Expand Up @@ -25,11 +25,11 @@ import * as estree from 'estree';
import {
isIdentifier,
isLiteral,
getObjectExpressionProperty,
flattenArgs,
toEncodedMessage,
getFullyQualifiedName,
isRequireModule,
getProperty,
} from '../helpers';
import { SONAR_RUNTIME } from '../../linter/parameters';

Expand Down Expand Up @@ -83,7 +83,7 @@ export const rule: Rule.RuleModule = {
// csurf(...)
if (getFullyQualifiedName(context, callee) === CSURF_MODULE) {
const [args] = callExpression.arguments;
const ignoredMethods = getObjectExpressionProperty(args, 'ignoreMethods');
const ignoredMethods = getProperty(args, 'ignoreMethods', context);
if (ignoredMethods) {
checkIgnoredMethods(ignoredMethods);
}
Expand Down
11 changes: 6 additions & 5 deletions packages/jsts/src/rules/S5122/rule.ts
Expand Up @@ -23,9 +23,9 @@ import { Rule } from 'eslint';
import * as estree from 'estree';
import {
getUniqueWriteUsage,
getObjectExpressionProperty,
toEncodedMessage,
getFullyQualifiedName,
getProperty,
} from '../helpers';
import { TSESTree } from '@typescript-eslint/utils';
import { SONAR_RUNTIME } from '../../linter/parameters';
Expand Down Expand Up @@ -63,13 +63,13 @@ export const rule: Rule.RuleModule = {
return;
}
const [arg] = call.arguments;
let sensitiveCorsProperty = getSensitiveCorsProperty(arg);
let sensitiveCorsProperty = getSensitiveCorsProperty(arg, context);
if (sensitiveCorsProperty) {
report(sensitiveCorsProperty);
}
if (arg?.type === 'Identifier') {
const usage = getUniqueWriteUsage(context, arg.name);
sensitiveCorsProperty = getSensitiveCorsProperty(usage);
sensitiveCorsProperty = getSensitiveCorsProperty(usage, context);
if (sensitiveCorsProperty) {
report(sensitiveCorsProperty, arg);
}
Expand All @@ -82,7 +82,7 @@ export const rule: Rule.RuleModule = {
},

ObjectExpression(node: estree.Node) {
const objProperty = getObjectExpressionProperty(node, CORS_HEADER);
const objProperty = getProperty(node, CORS_HEADER, context);
if (objProperty && isAnyDomain(objProperty.value)) {
report(objProperty);
}
Expand All @@ -103,8 +103,9 @@ function isAnyDomain(node: estree.Node) {

function getSensitiveCorsProperty(
node: estree.Node | undefined | null,
context: Rule.RuleContext,
): estree.Property | undefined {
const originProperty = getObjectExpressionProperty(node, 'origin');
const originProperty = getProperty(node, 'origin', context);
if (originProperty && isAnyDomain(originProperty.value)) {
return originProperty;
}
Expand Down
21 changes: 8 additions & 13 deletions packages/jsts/src/rules/S5332/rule.lib.ts
Expand Up @@ -22,12 +22,7 @@
import { Rule } from 'eslint';
import * as estree from 'estree';
import { URL } from 'url';
import {
getValueOfExpression,
getObjectExpressionProperty,
getParent,
getFullyQualifiedName,
} from '../helpers';
import { getValueOfExpression, getParent, getFullyQualifiedName, getProperty } from '../helpers';
import { normalizeFQN } from '../helpers/aws/cdk';

const INSECURE_PROTOCOLS = ['http://', 'ftp://', 'telnet://'];
Expand Down Expand Up @@ -65,22 +60,22 @@ export const rule: Rule.RuleModule = {

const firstArgValue = getValueOfExpression(context, firstArg, 'ObjectExpression');

const ses = getObjectExpressionProperty(firstArgValue, 'SES');
const ses = getProperty(firstArgValue, 'SES', context);
if (ses && usesSesCommunication(ses)) {
return;
}

const secure = getObjectExpressionProperty(firstArgValue, 'secure');
const secure = getProperty(firstArgValue, 'secure', context);
if (secure && (secure.value.type !== 'Literal' || secure.value.raw !== 'false')) {
return;
}

const requireTls = getObjectExpressionProperty(firstArgValue, 'requireTLS');
const requireTls = getProperty(firstArgValue, 'requireTLS', context);
if (requireTls && (requireTls.value.type !== 'Literal' || requireTls.value.raw !== 'false')) {
return;
}

const port = getObjectExpressionProperty(firstArgValue, 'port');
const port = getProperty(firstArgValue, 'port', context);
if (port && (port.value.type !== 'Literal' || port.value.raw === '465')) {
return;
}
Expand All @@ -96,14 +91,14 @@ export const rule: Rule.RuleModule = {

const ses = getValueOfExpression(
context,
getObjectExpressionProperty(configuration, 'ses')?.value,
getProperty(configuration, 'ses', context)?.value,
'NewExpression',
);
if (!ses || normalizeFQN(getFullyQualifiedName(context, ses)) !== '@aws_sdk.client_ses.SES') {
return false;
}

const aws = getObjectExpressionProperty(configuration, 'aws');
const aws = getProperty(configuration, 'aws', context);
if (
!aws ||
normalizeFQN(getFullyQualifiedName(context, aws.value)) !== '@aws_sdk.client_ses'
Expand Down Expand Up @@ -131,7 +126,7 @@ export const rule: Rule.RuleModule = {
return;
}
const firstArgValue = getValueOfExpression(context, firstArg, 'ObjectExpression');
const secure = getObjectExpressionProperty(firstArgValue, 'secure');
const secure = getProperty(firstArgValue, 'secure', context);
if (secure && secure.value.type === 'Literal' && secure.value.raw === 'false') {
context.report({
node: callExpression.callee,
Expand Down
5 changes: 3 additions & 2 deletions packages/jsts/src/rules/S5527/rule.ts
Expand Up @@ -24,9 +24,9 @@ import * as estree from 'estree';
import {
getValueOfExpression,
getPropertyWithValue,
getObjectExpressionProperty,
toEncodedMessage,
getFullyQualifiedName,
getProperty,
} from '../helpers';
import { SONAR_RUNTIME } from '../../linter/parameters';
import { childrenOf } from '../../linter';
Expand Down Expand Up @@ -73,9 +73,10 @@ export const rule: Rule.RuleModule = {
secondaryMessages.push(SECONDARY_MESSAGE);
shouldReport = true;
}
const checkServerIdentityProperty = getObjectExpressionProperty(
const checkServerIdentityProperty = getProperty(
argumentValue,
'checkServerIdentity',
context,
);
if (
checkServerIdentityProperty &&
Expand Down
4 changes: 2 additions & 2 deletions packages/jsts/src/rules/S5659/rule.ts
Expand Up @@ -24,10 +24,10 @@ import * as estree from 'estree';
import {
getPropertyWithValue,
getValueOfExpression,
getObjectExpressionProperty,
toEncodedMessage,
isNullLiteral,
getFullyQualifiedName,
getProperty,
} from '../helpers';
import { SONAR_RUNTIME } from '../../linter/parameters';

Expand Down Expand Up @@ -76,7 +76,7 @@ export const rule: Rule.RuleModule = {
thirdArgumentValue: estree.ObjectExpression,
secondaryLocations: estree.Node[],
) {
const algorithmsProperty = getObjectExpressionProperty(thirdArgumentValue, 'algorithms');
const algorithmsProperty = getProperty(thirdArgumentValue, 'algorithms', context);
if (!algorithmsProperty) {
if (isNullLiteral(publicKey)) {
raiseIssueOn(callExpression.callee, VERIFY_MESSAGE, secondaryLocations);
Expand Down
8 changes: 2 additions & 6 deletions packages/jsts/src/rules/S5691/rule.ts
Expand Up @@ -21,11 +21,7 @@

import { Rule } from 'eslint';
import * as estree from 'estree';
import {
getUniqueWriteUsage,
getObjectExpressionProperty,
getFullyQualifiedName,
} from '../helpers';
import { getUniqueWriteUsage, getFullyQualifiedName, getProperty } from '../helpers';

const SERVE_STATIC = 'serve-static';

Expand All @@ -46,7 +42,7 @@ export const rule: Rule.RuleModule = {
options = getUniqueWriteUsage(context, options.name);
}

const dotfilesProperty = getObjectExpressionProperty(options, 'dotfiles');
const dotfilesProperty = getProperty(options, 'dotfiles', context);
if (
dotfilesProperty?.value.type === 'Literal' &&
dotfilesProperty.value.value === 'allow'
Expand Down
12 changes: 6 additions & 6 deletions packages/jsts/src/rules/S5693/rule.ts
Expand Up @@ -26,8 +26,8 @@ import { parse } from 'bytes';
import {
getLhsVariable,
getValueOfExpression,
getObjectExpressionProperty,
getFullyQualifiedName,
getProperty,
} from '../helpers';

const FORMIDABLE_MODULE = 'formidable';
Expand Down Expand Up @@ -116,7 +116,7 @@ function checkFormidable(context: Rule.RuleContext, callExpression: estree.CallE

const options = getValueOfExpression(context, callExpression.arguments[0], 'ObjectExpression');
if (options) {
const property = getObjectExpressionProperty(options, MAX_FILE_SIZE);
const property = getProperty(options, MAX_FILE_SIZE, context);
checkSize(context, callExpression, property, FORMIDABLE_DEFAULT_SIZE);
}
}
Expand All @@ -136,9 +136,9 @@ function checkMulter(context: Rule.RuleContext, callExpression: estree.CallExpre
return;
}

const limitsPropertyValue = getObjectExpressionProperty(multerOptions, LIMITS_OPTION)?.value;
const limitsPropertyValue = getProperty(multerOptions, LIMITS_OPTION, context)?.value;
if (limitsPropertyValue && limitsPropertyValue.type === 'ObjectExpression') {
const fileSizeProperty = getObjectExpressionProperty(limitsPropertyValue, FILE_SIZE_OPTION);
const fileSizeProperty = getProperty(limitsPropertyValue, FILE_SIZE_OPTION, context);
checkSize(context, callExpression, fileSizeProperty);
}

Expand All @@ -158,14 +158,14 @@ function checkBodyParser(context: Rule.RuleContext, callExpression: estree.CallE
return;
}

const limitsProperty = getObjectExpressionProperty(options, LIMITS_OPTION);
const limitsProperty = getProperty(options, LIMITS_OPTION, context);
checkSize(context, callExpression, limitsProperty, BODY_PARSER_DEFAULT_SIZE, true);
}

function checkSize(
context: Rule.RuleContext,
callExpr: estree.CallExpression,
property?: estree.Property,
property?: estree.Property | null,
defaultLimit?: number,
useStandardSizeLimit = false,
) {
Expand Down