import { Application } from 'egg'; import { safeCurlForApplication, type HttpClientRequestURL, type HttpClientOptions, type HttpClientResponse, } from '../../lib/extend/safe_curl.ts'; const INPUT_CSRF = '\r\n'; const INJECTION_DEFENSE = ''; export default class SecurityApplication extends Application { injectCsrf(html: string): string { html = html.replace(/()([\s\S]*?)<\/form>/gi, (_, $1, $2) => { const match = $2; if (match.includes('name="_csrf"') || match.includes("name='_csrf'")) { return $1 + match + ''; } return $1 + match + INPUT_CSRF; }); return html; } injectNonce(html: string): string { html = html.replace(/([\s\S]*?)<\/script[^>]*?>/gi, (_, $1, $2) => { if (!$1.includes('nonce=')) { $1 += ' nonce="{{ctx.nonce}}"'; } return '' + $2 + ''; }); return html; } injectHijackingDefense(html: string): string { return INJECTION_DEFENSE + html + INJECTION_DEFENSE; } async safeCurl(url: HttpClientRequestURL, options?: HttpClientOptions): Promise> { return await safeCurlForApplication(this, url, options); } }