4 * Guard writes against CSRF. The Aphront structure takes care of most of this
5 * for you, you just need to call:
7 * AphrontWriteGuard::willWrite();
9 * ...before executing a write against any new kind of storage engine. MySQL
10 * databases and the default file storage engines are already covered, but if
11 * you introduce new types of datastores make sure their writes are guarded. If
12 * you don't guard writes and make a mistake doing CSRF checks in a controller,
13 * a CSRF vulnerability can escape undetected.
15 * If you need to execute writes on a page which doesn't have CSRF tokens (for
16 * example, because you need to do logging), you can temporarily disable the
17 * write guard by calling:
19 * AphrontWriteGuard::beginUnguardedWrites();
21 * AphrontWriteGuard::endUnguardedWrites();
23 * This is dangerous, because it disables the backup layer of CSRF protection
24 * this class provides. You should need this only very, very rarely.
26 * @task protect Protecting Writes
27 * @task disable Disabling Protection
28 * @task manage Managing Write Guards
29 * @task internal Internals
31 final class AphrontWriteGuard
extends Phobject
{
33 private static $instance;
34 private static $allowUnguardedWrites = false;
37 private $allowDepth = 0;
40 /* -( Managing Write Guards )---------------------------------------------- */
44 * Construct a new write guard for a request. Only one write guard may be
45 * active at a time. You must explicitly call @{method:dispose} when you are
46 * done with a write guard:
48 * $guard = new AphrontWriteGuard($callback);
52 * Normally, you do not need to manage guards yourself -- the Aphront stack
55 * This class accepts a callback, which will be invoked when a write is
56 * attempted. The callback should validate the presence of a CSRF token in
57 * the request, or abort the request (e.g., by throwing an exception) if a
58 * valid token isn't present.
60 * @param callable CSRF callback.
64 public function __construct($callback) {
65 if (self
::$instance) {
68 'An %s already exists. Dispose of the previous guard '.
69 'before creating a new one.',
72 if (self
::$allowUnguardedWrites) {
75 'An %s is being created in a context which permits '.
76 'unguarded writes unconditionally. This is not allowed and '.
77 'indicates a serious error.',
80 $this->callback
= $callback;
81 self
::$instance = $this;
86 * Dispose of the active write guard. You must call this method when you are
87 * done with a write guard. You do not normally need to call this yourself.
92 public function dispose() {
93 if (!self
::$instance) {
94 throw new Exception(pht(
95 'Attempting to dispose of write guard, but no write guard is active!'));
98 if ($this->allowDepth
> 0) {
101 'Imbalanced %s: more %s calls than %s calls.',
103 'beginUnguardedWrites()',
104 'endUnguardedWrites()'));
106 self
::$instance = null;
111 * Determine if there is an active write guard.
116 public static function isGuardActive() {
117 return (bool)self
::$instance;
121 * Return on instance of AphrontWriteGuard if it's active, or null
123 * @return AphrontWriteGuard|null
125 public static function getInstance() {
126 return self
::$instance;
130 /* -( Protecting Writes )-------------------------------------------------- */
134 * Declare intention to perform a write, validating that writes are allowed.
135 * You should call this method before executing a write whenever you implement
136 * a new storage engine where information can be permanently kept.
138 * Writes are permitted if:
140 * - The request has valid CSRF tokens.
141 * - Unguarded writes have been temporarily enabled by a call to
142 * @{method:beginUnguardedWrites}.
143 * - All write guarding has been disabled with
144 * @{method:allowDangerousUnguardedWrites}.
146 * If none of these conditions are true, this method will throw and prevent
152 public static function willWrite() {
153 if (!self
::$instance) {
154 if (!self
::$allowUnguardedWrites) {
157 'Unguarded write! There must be an active %s to perform writes.',
160 // Unguarded writes are being allowed unconditionally.
165 $instance = self
::$instance;
166 if ($instance->allowDepth
== 0) {
167 call_user_func($instance->callback
);
172 /* -( Disabling Write Protection )----------------------------------------- */
176 * Enter a scope which permits unguarded writes. This works like
177 * @{method:beginUnguardedWrites} but returns an object which will end
178 * the unguarded write scope when its __destruct() method is called. This
179 * is useful to more easily handle exceptions correctly in unguarded write
182 * // Restores the guard even if do_logging() throws.
183 * function unguarded_scope() {
184 * $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
188 * @return AphrontScopedUnguardedWriteCapability Object which ends unguarded
189 * writes when it leaves scope.
192 public static function beginScopedUnguardedWrites() {
193 self
::beginUnguardedWrites();
194 return new AphrontScopedUnguardedWriteCapability();
199 * Begin a block which permits unguarded writes. You should use this very
200 * sparingly, and only for things like logging where CSRF is not a concern.
202 * You must pair every call to @{method:beginUnguardedWrites} with a call to
203 * @{method:endUnguardedWrites}:
205 * AphrontWriteGuard::beginUnguardedWrites();
207 * AphrontWriteGuard::endUnguardedWrites();
212 public static function beginUnguardedWrites() {
213 if (!self
::$instance) {
216 self
::$instance->allowDepth++
;
220 * Declare that you have finished performing unguarded writes. You must
221 * call this exactly once for each call to @{method:beginUnguardedWrites}.
226 public static function endUnguardedWrites() {
227 if (!self
::$instance) {
230 if (self
::$instance->allowDepth
<= 0) {
233 'Imbalanced %s: more %s calls than %s calls.',
235 'endUnguardedWrites()',
236 'beginUnguardedWrites()'));
238 self
::$instance->allowDepth
--;
243 * Allow execution of unguarded writes. This is ONLY appropriate for use in
244 * script contexts or other contexts where you are guaranteed to never be
245 * vulnerable to CSRF concerns. Calling this method is EXTREMELY DANGEROUS
246 * if you do not understand the consequences.
248 * If you need to perform unguarded writes on an otherwise guarded workflow
249 * which is vulnerable to CSRF, use @{method:beginUnguardedWrites}.
254 public static function allowDangerousUnguardedWrites($allow) {
255 if (self
::$instance) {
258 'You can not unconditionally disable %s by calling %s while a write '.
259 'guard is active. Use %s to temporarily allow unguarded writes.',
262 'beginUnguardedWrites()'));
264 self
::$allowUnguardedWrites = true;