{"name":"node-sarif-builder","version":"2.0.3","keywords":["sarif","builder","node","simple","linter","output","format"],"license":"MIT","_id":"node-sarif-builder@2.0.3","ava":{"files":["!build/module/**"],"timeout":"60s","failFast":true,"typescript":{"compile":false,"rewritePaths":{"src/":"dist/"}}},"nyc":{"exclude":["**/*.spec.js"],"extends":"@istanbuljs/nyc-config-typescript"},"dist":{"shasum":"179ae590ce020f97f9e45037dc1cde85aa4398ec","tarball":"http://registry-npm.ywwl.com/node-sarif-builder/download/node-sarif-builder-2.0.3.tgz","fileCount":61,"integrity":"sha512-Pzr3rol8fvhG/oJjIq2NTVB0vmdNNlz22FENhhPojYRZ4/ee08CfK4YuKmuL54V9MLhI1kpzxfOJ/63LzmZzDg==","signatures":[{"sig":"MEUCIHylJEeGvboDDmjkyp/Ns50729wpA8If++Mh4uOBrUJXAiEAgJrzy7gobcd8gyYzbVu8ltjZ89qIRNmae0/Jav7Gu2M=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":306733,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjVJOuACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmq0GRAAhWFPjauquw0wRsud4EGp0IuePm2x1u4NDRa/4i0Vwuzmlnuv\r\n7c5VqRJyWLVgoLZ18+d+cYVTejkLKaAEMLLlq39vfHYX2A3cwjh5gRApmwDj\r\n6r097xRzPWammraTu8ahuWfqXv6fDFIc6DKNtTmTgvPRi/nbWmwf+pAgSDeE\r\n1yvEJApLcxtvjTIg+NuAcnJGRClaNknVNDyZ5cdVMQru9iZJgqwqLz7ivKUF\r\n78T4YDMRCaRCF8IGgoKY6C9aNO1hiFv1Uj7aJNz3XKZ++c6cz2iD+9cNstxI\r\nEavllP5hA99c2tT7Vtt4B/jAmRh5YXbS1GCCvReDva8l9spZKrrmP3lLmWuN\r\ns/jeED26Ra3b0jnK8woSXw80ZSSd9oWsZutiNsSES4abUUnLV9EHpuwE9beY\r\nrKw9XOMh+H26JwWOysH9RoQp6yHybV7zp2dCtr3hCJVZ7YO7wP4ecUwOnMnD\r\nOu4ETTIb6gbSXoVVBpRjg9NzvGDXjuOL370xPJSG4ER32WxAvNhTz9MgMxBi\r\nEtIk76Tc+NE89AtE0lcAH/3Q1VWINjzQX2kwfskYqxoyDApJ/5hFHLCpent2\r\nUxHoJPTrkP914v2E4ByWQgdhsXp+KhIOEhaGN2qi+3XDMGEXNzc4BXCabTLB\r\n3/UUcnqPMv+SyfLWWdZ1b+AyYM2K+aU7fLk=\r\n=WMGz\r\n-----END PGP SIGNATURE-----\r\n","size":106618,"noattachment":false,"key":"/node-sarif-builder/-/node-sarif-builder-2.0.3.tgz"},"main":"dist/index.js","config":{"commitizen":{"path":"cz-conventional-changelog"}},"module":"dit/index.js","engines":{"node":">=14"},"scripts":{"cov":"run-s build test:unit cov:html cov:lcov && open-cli coverage/index.html","fix":"run-s fix:*","test":"yarn test:unit","pretest":"tsc","cov:html":"nyc report --reporter=html","cov:lcov":"nyc report --reporter=lcov","cov:send":"run-s cov:lcov && codecov","fix:lint":"eslint src --ext .ts --fix","cov:check":"nyc report && nyc check-coverage --lines 100 --functions 100 --branches 100","test:lint":"eslint src --ext .ts","test:unit":"nyc --silent ava","prepublish":"tsc","watch:test":"nyc --silent ava --watch","watch:build":"tsc -p tsconfig.json -w","fix:prettier":"prettier \"src/**/*.ts\" --write","test:prettier":"prettier \"src/**/*.ts\" --list-different","diff-integration-tests":"mkdir -p diff && rm -rf diff/test && cp -r test diff/test && rm -rf diff/test/test-*/.git && cd diff && git init --quiet && git add -A && git commit --quiet --no-verify --allow-empty -m 'WIP' && echo '\\n\\nCommitted most recent integration test output in the \"diff\" directory. Review the changes with \"cd diff && git diff HEAD\" or your preferred git diff viewer.'"},"typings":"dist/index.d.ts","_npmUser":{"name":"nvuillam","email":"nicolas.vuillamy@gmail.com"},"prettier":{"singleQuote":true},"repository":{"url":"https://github.com/nvuillam/node-sarif-builder","type":"git"},"description":"Module to help building SARIF log files","directories":{},"licenseText":"MIT License\n\nCopyright (c) 2022 - Nicolas Vuillamy\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","dependencies":{"fs-extra":"^10.0.0","@types/sarif":"^2.1.4"},"_hasShrinkwrap":false,"devDependencies":{"ava":"^4.0.1","nyc":"^15.1.0","cspell":"^4.1.0","eslint":"^7.8.0","codecov":"^3.5.0","ts-node":"^9.0.0","prettier":"^2.1.1","typescript":"^4.0.2","@types/node":"^17.0.8","@ava/typescript":"^3.0.1","@types/fs-extra":"^9.0.13","eslint-plugin-import":"^2.22.0","eslint-config-prettier":"^6.11.0","@typescript-eslint/parser":"^4.0.1","eslint-plugin-eslint-comments":"^3.2.0","@typescript-eslint/eslint-plugin":"^4.0.1","@istanbuljs/nyc-config-typescript":"^1.0.2"},"_npmOperationalInternal":{"tmp":"tmp/node-sarif-builder_2.0.3_1666487214033_0.6022283916513327","host":"s3://npm-registry-packages"},"readme":"# node-sarif-builder\n\n[![Version](https://img.shields.io/npm/v/node-sarif-builder.svg)](https://npmjs.org/package/node-sarif-builder)\n[![Downloads/week](https://img.shields.io/npm/dw/node-sarif-builder.svg)](https://npmjs.org/package/node-sarif-builder)\n[![Downloads/total](https://img.shields.io/npm/dt/node-sarif-builder.svg)](https://npmjs.org/package/node-sarif-builder)<!-- gh-dependents-info-used-by-start -->\n[![Generated by github-dependents-info](https://img.shields.io/static/v1?label=Used%20by&message=6556&color=informational&logo=slickpic)](https://github.com/nvuillam/node-sarif-builder/blob/main/docs/github-dependents-info.md)<!-- gh-dependents-info-used-by-end -->\n[![Test](https://github.com/nvuillam/node-sarif-builder/workflows/Test/badge.svg?branch=main)](https://github.com/nvuillam/node-sarif-builder/actions?query=workflow%3ATest+branch%3Amain)\n[![Mega-Linter](https://github.com/nvuillam/node-sarif-builder/workflows/MegaLinter/badge.svg?branch=main)](https://megalinter.github.io/)\n[![GitHub contributors](https://img.shields.io/github/contributors/nvuillam/node-sarif-builder.svg)](https://github.com/nvuillam/node-sarif-builder/graphs/contributors/)\n[![GitHub stars](https://img.shields.io/github/stars/nvuillam/node-sarif-builder?label=stars&maxAge=3600)](https://github.com/nvuillam/node-sarif-builder/stargazers/)\n[![License](https://img.shields.io/npm/l/node-sarif-builder.svg)](https://github.com/nvuillam/node-sarif-builder/blob/master/package.json)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)\n\n## Introduction\n\nUntil today, every SAST tool (not exhaustive list available at <https://analysis-tools.dev/>) is using its own custom output format.\n\nIn order to **unify SAST tools output format**, more and more tools and services are implementing [**SARIF format**](https://sarifweb.azurewebsites.net/) ([example](https://github.com/microsoft/sarif-tutorials/blob/main/samples/1-Introduction/simple-example.sarif))\n\nSARIF logs can be:\n- **Uploaded to DevOps tools**, like [Github](https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/sarif-support-for-code-scanning), [Azure DevOps](https://github.com/microsoft/sarif-azuredevops-extension) to show issues directly in their web UI <!-- markdown-link-check-disable-line -->\n- **Visualized in IDEs**, like [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=MS-SarifVSCode.sarif-viewer), [Visual Studio](https://marketplace.visualstudio.com/items?itemName=WDGIS.MicrosoftSarifViewer), [Jetbrains editors](https://plugins.jetbrains.com/plugin/16938-qodana)\n- **Aggregated by multi-language linters**, like [MegaLinter](https://megalinter.io/latest/)\n\nExample of linters that can output logs in SARIF format:\n\n- bandit (python)\n- checkov (terraform)\n- checkstyle (java)\n- cfn-lint (AWS CloudFormation)\n- codeql (multi-language)\n- devskim (security)\n- eslint (javascript,typescript,json)\n- gitleaks (security)\n- ktlint (Kotlin)\n- hadolint (Dockerfile)\n- MegaLinter (linters orchestrator)\n- psalm (php)\n- semgrep (multi-language)\n- revive (Go)\n- tflint (terraform)\n- terrascan (terrasform)\n- trivy (security)\n- and many more...\n\nIf you are a **maintainer** of any **javascript/typescript based** SAST tool, but also IaC tool, or **any type of tool that can return a list of errors with a level of severity**, you can either:\n\n- read the whole [OASIS Specification](https://docs.oasis-open.org/sarif/sarif/v2.1.0/csprd01/sarif-v2.1.0-csprd01.html) and implement it\n- **simply use this library** to add SARIF as additional output format, so your tool will be natively compliant with any of SARIF-compliant tools !\n\n## Installation\n\n- with npm\n\n```shell\nnpm install node-sarif-builder\n```\n\n- with yarn\n\n```shell\nyarn add node-sarif-builder\n```\n\n## Use\n\nWith node-sarif-builder, you can generate complex SARIF format with simple methods\n\n___\n\n- Start by importing module\n\n```javascript\nconst { SarifBuilder, SarifRunBuilder, SarifResultBuilder, SarifRuleBuilder } = require(\"node-sarif-builder\");\n```\n\n___\n\n- Create and init **SarifBuilder** and **SarifRunBuilder** objects\n\n```javascript\n// SARIF builder\nconst sarifBuilder = new SarifBuilder();\n// SARIF Run builder\nconst sarifRunBuilder = new SarifRunBuilder().initSimple({\n    toolDriverName: \"npm-groovy-lint\",                           // Name of your analyzer tool\n    toolDriverVersion: \"9.0.5\",                                  // Version of your analyzer tool\n    url: \"https://nvuillam.github.io/npm-groovy-lint/\"           // Url of your analyzer tool\n});\n```\n\n___\n\n- Add all rules that can be found in your results (recommended but optional)\n\n```javascript\n// Add SARIF rules\nfor (const rule of rules) {                           // rules from your linter in any format\n    const sarifRuleBuiler = new SarifRuleBuilder().initSimple({\n        ruleId: rule.id,                              // ex: \"no-any\"\n        shortDescriptionText: rule.description,       // ex: \"Do not use any in your code !\"\n        helpUri: rule.docUrl                          // ex: \"http://my.linter.com/rules/no-any\"\n    });\n    sarifRunBuilder.addRule(sarifRuleBuiler);\n}\n```\n\n___\n\n- For each found issue, create a SarifResultBuilder and add it to the SarifRunBuilder object\n\n```javascript\nimport { pathToFileURL } from 'url'\n\n// Add results\nfor (const issue of issues) { // issues from your linter in any format\n    const sarifResultBuilder = new SarifResultBuilder();\n    const sarifResultInit = {\n         // Transcode to a SARIF level:  can be \"warning\" or \"error\" or \"note\"\n        level: issue.severity === \"info\" ? \"note\" : issue.severity,\n        messageText: err.msg,                                     // Ex: \"any is forbidden !\"\n        ruleId: err.rule,                                         // Ex: \"no-any\"\n        fileUri: process.env.SARIF_URI_ABSOLUTE                   // Ex: src/myfile.ts\n            ? pathToFileURL(fileNm)\n            : path.relative(process.cwd(), fileNm).replace(/\\\\/g, '/'),\n    };\n    // When possible, provide location of the issue in the source code\n    if (issue.range) {\n        sarifResultInit.startLine = issue.range.start.line;        // any integer >= 1 (optional)\n        sarifResultInit.startColumn = issue.range.start.character; // any integer >= 1 (optional)\n        sarifResultInit.endLine = issue.range.end.line;            // any integer >= 1 (optional)\n        sarifResultInit.endColumn = issue.range.end.character;     // any integer >= 1 (optional)\n    }\n    // Init sarifResultBuilder\n    sarifResultBuilder.initSimple(sarifResultInit); \n    // Add result to sarifRunBuilder\n    sarifRunBuilder.addResult(sarifResultBuilder);\n}\n```\n\n___\n\n- Add run to sarifBuilder then generate JSON SARIF output file\n\n```javascript\n    sarifBuilder.addRun(sarifRunBuilder);\n    const sarifJsonString = sarifBuilder.buildSarifJsonString({ indent: false }); // indent:true if you like\n    fs.writeFileSync(outputSarifFile,sarifJsonString);\n    // const sarifObj = sarifBuilder.buildSarifOutput();                          // You could also just get the Sarif log as an object and not a string\n```\n\n## Full example\n\n- Working in [npm-groovy-lint](https://github.com/nvuillam/npm-groovy-lint)\n\n```javascript\nimport { pathToFileURL } from 'url'\n\nfunction buildSarifResult(lintResult) {\n    // SARIF builder\n    const sarifBuilder = new SarifBuilder();\n    // SARIF Run builder\n    const sarifRunBuilder = new SarifRunBuilder().initSimple({\n        toolDriverName: \"npm-groovy-lint\",\n        toolDriverVersion: \"9.0.5\", \n        url: \"https://nvuillam.github.io/npm-groovy-lint/\"\n    });\n    // SARIF rules\n    for (const ruleId of Object.keys(lintResult.rules || {})) {\n        const rule = lintResult.rules[ruleId];\n        const sarifRuleBuiler = new SarifRuleBuilder().initSimple({\n            ruleId: ruleId,\n            shortDescriptionText: rule.description,\n            helpUri: rule.docUrl\n        });\n        sarifRunBuilder.addRule(sarifRuleBuiler);\n    }\n    // Add SARIF results (individual errors)\n    for (const fileNm of Object.keys(lintResult.files)) {\n        const fileErrors = lintResult.files[fileNm].errors;\n        for (const err of fileErrors) {\n            const sarifResultBuilder = new SarifResultBuilder();\n            const sarifResultInit = {\n                level: err.severity === \"info\" ? \"note\" : err.severity, // Other values can be \"warning\" or \"error\"\n                messageText: err.msg,\n                ruleId: err.rule,\n                fileUri: process.env.SARIF_URI_ABSOLUTE\n                    ? pathToFileURL(fileNm)\n                    : path.relative(process.cwd(), fileNm).replace(/\\\\/g, '/')\n            };\n            if (err.range) {\n                sarifResultInit.startLine = fixLine(err.range.start.line);\n                sarifResultInit.startColumn = fixCol(err.range.start.character);\n                sarifResultInit.endLine = fixLine(err.range.end.line);\n                sarifResultInit.endColumn = fixCol(err.range.end.character);\n            }\n            sarifResultBuilder.initSimple(sarifResultInit);\n            sarifRunBuilder.addResult(sarifResultBuilder);\n        }\n    }\n    sarifBuilder.addRun(sarifRunBuilder);\n    return sarifBuilder.buildSarifJsonString({ indent: false });\n}\n\nfunction fixLine(val) {\n    if (val === null) {\n        return undefined;\n    }\n    return val === 0 ? 1 : val;\n}\n\nfunction fixCol(val) {\n    if (val === null) {\n        return undefined;\n    }\n    return val === 0 ? 1 : val + 1;\n}\n```\n\n## Test\n\nYou can confirm that your generated SARIF logs are valid on <https://sarifweb.azurewebsites.net/Validation>\n","publish_time":1666487214287,"maintainers":[{"name":"nvuillam","email":"nicolas.vuillamy@gmail.com"}],"_cnpm_publish_time":1666487214287,"dist-tags":{"alpha":"3.3.2-alpha202512300944.0","beta":"3.3.2-beta202511230758.0","latest":"5.0.0"}}