Skip to content

Commit

Permalink
feat: add rule about analytics tools
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelDemey committed Sep 10, 2024
1 parent 7b71310 commit 7ab60d3
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions audit/apps/audit/src/checkers/async/http-system-checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,29 @@ import puppeteer from 'puppeteer';

type Checker = (
url: string,
parsers: { name: string; result: any }[]
) => Promise<{ name: string; result: any } | undefined>;
parsers: { name: string; result: any }[],
requests?: Set<string>
) => Promise<{ name: string; message?: string; result: any } | undefined>;

const hasAtLeastOneAnalyticsTools = (
_url: string,
parsers: { name: string; result: any }[],
requests: Set<string>
): Promise<{ name: string; result: any; message: string } | undefined> => {
const analytics = Array.from(requests).filter((request) =>
['matomo.js', 'metricalp'].find((a) => request.includes(a))
);

if (analytics.length > 1) {
return Promise.resolve({
name: 'hasAtLeastOneAnalyticsTools',
result: analytics,
message: 'You have multiple analytics tools',
});
}

return Promise.resolve(undefined);
};

const getStatistics = async (
url: string,
Expand Down Expand Up @@ -65,11 +86,17 @@ const getImageWithoutAlts = async (url: string, _parsers: any) => {
}
};
export class HttpChecker {
#checkers: Checker[] = [getStatistics, getImageWithoutAlts];
#checkers: Checker[] = [getImageWithoutAlts, hasAtLeastOneAnalyticsTools];
constructor(private parsers: { name: string; result: any }[]) {}
check(url: string) {
return Promise.all(
this.#checkers.map((checker) => checker(url, this.parsers))
async check(url: string) {
const statistics = await getStatistics(url, this.parsers);

const responses = await Promise.all(
this.#checkers.map((checker) =>
checker(url, this.parsers, statistics.result.urls)
)
).then((result) => result.filter((r) => !!r));

return [statistics, ...responses];
}
}

0 comments on commit 7ab60d3

Please sign in to comment.