flawopen.com/Angular सुरक्षा/Angular
Angular XSS सुरक्षा: सुरक्षित defaults और bypass APIs
Angular template bindings के मानों को अविश्वसनीय मानता है: interpolation टेक्स्ट को escape करता है और HTML व URL bindings को संदर्भ के अनुसार sanitize किया जाता है। जोखिम वाली सीमा स्पष्ट trust है: bypassSecurityTrust* Angular को उस मान की सामान्य sanitization छोड़ने के लिए कहता है। इसका उपयोग तभी करें जब मान को उसी सटीक security context के लिए validate और construct किया गया हो जहाँ वह इस्तेमाल होगा।
this.safeHtml = this.sanitizer
.bypassSecurityTrustHtml(comment);
// <div [innerHTML]="safeHtml"></div>
<div>{{ comment }}</div>
// interpolation markup को टेक्स्ट के रूप में escape करता है
Angular क्या sanitize करता है — और क्या नहीं कर सकता
Angular interpolated टेक्स्ट को escape करता है और HTML तथा URL contexts में अविश्वसनीय मानों को sanitize करता है। उदाहरण के लिए [innerHTML] खतरनाक markup हटाकर सुरक्षित HTML रखता है; सामान्य interpolation markup को parse ही नहीं करता। Resource URL (जैसे executable सामग्री लोड करने वाला iframe source) को सामान्य तरीके से sanitize नहीं किया जा सकता, क्योंकि वह मनमाने code की ओर जा सकता है। हमलावर-नियंत्रित मान bypassSecurityTrustHtml, bypassSecurityTrustUrl, bypassSecurityTrustResourceUrl या संबंधित bypass APIs को न दें: ये किसी विशिष्ट context में मान को सुरक्षित घोषित कर sanitization छोड़ देते हैं। वैध embed के लिए URL को fixed origin और validated identifier से बनाएँ और trusted चिह्नित करने से ठीक पहले ऐसा करें।
Bypass APIs और DOM sinks का audit करें
grep -rn "bypassSecurityTrust" --include="*.ts" .आम सवाल
CSP और Trusted Types से अतिरिक्त सुरक्षा
Angular की सुरक्षित bindings के साथ अतिरिक्त सुरक्षा के लिए Content Security Policy (CSP) का उपयोग करें और Trusted Types को लागू करें। जब ऐप DomSanitizer.bypassSecurityTrust* को कॉल करता है, तो Angular को Trusted Types policy angular#unsafe-bypass चाहिए। जहाँ संभव हो, इन कॉल को हटाएँ; अन्यथा हर call site दर्ज करें और केवल उसी सटीक context के लिए सत्यापित मान पर भरोसा करें।
नहीं। Template bindings को framework सुरक्षा मिलती है, लेकिन सीधे DOM APIs, ElementRef और third-party DOM libraries उसे bypass कर सकते हैं। जहाँ संभव हो template bindings का उपयोग करें, इन sinks का अलग audit करें और किसी input को trusted तभी मानें जब वह सटीक security context के लिए validate हो।