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

Pull in the latest safety-web development #4

Merged
merged 33 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8f46c03
Add a base skeleton for the plugin
neuracr Jul 2, 2024
245e69e
Set up Mocha with a placeholder test
neuracr Jul 3, 2024
5e93a0b
Remove tests from tsconfig and add yarn clean script
neuracr Jul 3, 2024
bb4ba4c
Add Copyright header to js files
neuracr Jul 3, 2024
880d07c
Add a notice that safety-web is under development
neuracr Jul 4, 2024
0037776
Sets the rule tester up
neuracr Jul 4, 2024
0cc2d3f
Add a script to update a vendored version of tsetse and add the curre…
neuracr Jul 5, 2024
ef0c259
Track the last run of the tsetse_update.sh script and the latest comm…
neuracr Jul 5, 2024
ba5fd67
Add readme instructions for updating tsetse
neuracr Jul 5, 2024
2d0362a
Add dependencies for tsetse
neuracr Jul 5, 2024
e49f7f5
Wire the tsetse checks, update the tests and test fixtures
neuracr Jul 5, 2024
0ef5c49
Add a test project as an integration test
neuracr Jul 8, 2024
f0a6559
Add a test helper to check for expected violations
neuracr Jul 8, 2024
afca3ca
Add a JavaScript test project as an integration test
neuracr Jul 9, 2024
c2119c5
Set ESLint up for safety-web sources
neuracr Jul 9, 2024
57b5a9c
Fix the lint issues in safety-web
neuracr Jul 10, 2024
13d4134
Fix the main entry point path
neuracr Jul 10, 2024
50653be
Only add compiled JS to the NPM release
neuracr Jul 11, 2024
a1b1d19
Improve the violation expectation helper
neuracr Jul 9, 2024
c0bf583
Add a typescript integration test set up with ESLint8
neuracr Jul 11, 2024
3aeb2c4
Fix the basic-typescript-eslint9 tests
neuracr Jul 11, 2024
4b85dba
Define common script for all packages to be able to use `yarn workspa…
neuracr Jul 12, 2024
a7bb661
Add a test:watch
neuracr Jul 12, 2024
654446f
Wire tsetse violations to ESLint message ids.
neuracr Jul 12, 2024
a79b037
Add commands to update the violation golden files
neuracr Jul 12, 2024
e661633
Clean the javascript eslint 9 test config up
neuracr Jul 15, 2024
a507056
Mark the test helper binary as executable once built
neuracr Jul 15, 2024
304a8b7
Add a integration for javascript running on ESLint8
neuracr Jul 15, 2024
6d87de7
Add a build:watch script to auto-rebuild safety-web on changes
neuracr Jul 15, 2024
fe28c64
Check that we're actually using eslint8 in integration test
neuracr Jul 15, 2024
6406441
Specify the yarn version to use with corepack
neuracr Jul 23, 2024
bfc0b81
Upgrade yarn to the latest version (Berry 4.3.1)
neuracr Jul 24, 2024
50e685c
Fix the project after Yarn was upgraded.
neuracr Jul 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
!.yarn/sdks
!.yarn/versions
node_modules
safety-web/lib/*
test-helpers/expect-violations/bin/*
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodeLinker: node-modules
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,43 @@

**This is not an officially supported Google product.**

**This project is under development and is not ready for production yet.**

safety-web is an ESLint plugin that works on TypeScript and JavaScript projects and surfaces security issues like Trusted Types violations statically.

## Development

This project uses yarn "modern" Berry (Yarn 4) with workspaces. To install the dependencies for all [workspaces](https://yarnpkg.com/features/workspaces):

```bash
yarn
```

The commands `clean`, `build`, `lint`, `test` are defined in all workspaces. This makes it possible to run them in all workspaces:

```bash
# Build all workspaces
yarn workspaces foreach --all run build
```

## safety-web unit testing

```bash
yarn workspace eslint-plugin-safety-web run test
```

## unit tests + integrations tests

```bash
yarn run unit_tests
```

## Updating tsetse

The core logic behind this plugin is re-used from [tsec](https://github.com/google/tsec). The [`common`](https://github.com/google/tsec/tree/main/common) directory of tsec is mirrored in `safety-web/src/common`, as vendored dependency.

Run tsetse_update.sh to pull the latest version of tsetse in:

```bash
bash update_tsetse.sh
```
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
{
"private": true,
"license": "Apache-2.0",
"author": "Google ISE Web Team",
"workspaces": [
"safety-web"
]
}
"safety-web",
"tests/*",
"test-helpers/*"
],
"scripts": {
"unit_tests": "yarn workspace eslint-plugin-safety-web test",
"integration_tests": "yarn workspace basic-typescript-eslint9 test && yarn workspace basic-typescript-eslint8 test && yarn workspace basic-javascript-eslint9 test && yarn workspace basic-javascript-eslint8 test",
"update_integration_tests": "yarn workspace basic-typescript-eslint9 update && yarn workspace basic-typescript-eslint8 update && yarn workspace basic-javascript-eslint9 update && yarn workspace basic-javascript-eslint8 update",
"test": "yarn workspaces foreach --all run test"
},
"packageManager": "yarn@4.3.1"
}
19 changes: 19 additions & 0 deletions safety-web/.mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

module.exports = {
// Use tsx as a TypeScript node loader for Mocha https://stackoverflow.com/a/77609121
"require": "tsx",
"extension": ["ts"],
}
34 changes: 34 additions & 0 deletions safety-web/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parser: tseslint.parser,
parserOptions: {
project: "tsconfig.json", // Indicates to find the closest tsconfig.json for each source file (see https://typescript-eslint.io/packages/parser#project).
tsconfigRootDir: import.meta.dirname,
},
},
files: ["**/*.ts"],
},
{
rules: {
'no-undef': 'off',
'no-dupe-class-members': 'off',
},
files: ['**/*.ts'],
},
{
ignores: [
"**/*.js",
"**/*.mjs",
"test/test_fixtures/",
"lib/",
"node_modules/",
"src/common/", // tsetse folder is linted internally.
]
},
);
35 changes: 33 additions & 2 deletions safety-web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
{
"name": "safety-web",
"name": "eslint-plugin-safety-web",
"version": "0.1.0",
"license": "Apache-2.0",
"author": "Google ISE Web Team",
"main": "lib/src/index.js",
"files": [
"lib/src/"
],
"scripts": {
"clean": "tsc --build --clean",
"build": "tsc -b ./tsconfig.json",
"build:watch": "tsc -b ./tsconfig.json --watch",
"lint": "eslint",
"test": "mocha",
"test:watch": "mocha -r ts-node/register --watch --watch-files src/**/*.ts,test/**/*.ts"
},
"dependencies": {
"@typescript-eslint/parser": "^7.17.0",
"@typescript-eslint/utils": "^7.17.0",
"eslint": "^8.56.0 <9.0.0",
"tsutils": "^3.21.0",
"typescript": "^5.4.3 <5.5.0"
},
"devDependencies": {
"@eslint/eslintrc": "^3.1.0",
"@types/chai": "^4.3.16",
"@types/mocha": "^10.0.7",
"@types/node": "^20.14.9",
"@typescript-eslint/rule-tester": "^7.17.0",
"chai": "^5.1.1",
"mocha": "^10.6.0",
"ts-node": "^10.9.2",
"tsx": "^4.16.2",
"typescript-eslint": "^7.17.0"
}
}
}
71 changes: 71 additions & 0 deletions safety-web/src/common/configured_checker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {ENABLED_RULES} from './rule_groups';
import {Checker} from './third_party/tsetse/checker';
import * as ts from 'typescript';

import {
ExemptionList,
parseExemptionConfig,
resolveExemptionConfigPath,
} from './exemption_config';

/**
* Create a new cheker with all enabled rules registered and the exemption list
* configured.
*/
export function getConfiguredChecker(
program: ts.Program,
host: ts.ModuleResolutionHost,
): {checker: Checker; errors: ts.Diagnostic[]} {
let exemptionList: ExemptionList | undefined = undefined;

const exemptionConfigPath = resolveExemptionConfigPath(
program.getCompilerOptions()['configFilePath'] as string,
);

const errors = [];

if (exemptionConfigPath) {
const projExemptionConfigOrErr = parseExemptionConfig(exemptionConfigPath);
if (projExemptionConfigOrErr instanceof ExemptionList) {
exemptionList = projExemptionConfigOrErr;
} else {
errors.push(...projExemptionConfigOrErr);
}
}

// Create all enabled rules with corresponding exemption list entries.
const checker = new Checker(program, host);
const wildcardAllowListEntry = exemptionList?.get('*');
const rules = ENABLED_RULES.map((ruleCtr) => {
const allowlistEntries = [];
const allowlistEntry = exemptionList?.get(ruleCtr.RULE_NAME);
if (allowlistEntry) {
allowlistEntries.push(allowlistEntry);
}
if (wildcardAllowListEntry) {
allowlistEntries.push(wildcardAllowListEntry);
}
return new ruleCtr({allowlistEntries});
});

// Register all rules.
for (const rule of rules) {
rule.register(checker);
}

return {checker, errors};
}
Loading