Invalid CRF ,on attempt to Create a Subject via Rest API

96 views
Skip to first unread message

mozzy mutesa

unread,
Feb 17, 2020, 1:19:50 PM2/17/20
to xnat_discussion
Hello , am able ti retrieve resources via rest,

I attempetd to create a new empty subject as below using this endpoint    http://41.220.3.47/data/archive/projects/Test/subjects/1000 in PostMan
but i keep on the error below. Note that i can succesfully do Get requests

java.lang.Exception: INVALID CSRF (PUT on URL: http://41.220.3.47/data/archive/projects/Test/subjects/1000 from 102.84.92.135 (38974) user: 172.20.0.4
Headers:
host: 41.220.3.47
x-real-ip: 102.84.92.135
x-forwarded-host: 41.220.3.47
x-forwarded-server: 41.220.3.47
x-forwarded-for: 102.84.92.135
connection: close
content-length: 0
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36
cache-control: no-cache
authorization: Basic YWRtaW46YWRtaW4=
postman-token: 52910b05-2c8c-8380-94f1-22a6d0217f71
accept: */*
origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
accept-encoding: gzip, deflate
accept-language: en-US,en;q=0.9
cookie: JSESSIONID=56D0804F78E71EE0702E8D8E2290582F; SESSION_EXPIRATION_TIME="1581963158773,900000"

 Cookies:
JSESSIONID 56D0804F78E71EE0702E8D8E2290582F -1 null
SESSION_EXPIRATION_TIME 1581963158773,900000 -1 null

Herrick, Rick

unread,
Feb 17, 2020, 2:14:00 PM2/17/20
to xnat_di...@googlegroups.com

The issue is the user-agent header in the request:

 

user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36

 

XNAT checks requests and, if a request comes from an “interactive agent”–i.e. a browser or something similar–it tests that the CSRF token matches the token stored in the user’s login session. This is to prevent cross-site request forgeries by malicious scripts embedded in the browser or other sites or whatever. If the request does not come from an interactive agent–e.g. curl, httpie, or other command-line tools–XNAT doesn’t check for the CSRF token.

 

In your case, this request appears to XNAT to be coming from an interactive agent, specifically Chrome v80 on Windows 10, so it’s checking the CSRF token, not finding it, and letting you know.

 

There are three ways to solve this:

 

  • Change how you’re making that query to use a different user agent. If you use a command-line tool like curl or httpie, these already have different values for the user-agent header that won’t be considered as interactive agents. Httpie has the value “HTTPie/2.0.0”, so those requests don’t require the CSRF token. This is pretty easy to do in PostMan, so you can set something like “PostMan” as the user-agent header value.
  • Change the interactive agent IDs setting under Administer->Site Administration->Security->User Logins/Session Controls->Interactive Agent IDs. The default value for this is “.*MSIE.*,.*Mozilla.*,.*AppleWebKit.*,.*Opera.*”. It’s the “.*Mozilla.*” and “.*AppleWebKit.*” patterns that you’re matching. You probably don’t want to do this if your XNAT instance is accessible by anyone other than yourself, as removing Mozilla and AppleWebKit from the agent specifications leave you open to CSRF attacks from a wide variety of browsers.
  • Add the CSRF token to your request. You can get the current value from the source of one of the XNAT pages in your browser (look for “var csrfToken = 'xxx'”) or one of the referenced calls from an XNAT page (look for XNAT_CSRF=xxx on the request URL) and add that to the URL you’re calling, so your request below becomes http://41.220.3.47/data/archive/projects/Test/subjects/1000?XNAT_CSRF=xxx.

 

HTH

 

-- 

Rick Herrick

Sr. Programmer/Analyst

Neuroinformatics Research Group

Washington University School of Medicine

Phone: +1 (314) 273-1645

--
You received this message because you are subscribed to the Google Groups "xnat_discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xnat_discussi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xnat_discussion/e0c7e428-06ff-4f29-86ae-d6678a0c757e%40googlegroups.com.

 


The materials in this message are private and may contain Protected Healthcare Information or other information of a sensitive nature. If you are not the intended recipient, be advised that any unauthorized use, disclosure, copying or the taking of any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error, please immediately notify the sender via telephone or return mail.

Tashrif

unread,
Aug 20, 2021, 10:12:44 AM8/20/21
to xnat_discussion
Hi Rick,

None of the ways exists in Windows XNAT Dekstop Client!

Reply all
Reply to author
Forward
0 new messages