SecurityModel.js 5.5 KB

1234567891011121314151617181920
  1. export default class SecurityModel extends SDK.SDKModel{constructor(target){super(target);this._dispatcher=new SecurityDispatcher(this);this._securityAgent=target.securityAgent();target.registerSecurityDispatcher(this._dispatcher);this._securityAgent.enable();}
  2. resourceTreeModel(){return(this.target().model(SDK.ResourceTreeModel));}
  3. networkManager(){return(this.target().model(SDK.NetworkManager));}
  4. static SecurityStateComparator(a,b){let securityStateMap;if(SecurityModel._symbolicToNumericSecurityState){securityStateMap=SecurityModel._symbolicToNumericSecurityState;}else{securityStateMap=new Map();const ordering=[Protocol.Security.SecurityState.Info,Protocol.Security.SecurityState.InsecureBroken,Protocol.Security.SecurityState.Insecure,Protocol.Security.SecurityState.Neutral,Protocol.Security.SecurityState.Secure,Protocol.Security.SecurityState.Unknown];for(let i=0;i<ordering.length;i++){securityStateMap.set(ordering[i],i+1);}
  5. SecurityModel._symbolicToNumericSecurityState=securityStateMap;}
  6. const aScore=securityStateMap.get(a)||0;const bScore=securityStateMap.get(b)||0;return aScore-bScore;}}
  7. SDK.SDKModel.register(SecurityModel,SDK.Target.Capability.Security,false);export const Events={SecurityStateChanged:Symbol('SecurityStateChanged'),VisibleSecurityStateChanged:Symbol('VisibleSecurityStateChanged')};export const SummaryMessages={[Protocol.Security.SecurityState.Unknown]:ls`The security of this page is unknown.`,[Protocol.Security.SecurityState.Insecure]:ls`This page is not secure.`,[Protocol.Security.SecurityState.Neutral]:ls`This page is not secure.`,[Protocol.Security.SecurityState.Secure]:ls`This page is secure (valid HTTPS).`,[Protocol.Security.SecurityState.InsecureBroken]:ls`This page is not secure (broken HTTPS).`};export class PageSecurityState{constructor(securityState,explanations,summary){this.securityState=securityState;this.explanations=explanations;this.summary=summary;}}
  8. export class PageVisibleSecurityState{constructor(securityState,certificateSecurityState,safetyTipInfo,securityStateIssueIds){this.securityState=securityState;this.certificateSecurityState=certificateSecurityState?new CertificateSecurityState(certificateSecurityState):null;this.safetyTipInfo=safetyTipInfo?new SafetyTipInfo(safetyTipInfo):null;this.securityStateIssueIds=securityStateIssueIds;}}
  9. export class CertificateSecurityState{constructor(certificateSecurityState){this.protocol=certificateSecurityState.protocol;this.keyExchange=certificateSecurityState.keyExchange;this.keyExchangeGroup=certificateSecurityState.keyExchangeGroup||null;this.cipher=certificateSecurityState.cipher;this.mac=certificateSecurityState.mac||null;this.certificate=certificateSecurityState.certificate;this.subjectName=certificateSecurityState.subjectName;this.issuer=certificateSecurityState.issuer;this.validFrom=certificateSecurityState.validFrom;this.validTo=certificateSecurityState.validTo;this.certificateNetworkError=certificateSecurityState.certificateNetworkError||null;this.certificateHasWeakSignature=certificateSecurityState.certificateHasWeakSignature;this.certificateHasSha1Signature=certificateSecurityState.certificateHasSha1Signature;this.modernSSL=certificateSecurityState.modernSSL;this.obsoleteSslProtocol=certificateSecurityState.obsoleteSslProtocol;this.obsoleteSslKeyExchange=certificateSecurityState.obsoleteSslKeyExchange;this.obsoleteSslCipher=certificateSecurityState.obsoleteSslCipher;this.obsoleteSslSignature=certificateSecurityState.obsoleteSslSignature;}
  10. isCertificateExpiringSoon(){const expiryDate=new Date(this.validTo*1000);return(expiryDate<new Date(Date.now()).setHours(48))&&(expiryDate>Date.now());}
  11. getKeyExchangeName(){if(this.keyExchangeGroup){return this.keyExchange?ls`${this.keyExchange} with ${this.keyExchangeGroup}`:this.keyExchangeGroup;}
  12. return this.keyExchange;}
  13. getCipherFullName(){return this.mac?ls`${this.cipher} with ${this.mac}`:this.cipher;}}
  14. class SafetyTipInfo{constructor(safetyTipInfo){this.safetyTipStatus=safetyTipInfo.safetyTipStatus;this.safeUrl=safetyTipInfo.safeUrl||null;}}
  15. export class SecurityStyleExplanation{constructor(securityState,title,summary,description,certificate=[],mixedContentType=Protocol.Security.MixedContentType.None,recommendations=[]){this.securityState=securityState;this.title=title;this.summary=summary;this.description=description;this.certificate=certificate;this.mixedContentType=mixedContentType;this.recommendations=recommendations;}}
  16. class SecurityDispatcher{constructor(model){this._model=model;}
  17. securityStateChanged(securityState,schemeIsCryptographic,explanations,insecureContentStatus,summary){const pageSecurityState=new PageSecurityState(securityState,explanations,summary||null);this._model.dispatchEventToListeners(Events.SecurityStateChanged,pageSecurityState);}
  18. visibleSecurityStateChanged(visibleSecurityState){const pageVisibleSecurityState=new PageVisibleSecurityState(visibleSecurityState.securityState,visibleSecurityState.certificateSecurityState||null,visibleSecurityState.safetyTipInfo||null,visibleSecurityState.securityStateIssueIds);this._model.dispatchEventToListeners(Events.VisibleSecurityStateChanged,pageVisibleSecurityState);}
  19. certificateError(eventId,errorType,requestURL){}}
  20. self.Security=self.Security||{};Security=Security||{};Security.SecurityModel=SecurityModel;Security.SecurityModel.Events=Events;Security.SummaryMessages=SummaryMessages;Security.PageSecurityState=PageSecurityState;Security.PageVisibleSecurityState=PageVisibleSecurityState;Security.CertificateSecurityState=CertificateSecurityState;Security.SecurityStyleExplanation=SecurityStyleExplanation;