Skip to main content

Salesforce Files and public links: ContentDistribution exposure

A Salesforce public link turns a private file into a URL that anyone can open without logging in. That is exactly what a sales rep wants when sending a quote, and exactly what an attacker wants when they find an old link in a forwarded email, a support ticket or a search engine. The file stays reachable for as long as the link exists, whoever holds it.

Most orgs have no idea how many of these links exist. They are easy to create, rarely expire unless someone set an expiry date, and are invisible from the file owner's point of view once the email has gone. This post covers what a public link is in the data model, how to inventory them, how Apex creates them, and the settings that keep the next ones safe.

Every public link and every content delivery is a ContentDistribution record pointing at a specific file version. The record holds the public URL, the download URL and the link's preferences: whether it needs a password, whether it expires and when, and whether the viewer may download the original. Deleting the record revokes the link.

Three things decide whether links can be created at all:

Control Where Effect
Content deliveries and public links Setup, Content Deliveries and Public Links Org-level switches. Public links require content deliveries to be on
Create Public Links User permission on a profile or permission set Who can create a public link to a file they can share
Library permissions Library permission definition (Deliver Content) Who can create links for files in a shared library

Since Winter '24, the person creating a public link in Lightning Experience can set an expiration date and a password, and admins choose the password default on the same Content Deliveries and Public Links page. Links created before that, links created by integrations and links to folders, which support neither password nor expiry, are the usual gaps.

Start with the links that need nothing to open and have no end date:

SELECT Id, Name, ContentDocumentId, CreatedBy.Name, CreatedDate,
       PreferencesAllowOriginalDownload, ViewCount, LastViewDate
FROM ContentDistribution
WHERE PreferencesPasswordRequired = false
  AND PreferencesExpires = false
ORDER BY CreatedDate DESC

Then answer three questions for the result:

  1. What is in the file? Join ContentDocumentId to ContentDocument for the title and file type. Contracts, exports, ID scans and spreadsheets matter more than marketing PDFs.
  2. Who created it? A long list from one user usually means a workflow, an integration or an Apex class creating links automatically.
  3. Is it still used? ViewCount and LastViewDate show whether anyone opens it. A link with views from last week to a file nobody meant to publish is an incident, not a hygiene item.

For a large org, export and review offline:

sf data query --target-org prod --result-format csv \
  --query "SELECT Id, Name, ContentDocumentId, CreatedById, CreatedDate, ViewCount, LastViewDate FROM ContentDistribution WHERE PreferencesPasswordRequired = false AND PreferencesExpires = false" \
  > open-links.csv

Revoking is deleting the ContentDistribution row, from the file's sharing settings, from the Content Deliveries and Public Links list, or in bulk with sf data delete bulk --sobject ContentDistribution --file ids.csv --target-org prod. Tell the business first: anything that embedded the URL will break, which is the point.

The riskiest links are not made by people. They are made by code, often a convenience method behind a button:

public without sharing class ShareFileController {
    @AuraEnabled
    public static String publicUrl(Id contentVersionId) {
        ContentDistribution cd = new ContentDistribution(
            Name = 'Shared file',
            ContentVersionId = contentVersionId,
            PreferencesAllowViewInBrowser = true,
            PreferencesAllowOriginalDownload = true
        );
        insert cd;
        return [SELECT DistributionPublicUrl FROM ContentDistribution
                WHERE Id = :cd.Id].DistributionPublicUrl;
    }
}

This method lets any user who can reach the component name any file version in the org and receive a link that works for anyone. without sharing removes the file sharing check, the caller chooses the ID, and the link has no password and no expiry. It is a file download service for the whole org.

A safer version checks access in user mode, limits the link and returns the password with it:

public with sharing class ShareFileController {
    @AuraEnabled
    public static Map<String, String> publicUrl(Id contentVersionId) {
        ContentVersion cv = [SELECT Id, Title FROM ContentVersion
                             WHERE Id = :contentVersionId WITH USER_MODE];
        ContentDistribution cd = new ContentDistribution(
            Name = cv.Title,
            ContentVersionId = cv.Id,
            PreferencesAllowViewInBrowser = true,
            PreferencesAllowOriginalDownload = false,
            PreferencesPasswordRequired = true,
            PreferencesExpires = true,
            ExpiryDate = System.now().addDays(14)
        );
        insert as user cd;
        ContentDistribution saved = [SELECT DistributionPublicUrl, Password
                                     FROM ContentDistribution WHERE Id = :cd.Id];
        return new Map<String, String>{
            'url' => saved.DistributionPublicUrl, 'password' => saved.Password
        };
    }
}

If the method is only for certain users, gate it with a custom permission as well, so the button and the server agree.

Experience Cloud files

Sites add two more ways files leave the org:

  • Guest file access. A Network with enableGuestFileAccess set to true lets unauthenticated visitors read files shared with the site. One file shared to the wrong group becomes public.
  • File visibility on records. A file linked to a record with ContentDocumentLink.Visibility of AllUsers is visible to external users who can see the record. Files attached by internal users to cases or accounts that customers can view are the common surprise. Query ContentDocumentLink for the objects your site exposes and check the visibility value.

Review both alongside the guest profile, because a guest who can read a record and a file linked to it with AllUsers visibility can read the file.

  • Grant Create Public Links only to roles that send files to outsiders, not to every internal profile.
  • Set the org default so password protection is on when a link is created.
  • Train senders to set an expiry date, and choose short ones for anything sensitive.
  • For automated links, put the expiry and password in code, as above, and review every class that inserts ContentDistribution.
  • If you have Event Monitoring, the Content Distribution event type records link activity. A user creating many links in a short time deserves a look.
  • Re-run the inventory query each quarter and track the count of open links. It should go down.

Where Vulkro for Salesforce fits

vulkro-sf org governance -t prod reports SF-FILE-LINK-001 with the count and names of public links that need no password and have no expiry date, read with a SELECT and nothing changed. vulkro-sf org event-monitoring -t prod includes SF-EVENT-MON-020, a burst of public file shares by one user, on orgs with Event Monitoring. In source, vulkro-sf scan flags network-guest-file-access on a site that opens files to guests, and its risk rating treats an entry point that publishes an unrestricted link to a caller-chosen file, like the first example above, as a file leaving the org.

FAQ

Delete the ContentDistribution record, from the file's sharing settings or in bulk. The URL stops working immediately. Anyone who downloaded the file already keeps their copy.

Only if an expiry date was set when it was created. Lightning Experience offers expiry and password options, but older links, links made by code and folder links often have neither. Query PreferencesExpires to find out.

Can guest users of an Experience Cloud site see internal files?

They can see files shared with the site when guest file access is on, and files linked to records they can read with AllUsers visibility. Check both.

See the org governance command and the guest user exposure use case, or start a 14-day trial. Related: Experience Cloud guest user security checklist and anatomy of a guest user data leak.