IHOR.DEVKNOWLEDGE_BASE
MENU

~/blog / Security / optional-permissions-for-better-extension-privacy.md

// pillar guide · 5 min read

Optional Permissions for Better Extension Privacy

Build an optional-permission flow that explains access, requests it on user interaction, handles denial and revocation, and keeps grants separate from settings.

Quick answer

Optional permissions let users enable a feature when they understand why it needs access. Declare the possible capability in the manifest, request the narrow actual grant from a user gesture, and treat denial as a supported result. A saved preference such as autoInspect: true is not proof that Chrome still grants the necessary site access. Ask the permissions API for the current state.

This guide focuses on the interface and lifecycle of consent. Use activeTab instead when the feature needs only a single user-invoked inspection; use optional host access when the user wants a continuing feature on a site. Making access optional does not excuse requesting more than the feature needs, and not every browser permission supports optional declaration.

Practical implementation steps

Explain the feature before the prompt

Place a control beside the feature it enables, for example “Enable automatic inspection on app.example.com.” Say what will be read, whether it stays local, and how to disable the feature. Do not hide a request behind a generic Save button or combine site access with unrelated capabilities. Keep the browser's prompt and the product's wording consistent without pretending you control the browser's warning text.

Request inside the interaction

Prepare the requested origin before the click handler. Call permissions.request directly from that handler, then process its result. Unrelated asynchronous preparation before the request can break the user-gesture requirement. The returned Boolean tells you whether access was granted; an exception is a separate failure, not another name for the user's refusal. Show those states separately in the interface.

Keep intent and capability separate

A checkbox expresses the user's desired feature state. Chrome's grant expresses what the extension can currently do. Persist intent only after the request succeeds and derive the effective state from intent plus current permissions. Listen for permission changes and refresh open views. A grant may exist while the feature is disabled; a preference may survive while a grant has been removed. Neither state should cause a misleading “enabled” indicator.

Reconcile registrations and retained data

If granting a site registers a dynamic content script, removal should reconcile that registration and stop future work. Unregistering a script does not remove code or UI already injected into an existing document. Provide a cleanup path for that UI if required. Revoking access also does not inherently delete a locally saved collection. Offer deletion as a separate action, and document what each control changes.

Implementation example

The following illustration assumes an extension options page with a button enable-site and a status element site-status. It requests one declared host. If supporting arbitrary sites, declare an appropriate optional ceiling and compute an exact requested origin from validated input before the click. The example does not implement content-script registration.

{
  "manifest_version": 3,
  "name": "Optional site example",
  "version": "1.0.0",
  "optional_host_permissions": ["https://app.example.com/*"]
}
const grant = { origins: ["https://app.example.com/*"] };
const button = document.querySelector("#enable-site");
const status = document.querySelector("#site-status");
button.addEventListener("click", async () => {
  try {
    const accepted = await chrome.permissions.request(grant);
    status.textContent = accepted ? "Site access enabled" : "Access was not granted";
  } catch {
    status.textContent = "Could not request site access";
  }
});
chrome.permissions.onRemoved.addListener(async () => {
  const available = await chrome.permissions.contains(grant);
  status.textContent = available ? "Site access enabled" : "Site access unavailable";
});

In a complete feature, disable repeated clicks during an active request and initialize the UI from contains when the page opens. Before performing privileged work, recheck and handle failure as well: permission can change between checking and using it. Use the browser grant as the authority rather than trusting a cached Boolean.

Tradeoffs and alternatives

Required access can be appropriate for the product's essential purpose, while optional access fits enhancements that can stay disabled. Splitting one feature into many tiny prompts may confuse users more than a single coherent request. Group capabilities only when they enable the same explained operation, and preserve usable behavior when an enhancement remains off.

Font Picker's documented manual page inspection is a useful scenario for reasoning about temporary access versus a proposed automatic mode. Its repository description does not establish that such an automatic mode exists. Treat persistent inspection as a separate design proposal that would need its own permissions, privacy description, and failure handling.

Common mistakes

  • Asking again on every popup open after a denial.
  • Treating an optional declaration as a current grant.
  • Saving enabled state before the request resolves.
  • Forgetting registrations or injected UI when access changes.
  • Assuming removal deletes previously saved user data.

Production checklist

  • The request follows a clearly labelled user interaction.
  • The actual requested origins are narrower than or equal to the declared ceiling.
  • Denial, exception, and later revocation have distinct states.
  • Opening settings reads current permissions.
  • Registration and UI cleanup follow the effective feature state.
  • Access removal and saved-data deletion are separately explained.

FAQ

Can every permission be optional?

No. Consult the current API reference's exceptions before designing the interface. A feature requiring an unsupported optional permission needs a different capability or a required-permission design. Do not claim a prompt flow the platform cannot implement.

Does adding an optional permission enable it for existing users?

No. It declares something the extension may request. The user still needs to enable the corresponding feature and grant access. Keep the default feature state consistent with the absence of that grant.

Should declining block the rest of the extension?

Usually an optional feature should stay off while independent features remain usable. If no useful behavior exists without the permission, reconsider whether it is really optional and explain the product's core requirement honestly.

Conclusion

Build consent as a state machine: unavailable, requesting, granted, denied, and revoked. Derive effective behavior from actual grants and user intent, then reconcile injected work and saved data explicitly. A clear removal path is part of the feature, not an afterthought to the request button.

Primary sources

Ihor Kharchyshyn

Independent full-stack product engineer writing source-backed implementation guides for browser extensions and modern web products.

Need this implemented in a real product?

Browser tools with a clear purpose, focused permissions, and a maintainable Manifest V3 architecture.