/    Sign up×
Community /Pin to ProfileBookmark

Using Database IDs in URLs

Are there security risks by using database IDs as URL parameter values?

EX. bob.com/accounts.php?nameid=10, where 10 represents the ‘names’ table primary key for a certain persons name record.

I appreciate any insight into this,

bubbisthedog

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYJan 28.2007 — Sure there is, what does accounts.php do?
Copy linkTweet thisAlerts:
@bubbisthedogauthorJan 28.2007 — Thanks for the reply. And it's disturbing that you said 'yes' that quick. :eek:

That was just an example to clarify what I meant, but let's say that the account.php file contains PHP that performs the following query:

SELECT fname, lname, address
FROM NAMES
WHERE nameid = $_GET['nameid']


which would translate to

SELECT fname, lname, address
FROM NAMES
WHERE nameid = 10


and I display the fname, lname and address on the screen.

There's a security risk doing it that way?

Thanks.
Copy linkTweet thisAlerts:
@NightShift58Jan 28.2007 — SELECT fname, lname, address
FROM NAMES
WHERE nameid = $_GET['nameid']
[/QUOTE]
Well, somehow, the information has to be passed from one script to another and there no real way to do so with absolute stealth.

However, the way you are taking a value directly from the URL and applying it to a query could lead to problems.

You always to sanitize or neutralize such user input. Instead of just typing a "10", a malevolent user could write "10' and 'blabla", making your statement think that that's what it's supposed to do.

In this particular case, as you are expecting a record ID, an integer, you could, prior to executing the query, do:[code=php]<?php
$recID = intval($_GET['nameid']);
$sql = "SELECT fname, lname, address FROM NAMES WHERE nameid = $recID";
?>[/code]
In case you are expecting something other than an integer, use mysql_real_escape_string() to make sure any strange characters are neutralized prior to executing a query.
Copy linkTweet thisAlerts:
@bubbisthedogauthorJan 28.2007 — See, that's why I come here. ? Excellent advice, NightShift.

Thankfully, when I enter 'blah,' as one my parameter values, all that is returned is 'Unknown column 'blah' in 'where clause,' which I would not [i]think[/i] is a 'vulnerable' response, right? The rest of the page simply does not display when that error occurs.

I will nonetheless use intval(); I should be doing that anyway.

Thanks for great advice, NightShift,

bubbisthedog
Copy linkTweet thisAlerts:
@NightShift58Jan 29.2007 — You're welcome!
×

Success!

Help @bubbisthedog 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.20,
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,
)...