See our current docs
You want to limit access to certain uploaded files, such as PDFs, to website users who have specifically been given permission to view those files.
By default, files uploaded to your Apostrophe site can be accessed by anyone who knows the URL... which is why the URLs are unguessable. This is a common approach for high-traffic services. It allows media to be served as fast as possible by static webservers. And it still protects that media from being discovered by search engines, until someone shares that link in public.
This compromise works well for most sites, but it is not acceptable for Intranet sites. And that's why you tracked this module down.
All attachments are delivered a little bit slower, including attachments that have public permissions. That's because Apostrophe must get involved in serving each one. It's not terrible, but there's an impact. So just bear that in mind, and don't use this module on a super-high-traffic site that doesn't really need it.
This module supports configurations where Apostrophe's media is served from S3 rather than local files, with the following contraints:
- Apostrophe's static assets will still be served, but they won't be copied to S3. The provided implementation is compatible with
APOS_BUNDLE=1bundle extraction behavior. They will be served via Apostrophe and/or nginxtry_filesrather than the S3 bucket. - As of this writing, only S3 is currently supported for this, not Azure or Google Cloud Storage. Interested parties may wish to contribute implementations of the
streamOutmethod to those storage adapters in uploadfs. - Streaming media from S3 in this way imposes a more significant performance penalty than streaming from local files. Apostrophe must first check the attachment permissions, then open a connection to S3, then stream the data to itself and pipe it onwards to the browser.
NOTE: node 18 or better is required for this module.
npm install apostrophe-secure-attachments
This module only works with the local storage backend of uploadfs.
This is the default way attachments are stored in Apostrophe. You cannot
use this module with the s3 or azure storage backends.
"Why not?" Those services are basically static webservers for your media. That defeats the purpose of using this module.
Please note that limiting access to a URL doesn't prevent an authorized user from saving a file to their hard drive, then sharing that file directly. All this module does is prevent link sharing. However, it is still a useful tool to keep sensitive documents from being indexed accidentally after being inadvertently linked once on a public page, etc.
Just turn it on in app.js:
module.exports = {
// in app.js
const apos = require('apostrophe')({
modules: {
'apostrophe-secure-attachments': {}
}
});
};-
Move
public/uploadstodata/secure-uploads. -
Your
datafolder should already be a shared, persistent folder and if you are using our stagecoach deployment system, then it is. But if you're not... make very sure this folder is shared and persistent between deployments. Otherwise, you'll lose your uploads every time you deploy. -
That's all.
Legacy URLs like
/uploads/attachments/yyy-some-file.pdfwill be automatically redirected to/secure-uploads/attachments/yyy-some-file.pdf.
Just click on "Images" or "Files" in your admin bar, browse to the file of interest, click to edit, and select the Permissions tab. You can set up view permissions just as you would for a page, including options like "Login Required" and "Certain People." The latter can be locked down both by user and by group.
If you have added attachment schema fields directly to your own pages
or pieces, just set the view permissions for those pages and pieces.
For pages, this option is always available via "Page Settings." For
pieces, it becomes available in the editor if you set permissionsFields: true as
an option to your pieces module, like this:
// lib/modules/products/index.js
module.exports = {
extend: 'apostrophe-pieces',
name: 'product',
// Enables view permissions on a per-piece basis
permissionsFields: true,
addFields: [
{
type: 'attachment',
group: 'office',
name: 'resume'
}
]
};By default users just see a "forbidden" message. This is secure, but you might want a friendlier approach.
If you do, pass a custom handleForbidden function when configuring apostrophe-attachments:
// in lib/modules/apostrophe-attachments/index.js
module.exports = {
// self is the apostrophe-attachments module object, in case you need it
handleForbidden(self, req) {
// This is the default behavior, change it in your override
req.res.status(403).send('forbidden');
}
}You can access the URL the user originally wanted via req.url.