---
title: Permissions-Policy设备与传感器权限治理(camera/microphone/geolocation/sensors)最佳实践
keywords:
- Permissions-Policy
- camera
- microphone
- geolocation
- accelerometer
- gyroscope
- magnetometer
description: 通过Permissions-Policy对白名单页面与来源精细限制摄像头、麦克风、定位与传感器,降低越权与隐私风险。
categories:
- 文章资讯
- 编程技术
---
背景与价值
设备与传感器权限具有隐私敏感性。统一在响应头限制并按白名单逐项开启,可有效降低风险。
统一规范
- 默认禁用:
camera=(); microphone=(); geolocation=(); accelerometer=(); gyroscope=(); magnetometer=()。 - 例外开启:针对受控来源与页面按需开启。
- 审计:记录权限命中与拒绝。
核心实现
头设置
type Res = { setHeader: (k: string, v: string) => void }
function setPermissions(res: Res, allow: { camera?: string[]; microphone?: string[]; geolocation?: string[]; accelerometer?: string[]; gyroscope?: string[]; magnetometer?: string[] } = {}) {
function fmt(name: string, list?: string[]): string { if (!list || list.length === 0) return `${name}=()`; return `${name}=( ${list.map(o => `"${o}"`).join(' ')} )` }
const v = [
fmt('camera', allow.camera),
fmt('microphone', allow.microphone),
fmt('geolocation', allow.geolocation),
fmt('accelerometer', allow.accelerometer),
fmt('gyroscope', allow.gyroscope),
fmt('magnetometer', allow.magnetometer)
].join(', ')
res.setHeader('Permissions-Policy', v)
}
落地建议
- 全站默认禁用,针对受控页面按来源白名单开启必要权限;最小化粒度与范围。
- 持续审计与复核权限使用,收敛不必要的访问能力。
验证清单
- 是否统一下发禁用策略;例外是否仅限受控来源并按需开启。

发表评论 取消回复