Phabricator AphrontRequest / Save Error Fix

By  on  

Working on Phabricator extensions has thrust me back into the world of PHP, a language I had only touched via WordPress plugins and blog themes over the past few years.  Despite being away from the language, I was able to jump back in fairly quickly and felt a small "back at home" comfort...until I realized Phabricator was not greatly documented and that I'd need to dive deep into the source code to figure out how to do just about every task.

One error that baffled me for a while was the following:

You are trying to save some data to Phabricator, but the request your browser made included an incorrect token. Reload the page and try again. You may need to clear your cookies.

I was trying to create and save a PhabricatorAuthTemporaryToken instance for the sake of allowing login via a third party service, and the request from that service to Phabricator was made in the background, so there was no PHCID because there was no session, thus the CSRF token was invalid.  Essentially I needed a way to write to Phabricator without a CSRF validation (via PHCID) step.  The solution was simple:

// Turn off CSRF check
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
// Save token
id(new PhabricatorAuthTemporaryToken())
  ->setTokenResource($transaction_code)
  // ...
  ->save();
// Turn CSRF check back on
unset($unguarded);

AphrontWriteGuard::beginScopedUnguardedWrites() allowed me to turn of the CSRF check, save the token, and then turn the CSRF check back on via unset.  Short amount of code but took a long time to find!

I love that advanced libraries like Phabricator insist on these security checks without developers having to do so explicitly; I also love that turning off this check momentarily is easy.  I'll be sure to share more PHP / Phabricator tips as I run into problems!

Recent Features

  • By
    How to Create a RetroPie on Raspberry Pi – Graphical Guide

    Today we get to play amazing games on our super powered game consoles, PCs, VR headsets, and even mobile devices.  While I enjoy playing new games these days, I do long for the retro gaming systems I had when I was a kid: the original Nintendo...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    Sexy Album Art with MooTools or jQuery

    The way that album information displays is usually insanely boring. Music is supposed to be fun and moving, right? Luckily MooTools and jQuery allow us to communicate that creativity on the web. The XHTML A few structure DIVs and the album information. The CSS The CSS...

  • By
    Simple Image Lazy Load and Fade

    One of the quickest and easiest website performance optimizations is decreasing image loading.  That means a variety of things, including minifying images with tools like ImageOptim and TinyPNG, using data URIs and sprites, and lazy loading images.  It's a bit jarring when you're lazy loading images and they just...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!