/    Sign up×
Community /Pin to ProfileBookmark

Design problem: displaying a file to an authorized user only

I need some assistance thinking about how to approach a problem of delivering data securely.

The data is a PDF file that resides on the server. My mission is to display it in a new tab or window when an authorized user requests access through the web site — but not allow anyone to load it directly by entering the file’s URL.

The approach I had in mind was to store the file with permissions set to 700, making it inaccessible through browsers. When the authorized user requests access, the site sets the permission to 704, allowing the user read-only access. It loads the file, then sets the permission back to 700. For a brief period the file is accessible to anyone, but it’s not so sensitive that that is a serious problem.

However, I’ve found that I have to load the file with an anchor tag that’s activated at load time by Javascript. (I’d rather not go into for reasons that beacuse they’re rather involved, and probably not relevant). That creates a timing problem, because the browser loads the PDF asynchronously with the server/browser interaction in the main window. Thus the server has no way of knowing when the PDF has finished loading, so it has no way of knowing when it’s safe to restore the file’s permissions.

I’ve thought about ways around this problem, but all of them have serious drawbacks. For example, the anchor tag could trigger a Javascript macro that sends a request to the server, which waits a few seconds for the PDF to load and then resets its permissions, but what if the server happens to be heavily loaded and the PDF doesn’t get loaded that fast? The delay could be too long for reasonable security and yet too short to work reliably.

Are there other ways I can approach this problem that will yield reasonably tight security without very elaborate coding?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@LaffinTooJan 29.2012 — And how do you validate users?
Copy linkTweet thisAlerts:
@NogDogJan 29.2012 — Typically this is done by using a file-server script, and storing the file in question in a directory inaccessible via the web. Then you can use a function such as readfile() to output it to a validated user, e.g.:
[code=php]
<?php
session_start();
if(!empty($_SESSION['valid_user'])) {
header("Content-Type: Application/PDF"); // or whatever correct MIME type is
readfile("../path/to/file.pdf");
exit; // make sure no extraneous bytes get sent once you're done
}
<html><head><title>Error</title></head><body>
<p>Sorry, you must sign up first</p>
</body></html>
[/code]
Copy linkTweet thisAlerts:
@ssystemsJan 29.2012 — And how do you validate users?[/QUOTE]

I think this is a more relevant question. NogDog's approach would be great if you are using a database based authentication. If you are using ldap, nis, etc the approach would be different perhaps much simpler. For ldap,nis just set the permisssion of the file to that user.
Copy linkTweet thisAlerts:
@jhsachsauthorJan 29.2012 — Thank you, NogDog, that's exactly what I was looking for.

The "how do you validate?" question seems to be based on a misunderstanding of what I meant by "validate." "Authorize" would have been a more accurate term.

To answer the question, I validate the user by confirming that she has paid for the service that the PDF file represents. That information comes from a session variable, which is set by preceding business logic if the user paid in the current session, or is retrieved from the database if she paid in an earlier one.
×

Success!

Help @jhsachs spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.10,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...