Common Implementation Patterns
Common Implementation Patterns
Progressive Web App (PWA) Configuration
// PWA with necessary permissions
app.use((req, res, next) => {
if (req.path.startsWith('/app')) {
// PWA needs certain features
res.setHeader('Permissions-Policy',
'geolocation=(self), ' +
'camera=(self), ' +
'microphone=(self), ' +
'notifications=(self), ' +
'push=(self), ' +
'sync-xhr=(), ' +
'magnetometer=(), ' +
'gyroscope=()'
);
} else {
// Restrictive policy for other pages
res.setHeader('Permissions-Policy',
'camera=(), microphone=(), geolocation=(), payment=()'
);
}
next();
});
Third-Party Widget Integration
// Secure third-party widget embedding
function embedThirdPartyWidget(widgetUrl, container, allowedFeatures = []) {
const iframe = document.createElement('iframe');
iframe.src = widgetUrl;
iframe.style.width = '100%';
iframe.style.height = '400px';
iframe.style.border = 'none';
// Set restrictive permissions by default
const permissions = {
'camera': "'none'",
'microphone': "'none'",
'geolocation': "'none'",
'payment': "'none'",
'usb': "'none'",
'bluetooth': "'none'"
};
// Allow specific features
allowedFeatures.forEach(feature => {
permissions[feature] = "'self'";
});
const allowAttribute = Object.entries(permissions)
.map(([k, v]) => `${k} ${v}`)
.join('; ');
iframe.setAttribute('allow', allowAttribute);
iframe.setAttribute('sandbox', 'allow-scripts allow-same-origin allow-forms');
container.appendChild(iframe);
}
// Usage
embedThirdPartyWidget(
'https://chat-widget.example.com',
document.getElementById('chat-container'),
['microphone', 'camera'] // Only allow necessary features
);