GithubHelp home page GithubHelp logo

eslint-plugin-sdl's Introduction

eslint-plugin-sdl

Node CI E2E integration

ESLint Plugin focused on common security issues and misconfigurations.

Plugin is intended as a baseline for projects that follow Microsoft Security Development Lifecycle (SDL) and use ESLint to perform Static Analysis Security Testing (SAST).

Installation

npm install microsoft/eslint-plugin-sdl

or

yarn add microsoft/eslint-plugin-sdl

Usage

When you run npm install within your project's root folder, the plugin will be added automatically to your package.json and package-lock.json files. You can also add the plugin to your package.json file manually by specifying the name and version number in the dependencies section like so:

"dependencies": {
    "@microsoft/eslint-plugin-sdl": "^0.1.9"
}

Run npm install within your root folder to install everything listed in the dependencies section of package.json. If the plugin is listed in your package.json dependencies, eslint will enforce all plugin rules using default settings.

Configs

Including an eslint configuration file in your project allows you to customize how eslint applies rules to your project. If you are using an .eslintrc file, you can include the plugin by adding:

plugins: ["@microsoft/eslint-plugin-sdl"]

Eslint will then only enforce rules you specify in the rules section of your .eslintrc file at the severity level you designate. The severity level options are 0 (no error), 1 (warning), and 2 (error). For example:

rules: {
  "no-eval": 2,
  "@microsoft/sdl/no-inner-html": 2
}

You can also used the below Shareable config files as guidelines depending on the type of project.

Plugin is shipped with following Shareable Configs:

  • angular - Set of rules for Angular applications
  • angularjs - Set of rules for AngularJS applications
  • common - Set of rules for common JavaScript applications
  • electron - Set of rules for Electron applications
  • node - Set of rules for Node applications
  • react - Set of rules for ReactJS applications
  • recommended - SDL Recommended rules for all applications
  • required - SDL Required rules for all applications
  • typescript - Set of rules for TypeScript applications

Rules

Where possible, we leverage existing rules from ESLint and community plugins such as react, typescript-eslint or security.

We also implemented several custom rules where we did not find sufficient alternative in the community.

Name Description
no-caller Bans usage of deprecated functions arguments.caller() and arguments.callee that could potentially allow access to call stack.
no-delete-var Bans usage of operator delete on variables as it can lead to unexpected behavior.
no-eval Bans usage of eval() that allows code execution from string argument.
no-implied-eval Bans usage of setTimeout(), setInterval() and execScript(). These functions are similar to eval() and prone to code execution.
no-new-func Bans calling new Function() as it's similar to eval() and prone to code execution.
node/no-deprecated-api Bans usage of deprecated APIs in Node.
@microsoft/sdl/no-angular-bypass-sanitizer Calls to bypassSecurityTrustHtml, bypassSecurityTrustScript and similar methods bypass DomSanitizer in Angular and need to be reviewed.
@microsoft/sdl/no-angularjs-bypass-sce Calls to $sceProvider.enabled(false), $sceDelegate.trustAs(), $sce.trustAs() and relevant shorthand methods (e.g. trustAsHtml or trustAsJs) bypass Strict Contextual Escaping (SCE) in AngularJS and need to be reviewed.
@microsoft/sdl/no-angularjs-enable-svg Calls to $sanitizeProvider.enableSvg(true) increase attack surface of the application by enabling SVG support in AngularJS sanitizer and need to be reviewed.
@microsoft/sdl/no-angularjs-sanitization-whitelist Calls to $compileProvider.aHrefSanitizationWhitelist or $compileProvider.imgSrcSanitizationWhitelist configure whitelists in AngularJS sanitizer and need to be reviewed.
@microsoft/sdl/no-cookies HTTP cookies are an old client-side storage mechanism with inherent risks and limitations. Use Web Storage, IndexedDB or other modern methods instead.
@microsoft/sdl/no-document-domain Writes to document.domain property must be reviewed to avoid bypass of same-origin checks. Usage of top level domains such as azurewebsites.net is strictly prohibited.
@microsoft/sdl/no-document-write Calls to document.write or document.writeln manipulate DOM directly without any sanitization and should be avoided. Use document.createElement() or similar methods instead.
@microsoft/sdl/no-electron-node-integration Node.js Integration must not be enabled in any renderer that loads remote content to avoid remote code execution attacks.
@microsoft/sdl/no-html-method Direct calls to method html() often (e.g. in jQuery framework) manipulate DOM without any sanitization and should be avoided. Use document.createElement() or similar methods instead.
@microsoft/sdl/no-inner-html Assignments to innerHTML or outerHTML properties manipulate DOM directly without any sanitization and should be avoided. Use document.createElement() or similar methods instead.
@microsoft/sdl/no-insecure-url Insecure protocols such as HTTP or FTP should be replaced by their encrypted counterparts (HTTPS, FTPS) to avoid sending potentially sensitive data over untrusted networks in plaintext.
@microsoft/sdl/no-msapp-exec-unsafe Calls to MSApp.execUnsafeLocalFunction() bypass script injection validation and should be avoided.
@microsoft/sdl/no-postmessage-star-origin Always provide specific target origin, not * when sending data to other windows using postMessage to avoid data leakage outside of trust boundary.
@microsoft/sdl/no-unsafe-alloc When calling Buffer.allocUnsafe and Buffer.allocUnsafeSlow, the allocated memory is not wiped-out and can contain old, potentially sensitive data.
@microsoft/sdl/no-winjs-html-unsafe Calls to WinJS.Utilities.setInnerHTMLUnsafe() and similar methods do not perform any input validation and should be avoided. Use WinJS.Utilities.setInnerHTML() instead.
@microsoft/sdl/react-iframe-missing-sandbox The sandbox attribute enables an extra set of restrictions for the content in the iframe and should always be specified.
react/no-danger Bans usage of dangerouslySetInnerHTML property in React as it allows passing unsanitized HTML in DOM.
@typescript-eslint/no-implied-eval Similar to built-in ESLint rule no-implied-eval. Bans usage of setTimeout(), setInterval(), setImmediate(), execScript() or new Function() as they are similar to eval() and allow code execution from string arguments.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

eslint-plugin-sdl's People

Contributors

a-katopodis avatar jason-ha avatar kptil avatar mozfreddyb avatar moztomer avatar roddolf avatar syphe avatar taurheim avatar tido64 avatar tosmolka avatar vflouirac avatar youssef1313 avatar zl1n avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eslint-plugin-sdl's Issues

Update eslint-plugin-react to at least 7.27.0 to support ESLint 8

Describe the bug
When installing eslint-plugin-sdl and eslint ^8.0.0 is present in package.json you'll see the below warning

warning "@vue-storefront/eslint-config > @microsoft/eslint-plugin-sdl > [email protected]" has incorrect peer dependency "eslint@^3 || ^4 || ^5 || ^6 || ^7".

This package has a hardcoded dependency on eslint-plugin-react 7.24.0
https://github.com/microsoft/eslint-plugin-sdl/blob/main/package.json#L25

7.24.0 of eslint-plugin-react requires eslint as a peerDependency, but eslint v8 is not included in the supported version range
7.27.0 enables support for ESLint v8

Please update

To Reproduce
Steps to reproduce the behavior:

  1. Run yarn add eslint@^8.0.0 then yarn add eslint-plugin-sdl
  2. See warning about wrong peerDependency version

Expected behavior
Warning about wrong eslint version should not appear.

use of recommended config requires tsconfig coverage of all files

When plugin:@microsoft/sdl/recommended is added to eslint configuration errors are generated for all files not listed in tsconfig.json file - not just typescript files.

Example error:

  0:0  error  Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: packages\foo\gulpfile.js.
The file must be included in at least one of the projects provided

Expected behavior
@typescript-eslint/parser is only applied to **/*.{ts,tsx} files as listed in the overrides and thus no Parsing errors.

no-inner-html does not work for extended Typescript HTMLElement interfaces

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Use typescript & eslint-plugin-sdl
  2. Write code such as
const el: HTMLDivElement;
el.innerHTML = '';
  1. Not marked as error in eslint

Expected behavior
Error should show for all HTML(type)Element

Additional context
Example extended HTMLElement type: https://github.com/microsoft/TypeScript/blob/main/lib/lib.dom.d.ts#L6353

Implement rule no-weak-crypto

We typically mandate plenty of crypto requirements, we need a rule that will be aligned and ban certain modules, functions, calls with unwanted arguments and so on.

This area is quite complex, let's focus on getting initial rule out and then plan a follow-up task.

Candidates for detection:

  • Weak hashing algorithms (MD2, MD4, MD5, SHA1, ...).
  • Weak encryption (DES, 3DES, RC4, ...)
  • Weak SSL protocol (SSLv2, SSLv23, SSLv3, ...)
  • RSA with < 2048 bit keys
  • ECDSA with < 256b keys
  • Weak cipher modes (CBC, OFB, CFB, ...)
  • Hard-coded IV
  • Empty/null encryption key?
  • Disabling SSL certificate validation

Please create a new release

Is your feature request related to a problem? Please describe.
Firefox is currently on a fork of this package.

Describe the solution you'd like
With #39 merged, it would be great for us if this package would create a new release

Describe alternatives you've considered
Of course, we could always stay on our fork.... ๐Ÿ™„

no-insecure-random fails when ESLint is not in the same directory.

Describe the bug
The rule no-insecure-random is requiring ESLint.
const eslint = require("eslint/lib/eslint");
If ESLint is installed in a different directory from the plugin this will lead to an error.

To Reproduce
Steps to reproduce the behavior:

  1. Create a code with insecure random algorithm. E.x. crypo.pseudoRandomBytes
  2. Install ESLint in a different directory from the plugins.
  3. Add the resolve-plugin-relative-to command to load those plugins.

Expected behavior
The rule should run properly without any crashes and not requiring ESLint.

Add react/jsx-no-target-blank to config/react.js

Is your feature request related to a problem? Please describe.
When scanning React apps, I would like to be warned about external links or forms without noreferrer or noopener in their rel attribute. This is quite common security requirement (see https://web.dev/external-anchors-use-rel-noopener/ and other public sources).

Describe the solution you'd like
I think react/jsx-no-target-blank does a good job of detecting most common scenarios, I would suggest adding it to SDL Required ruleset for React apps (./config/react.js).

I would also consider rule config that is slightly more strict that the defaults, such as:

{
  allowReferrer: false,
  enforceDynamicLinks: 'always',
  warnOnSpreadAttributes: true,
  links: true,
  forms: true
}

Describe alternatives you've considered
I don't think it makes sense to implement and maintain custom rule in this case.

Additional context
This check was implemented in tslint via react-anchor-blank-noopener but for some reason was omitted during migration of SDL rules to eslint.

[no-insecure-url] Autofix that replaces HTTP with HTTPS in literals and templateliterals

Is your feature request related to a problem? Please describe.
We at Mozilla would like to make more use of the no-insecure-url rule of this plugin.. For this to work best, we'd prefer if the rule had the --fix functionality.

Describe the solution you'd like
We actually have a prototype patch that works for Literals and would be easy to adapt to TemplateLiterals and (obviously) takes the exceptions and blocklists into account.

Describe alternatives you've considered
N/A

Additional context
Not sure, but feel free to ask. We'd really like to make this work.

http://localhost should be allowed by @microsoft/sdl/no-insecure-url

http://localhost is common for some testing scenarios and situations where communication does not leave the OS are not a risk.

To Reproduce
Steps to reproduce the behavior:

  1. Add 'http://localhost' use in code.
  2. Enable recommended configuration.
  3. Run eslint
  4. See report similar to:
Error @microsoft/sdl/no-insecure-url: Insecure protocols such as [HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) or [FTP](https://en.wikipedia.org/wiki/File_Transfer_Protocol) should be replaced by their encrypted counterparts ([HTTPS](https://en.wikipedia.org/wiki/HTTPS), [FTPS](https://en.wikipedia.org/wiki/FTPS)) to avoid sending (potentially sensitive) data over untrusted network in plaintext.: Do not use insecure URLs

Related
See tslint no-http issue where localhost was resolved to be allowed.

Expected behavior
http://localhost should be allowed.

Rule "react/jsx-no-target-blank" is using out-of-date version of dependency "eslint-plugin-react"

Describe the bug

ESLint validation for rule "react/jsx-no-target-blank" is always invalid with error "Schema error: should NOT have additional properties" because new configuration properties are not aligned with "eslint-plugin-react"

The new configuration:

"react/jsx-no-target-blank": ["error",
{
allowReferrer: false,
enforceDynamicLinks: 'always',
warnOnSpreadAttributes: true,
links: true,
forms: true
}
]

It has been updated in an enhancement PR #26

CC @A-Katopodis @Vflouirac

But version "7.24.0" of "eslint-plugin-react" in the PR above only has 3 properties:

Screen Shot 2022-03-01 at 18 11 55

To Reproduce
Steps to reproduce the behavior:

  1. Open any Javascript or Typescript project using ESLint with Webstorm IDE
  2. Install dependency "@microsoft/eslint-plugin-sdl"
  3. Add "plugin:@microsoft/sdl/react" to "extends" of ".eslintrc.js" or ".eslintrc.json"
  4. Open any Javascript or Typescript file and see error "Schema error: should NOT have additional properties"

Expected behavior
ESLint is executed without any error.

Screenshots
Screen Shot 2022-03-01 at 18 08 08

FYI: typescript-eslint@v6 will remove parserServices.hasFullTypeInformation

๐Ÿ‘‹ Hi! I'm one of the maintainers of typescript-eslint. We're getting ready to release a new v6 major version of the tooling, which includes some small breaking changes.

Coming over from typescript-eslint/typescript-eslint#7124: I see this repo contains references to hasFullTypeInformation. That property is something we've never recommended using (see that issue & typescript-eslint/typescript-eslint#7158). We recommend instead using ESLintUtils.getParserServices per https://typescript-eslint.io/developers/custom-rules#typed-rules.

If you really want to "progressively enhance" lint rules, parserServices.program will be null if type information isn't available. But we definitely recommend against that kind of rule behavior changing.

Overall, I'd suggest trying out typescript-eslint@v6 and making sure internal builds & downstream repos still compile & lint nicely with all the changes. Let me know if there's anything we can do to help with the v6 upgrade! ๐Ÿ’–

Prepare custom rules for ESLint v9

Describe the bug

The upcoming major release of ESLint includes several breaking changes for rule authors: https://eslint.org/blog/2023/09/preparing-custom-rules-eslint-v9/

To Reproduce

n/a

Expected behavior

n/a

Screenshots

n/a

Desktop (please complete the following information):

  • OS: any
  • Browser: any
  • Version: any

Smartphone (please complete the following information):

  • Device: any
  • OS: any
  • Browser: any
  • Version: any

Additional context

Intro to ESLint's new config format: https://eslint.org/blog/2022/08/new-config-system-part-2/

Migrate from @microsoft/sdl/react-iframe-missing-sandbox to react/iframe-missing-sandbox

Is your feature request related to a problem? Please describe.
[email protected] now includes rule react/iframe-missing-sandbox which is slightly improved version of @microsoft/sdl/react-iframe-missing-sandbox.

Unless there is a good reason to keep maintaining two versions of essentially identical rule I would suggest to deprecate @microsoft/sdl/react-iframe-missing-sandbox in favor of react/iframe-missing-sandbox.

Describe the solution you'd like

  • Update config/react.js and switch from @microsoft/sdl/react-iframe-missing-sandbox to react/iframe-missing-sandbox
  • Update version of eslint-plugin-react in package.json
  • This is potentially breaking change, consider increasing minor package version, releasing in multiple stages, publishing release notes, etc.

Describe alternatives you've considered
Backport changes from react/iframe-missing-sandbox to @microsoft/sdl/react-iframe-missing-sandbox and continue maintaining two versions

Consider migrating from `eslint-plugin-node` to `eslint-plugin-n`

Describe the bug

Consider migrating from eslint-plugin-node to eslint-plugin-n as the former is no longer maintained: mysticatea/eslint-plugin-node#300

To Reproduce

n/a

Expected behavior

n/a

Screenshots

n/a

Desktop (please complete the following information):

  • OS: any
  • Browser: any
  • Version: any

Smartphone (please complete the following information):

  • Device: any
  • OS: any
  • Browser: any
  • Version: any

Additional context

For additional context, see mysticatea/eslint-plugin-node#300

xmlns attributes flagging no-insecure-url

We're integrating sdl/recommended to our repo and we're seeing false flags on our SVGs due to the xmlns attribute being set to a valid http:// based namespace. We're working around this currently but are there plans to fix this?

Setup example

For those not familiar with plugin setup and eslint config setup, could you please write in the readme how to configure eslint to be able to use your plugin? Some lines with the settings (I assume it isn't much) would be sufficient and for some people (like me), very helpful.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.