This repository was archived by the owner on May 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathIntegerAccessReferenceMap.php
More file actions
executable file
·247 lines (213 loc) · 6.53 KB
/
IntegerAccessReferenceMap.php
File metadata and controls
executable file
·247 lines (213 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/**
* OWASP Enterprise Security API (ESAPI)
*
* This file is part of the Open Web Application Security Project (OWASP)
* Enterprise Security API (ESAPI) project. For details, please see
* <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
*
* Copyright (c) 2007 - 2011 The OWASP Foundation
*
* The ESAPI is published by OWASP under the BSD license. You should read and accept the
* LICENSE before you use, modify, and/or redistribute this software.
*
* @author Andrew van der Stock
* @created 2009
* @since 1.6
* @package ESAPI_Reference
*/
require_once dirname(__FILE__).'/../AccessReferenceMap.php';
require_once dirname(__FILE__).'/../StringUtilities.php';
/**
* Reference Implementation of the IntegerAccessReferenceMap interface.
*
* @category OWASP
* @package ESAPI_Reference
* @copyright 2009-2010 The OWASP Foundation
* @license http://www.opensource.org/licenses/bsd-license.php New BSD license
* @version Release: @package_version@
* @link http://www.owasp.org/index.php/ESAPI
*/
class IntegerAccessReferenceMap implements AccessReferenceMap {
private $dtoi = null;
private $itod = null;
private $count = 1;
function __construct($directReferences = null)
{
$this->dtoi = new ArrayObject();
$this->itod = new ArrayObject();
if ( !empty($directReferences) )
{
$this->update($directReferences);
}
}
/**
* Get an iterator through the direct object references. No guarantee is made as
* to the order of items returned.
*
* @return the iterator
*/
function iterator()
{
return $this->dtoi->getIterator();
}
/**
* Get a safe indirect reference to use in place of a potentially sensitive
* direct object reference. Developers should use this call when building
* URL's, form fields, hidden fields, etc... to help protect their private
* implementation information.
*
* @param directReference
* the direct reference
*
* @return
* the indirect reference
*/
function getIndirectReference($direct)
{
if ( empty($direct) )
{
return null;
}
$hash = $this->getHash($direct);
if ( !($this->dtoi->offsetExists($hash)) )
{
return null;
}
return $this->dtoi->offsetGet($hash);
}
/**
* Get the original direct object reference from an indirect reference.
* Developers should use this when they get an indirect reference from a
* request to translate it back into the real direct reference. If an
* invalid indirect reference is requested, then an AccessControlException is
* thrown.
*
* @param indirectReference
* the indirect reference
*
* @return
* the direct reference
*
* @throws AccessControlException
* if no direct reference exists for the specified indirect reference
*/
function getDirectReference($indirectReference)
{
if (!empty($indirectReference) && $this->itod->offsetExists($indirectReference) )
{
return $this->itod->offsetGet($indirectReference);
}
throw new AccessControlException("Access denied", "Request for invalid indirect reference: " + $indirectReference);
return null;
}
/**
* Adds a direct reference to the AccessReferenceMap, then generates and returns
* an associated indirect reference.
*
* @param direct
* the direct reference
*
* @return
* the corresponding indirect reference
*/
function addDirectReference($direct)
{
if ( empty($direct) )
{
return null;
}
$hash = $this->getHash($direct);
if ( $this->dtoi->offsetExists($hash) )
{
return $this->dtoi->offsetGet($hash);
}
$indirect = $this->getUniqueReference();
$this->itod->offsetSet($indirect, $direct);
$this->dtoi->offsetSet($hash, $indirect);
return $indirect;
}
/**
* Create a new random reference that is guaranteed to be unique.
*
* @return
* a random reference that is guaranteed to be unique
*/
function getUniqueReference() {
return "".$this->count++;
}
function getHash($direct)
{
if ( empty($direct) )
{
return null;
}
$hash = hexdec(substr(md5(serialize($direct)), -7));
return $hash;
}
/**
* Removes a direct reference and its associated indirect reference from the AccessReferenceMap.
*
* @param direct
* the direct reference to remove
*
* @return
* the corresponding indirect reference
*
* @throws AccessControlException
*/
function removeDirectReference($direct)
{
if ( empty($direct) ) {
return null;
}
$hash = $this->getHash($direct);
if ( $this->dtoi->offsetExists($hash) ) {
$indirect = $this->dtoi->offsetGet($hash);
$this->itod->offsetUnset($indirect);
$this->dtoi->offsetUnset($hash);
return $indirect;
}
return null;
}
/**
* Updates the access reference map with a new set of direct references, maintaining
* any existing indirect references associated with items that are in the new list.
* New indirect references could be generated every time, but that
* might mess up anything that previously used an indirect reference, such
* as a URL parameter.
*
* @param directReferences
* a Set of direct references to add
*/
function update($directReferences)
{
$dtoi_old = clone $this->dtoi;
unset($this->dtoi);
unset($this->itod);
$this->dtoi = new ArrayObject();
$this->itod = new ArrayObject();
$dir = new ArrayObject($directReferences);
$directIterator = $dir->getIterator();
while ($directIterator->valid())
{
$indirect = null;
$direct = $directIterator->current();
$hash = $this->getHash($direct);
// Try to get the old direct object reference (if it exists)
// otherwise, create a new entry
if (!empty($direct) && $dtoi_old->offsetExists($hash) )
{
$indirect = $dtoi_old->offsetGet($hash);
}
if ( empty($indirect) )
{
$indirect = $this->getUniqueReference();
}
$this->itod->offsetSet($indirect, $direct);
$this->dtoi->offsetSet($hash, $indirect);
$directIterator->next();
}
}
}
?>