FIX: Show notice if context is insecure

FIX: Show notice if context is insecure

diff --git a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
index 33bb69c..a95fc0b 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.hbs
@@ -1,51 +1,57 @@
 {{#if canEnableEncrypt}}
   <div class="control-group pref-title">
     <label class="control-label">{{i18n 'encrypt.title'}}</label>
-    <div class="controls">
-      {{#if isCurrentUser}}
-        {{#if isEncryptEnabled}}
-          {{#if isEncryptActive}}
-            <p>{{i18n 'encrypt.preferences.status_enabled'}}</p>
-            {{d-button icon="times" action=(action "deactivateEncrypt") label="encrypt.preferences.deactivate" id="deactivate"}}
-            {{d-button icon="plus" action=(action "generatePaperKey" true) label="encrypt.generate_paperkey.title_device"}}
-            {{d-button icon="ticket-alt" action=(action "generatePaperKey") label="encrypt.generate_paperkey.title"}}
-            {{encrypt-preferences-dropdown export=(action "export")
-                                           managePaperKeys=(action "managePaperKeys")}}
+    {{#if isSecureContext}}
+      <div class="controls">
+        {{#if isCurrentUser}}
+          {{#if isEncryptEnabled}}
+            {{#if isEncryptActive}}
+              <p>{{i18n 'encrypt.preferences.status_enabled'}}</p>
+              {{d-button icon="times" action=(action "deactivateEncrypt") label="encrypt.preferences.deactivate" id="deactivate"}}
+              {{d-button icon="plus" action=(action "generatePaperKey" true) label="encrypt.generate_paperkey.title_device"}}
+              {{d-button icon="ticket-alt" action=(action "generatePaperKey") label="encrypt.generate_paperkey.title"}}
+              {{encrypt-preferences-dropdown export=(action "export")
+                                            managePaperKeys=(action "managePaperKeys")}}
+            {{else}}
+              <form>
+                <p>{{i18n 'encrypt.preferences.status_enabled_but_inactive'}}</p>
+                {{{i18n 'encrypt.preferences.notice_active'}}}
+                <p>
+                  <label for="password">{{i18n "encrypt.preferences.passphrase_label"}}</label>
+                  {{input type="password" value=passphrase id="passphrase" autocomplete="current-password" disabled=inProgress}}
+                </p>
+                <p>
+                  {{d-button class="btn-primary" icon="unlock" action=(action "activateEncrypt") disabled=inProgress label="encrypt.preferences.activate"}}
+                  <span><a {{action "reset"}} class="text-danger">{{i18n 'encrypt.preferences.reset'}}</a></span>
+                </p>
+              </form>
+            {{/if}}
           {{else}}
-            <form>
-              <p>{{i18n 'encrypt.preferences.status_enabled_but_inactive'}}</p>
-              {{{i18n 'encrypt.preferences.notice_active'}}}
-              <p>
-                <label for="password">{{i18n "encrypt.preferences.passphrase_label"}}</label>
-                {{input type="password" value=passphrase id="passphrase" autocomplete="current-password" disabled=inProgress}}
-              </p>
-              <p>
-                {{d-button class="btn-primary" icon="unlock" action=(action "activateEncrypt") disabled=inProgress label="encrypt.preferences.activate"}}
-                <span><a {{action "reset"}} class="text-danger">{{i18n 'encrypt.preferences.reset'}}</a></span>
-              </p>
-            </form>
+            <p>{{i18n 'encrypt.preferences.status_disabled'}}</p>
+            {{#if importIdentity}}
+              {{{i18n 'encrypt.preferences.notice_import'}}}
+              {{textarea class="imported-keypair" value=identity}}
+            {{/if}}
+            {{d-button class="btn-primary" icon="lock" action=(action "enableEncrypt") disabled=inProgress label="encrypt.preferences.enable"}}
+            {{#if importIdentity}}
+              <span><a {{action (mut importIdentity) false}}>{{i18n 'cancel'}}</a></span>
+            {{else}}
+              <span><a {{action (mut importIdentity) true}}>{{i18n 'encrypt.preferences.import_key'}}</a></span>
+            {{/if}}
           {{/if}}
         {{else}}
-          <p>{{i18n 'encrypt.preferences.status_disabled'}}</p>
-          {{#if importIdentity}}
-            {{{i18n 'encrypt.preferences.notice_import'}}}
-            {{textarea class="imported-keypair" value=identity}}
-          {{/if}}
-          {{d-button class="btn-primary" icon="lock" action=(action "enableEncrypt") disabled=inProgress label="encrypt.preferences.enable"}}
-          {{#if importIdentity}}
-            <span><a {{action (mut importIdentity) false}}>{{i18n 'cancel'}}</a></span>
+          {{#if isEncryptEnabled}}
+            <p>{{i18n 'encrypt.preferences.status_enabled_other'}}</p>
+            <p><a {{action "reset"}} class="text-danger">{{i18n 'encrypt.preferences.reset'}}</a></p>
           {{else}}
-            <span><a {{action (mut importIdentity) true}}>{{i18n 'encrypt.preferences.import_key'}}</a></span>
+            <p>{{i18n 'encrypt.preferences.status_disabled_other'}}</p>
           {{/if}}
         {{/if}}
-      {{else}}
-        {{#if isEncryptEnabled}}
-          <p>{{i18n 'encrypt.preferences.status_enabled_other'}}</p>
-          <p><a {{action "reset"}} class="text-danger">{{i18n 'encrypt.preferences.reset'}}</a></p>
-        {{else}}
-          <p>{{i18n 'encrypt.preferences.status_disabled_other'}}</p>
-        {{/if}}
-      {{/if}}
-    </div>
+      </div>
+    {{else}}
+      <div class="alert alert-error">
+        <p>{{i18n 'encrypt.preferences.insecure_context'}}</p>
+      </div>
+    {{/if}}
   </div>
 {{/if}}
diff --git a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6 b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
index 85b65ba..db262f2 100644
--- a/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
+++ b/assets/javascripts/discourse/connectors/user-preferences-account/encrypt.js.es6
@@ -26,25 +26,27 @@ export default {
       const status = getEncryptionStatus(args.model);
       component.setProperties({
         model: args.model,
-        /** @var Value of passphrase input (old, current and retyped).
-         *       It should stay in memory for as little time as possible.
-         *       Clear it often.
+        /** Value of passphrase input (old, current and retyped).
+         *  It should stay in memory for as little time as possible.
+         *  Clear it often.
          */
         passphrase: "",
-        /** @var Whether any operation (AJAX request, key generation, etc.) is
+        /** Whether any operation (AJAX request, key generation, etc.) is
          *       in progress. */
         inProgress: false,
-        /** @var Whether current user is the same as model user. */
+        /** Whether current user is the same as model user. */
         isCurrentUser: true,
-        /** @var Whether plugin is enabled for current user. */
+        /** crypto.subtle is only available in secure contexts. */
+        isSecureContext: window.isSecureContext,
+        /** Whether plugin is enabled for current user. */
         canEnableEncrypt: canEnableEncrypt(args.model),
-        /** @var Whether the encryption is enabled or not. */
+        /** Whether the encryption is enabled or not. */
         isEncryptEnabled: status !== ENCRYPT_DISABLED,
-        /** @var Whether the encryption is active on this device. */
+        /** Whether the encryption is active on this device. */
         isEncryptActive: status === ENCRYPT_ACTIVE,
-        /** @var Whether it is an import operation. */
+        /** Whether it is an import operation. */
         importIdentity: false,
-        /** @var Key to be imported .*/
+        /** Key to be imported .*/
         identity: "",

[... diff too long, it was truncated ...]

GitHub sha: e361d75f

1 Like

That handles the preferences tab, but what happens if you haven’t yet enabled encrypted messages and open an encrypted topic in an insecure context?

1 Like

FIX: Show better error messages when decrypting posts