All files / src/context/directory/handlers attackProtection.ts

95.65% Statements 22/23
75% Branches 3/4
100% Functions 3/3
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 841x 1x 1x 1x                                       51x   51x                 50x   50x 48x         2x       2x       2x         2x                   1x   1x   1x 1x   1x 1x 1x     1x         1x  
import fs from 'fs-extra';
import path from 'path';
import { constants } from '../../../tools';
import { dumpJSON, existsMustBeDir, loadJSON } from '../../../utils';
import { DirectoryHandler } from '.';
import DirectoryContext from '..';
import { Asset, ParsedAsset } from '../../../types';
 
type ParsedAttackProtection = ParsedAsset<
  'attackProtection',
  {
    breachedPasswordDetection: Asset;
    bruteForceProtection: Asset;
    suspiciousIpThrottling: Asset;
  }
>;
 
function attackProtectionFiles(filePath: string): {
  directory: string;
  breachedPasswordDetection: string;
  bruteForceProtection: string;
  suspiciousIpThrottling: string;
} {
  const directory = path.join(filePath, constants.ATTACK_PROTECTION_DIRECTORY);
 
  return {
    directory: directory,
    breachedPasswordDetection: path.join(directory, 'breached-password-detection.json'),
    bruteForceProtection: path.join(directory, 'brute-force-protection.json'),
    suspiciousIpThrottling: path.join(directory, 'suspicious-ip-throttling.json'),
  };
}
 
function parse(context: DirectoryContext): ParsedAttackProtection {
  const files = attackProtectionFiles(context.filePath);
 
  if (!existsMustBeDir(files.directory)) {
    return {
      attackProtection: null,
    };
  }
 
  const breachedPasswordDetection = loadJSON(files.breachedPasswordDetection, {
    mappings: context.mappings,
    disableKeywordReplacement: context.disableKeywordReplacement,
  });
  const bruteForceProtection = loadJSON(files.bruteForceProtection, {
    mappings: context.mappings,
    disableKeywordReplacement: context.disableKeywordReplacement,
  });
  const suspiciousIpThrottling = loadJSON(files.suspiciousIpThrottling, {
    mappings: context.mappings,
    disableKeywordReplacement: context.disableKeywordReplacement,
  });
 
  return {
    attackProtection: {
      breachedPasswordDetection,
      bruteForceProtection,
      suspiciousIpThrottling,
    },
  };
}
 
async function dump(context: DirectoryContext): Promise<void> {
  const { attackProtection } = context.assets;
 
  Iif (!attackProtection) return;
 
  const files = attackProtectionFiles(context.filePath);
  fs.ensureDirSync(files.directory);
 
  dumpJSON(files.breachedPasswordDetection, attackProtection.breachedPasswordDetection);
  dumpJSON(files.bruteForceProtection, attackProtection.bruteForceProtection);
  dumpJSON(files.suspiciousIpThrottling, attackProtection.suspiciousIpThrottling);
}
 
const attackProtectionHandler: DirectoryHandler<ParsedAttackProtection> = {
  parse,
  dump,
};
 
export default attackProtectionHandler;