-
Notifications
You must be signed in to change notification settings - Fork 60.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port
secret-scanning.js
to TypeScript (#51187)
- Loading branch information
Showing
4 changed files
with
56 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import fs from 'fs' | ||
|
||
import yaml from 'js-yaml' | ||
import type { NextFunction, Response } from 'express' | ||
|
||
import getApplicableVersions from '@/versions/lib/get-applicable-versions.js' | ||
import { liquid } from '@/content-render/index.js' | ||
import { ExtendedRequest, SecretScanningData } from '@/types' | ||
|
||
const secretScanningPath = 'data/secret-scanning.yml' | ||
|
||
export default async function secretScanning( | ||
req: ExtendedRequest, | ||
res: Response, | ||
next: NextFunction, | ||
) { | ||
if (!req.pagePath!.endsWith('code-security/secret-scanning/secret-scanning-patterns')) | ||
return next() | ||
|
||
const secretScanningData = yaml.load( | ||
fs.readFileSync(secretScanningPath, 'utf-8'), | ||
) as SecretScanningData[] | ||
|
||
if (!req.context) throw new Error('request not contextualized') | ||
const { currentVersion } = req.context | ||
|
||
req.context.secretScanningData = secretScanningData.filter((entry) => | ||
getApplicableVersions(entry.versions).includes(currentVersion), | ||
) | ||
|
||
// Some entries might use Liquid syntax, so we need | ||
// to execute that Liquid to get the actual value. | ||
req.context.secretScanningData.forEach(async (entry) => { | ||
for (const [key, value] of Object.entries(entry)) { | ||
if (key === 'hasValidityCheck' && typeof value === 'string' && value.includes('{%')) { | ||
const evaluated = yaml.load(await liquid.parseAndRender(value, req.context)) | ||
entry[key] = evaluated as string | ||
} | ||
} | ||
}) | ||
|
||
return next() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters