<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Custom API endpoint returning CSRF error on post? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/Custom-API-endpoint-returning-CSRF-error-on-post/m-p/484595#M82979</link>
    <description>&lt;P&gt;Hello, I am trying to get a custom API endpoint to work, but I am getting CSRF errors when posting any data to it:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;401 (Splunk cannot authenticate the request. CSRF validation failed.)&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;My endpoint looks like this (my_app/bin/test.py):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;#!/usr/bin/python

import os
import csv
import splunk

class Echo(splunk.rest.BaseRestHandler):
   def handle_GET(self):
       self.response.setStatus(200)
       self.response.write('session: ' + self.sessionKey + '\n')

       for key, value in self.request["headers"].iteritems():
           self.response.write(key + ': ' + value + '\n')

   handle_POST = handle_GET
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Splunk restmap.conf (my_app/default/restmap.conf):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;[script:echo]
match=/echo
handler=test.Echo
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Splunk web.conf (my_app/default/web.conf):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;[expose:echo]
pattern=echo
methods=GET,POST
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've tried communicating with the API two different ways:&lt;/P&gt;
&lt;P&gt;Over port 8000 (POST not working)&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/7704i7A395E1A30190D7E/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Over port 8089 (POST and GET working)&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/7705i2DB705A618C74012/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;My log (/opt/splunk/var/log/splunk/splunkd.log) keeps saying this: &lt;CODE&gt;ERROR UiAuth - Request from xxx.xxx.xxx.xxx to "/en-US/splunkd/__raw/services/echo" failed CSRF validation -- expected "5038769918656995927", but instead cookie had "5038769918656995927" and form parameter had ""&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;What I've tried&lt;/STRONG&gt;:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Adding &lt;CODE&gt;skipCSRFProtection=1&lt;/CODE&gt; to the endpoints config in &lt;CODE&gt;web.conf&lt;/CODE&gt; (documented feature) but it seems to have zero effect.&lt;/LI&gt;
&lt;LI&gt;Cleared my cookies for the domain, as detailed in this splunk answers question: &lt;CODE&gt;answers.splunk.com/answers/581168/splunk-cannot-authenticate-the-request-csrf-valida.html&lt;/CODE&gt;&lt;/LI&gt;
&lt;LI&gt;Tried manually adding &lt;CODE&gt;X-Splunk-Form-Key&lt;/CODE&gt; as a header, as suggested by this splunk answers question: &lt;CODE&gt;answers.splunk.com/answers/661095/post-to-splunkd-raw-endpoint-returns-csrf-validati.html&lt;/CODE&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;It feels like the session cookies are not being transmitted properly, but that doesn't seem correct, given the headers received in the GET request example attached.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;What I need:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;I need to be able to communicate with the web API (authenticated) on port 8000 for GET and POST requests. I am trying to make requests in my custom dashboard:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;const service = mvc.createService({
  owner: 'username here'
});

service.post(
  '/services/echo',
  JSON.stringify({ my: data }),
  function(err, response) {
    // what ever
  }
);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Other Info:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;I'm running splunk in docker, using the 7.1.2 tag.&lt;/P&gt;</description>
    <pubDate>Wed, 19 Oct 2022 16:20:08 GMT</pubDate>
    <dc:creator>zackurben</dc:creator>
    <dc:date>2022-10-19T16:20:08Z</dc:date>
    <item>
      <title>Custom API endpoint returning CSRF error on post?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Custom-API-endpoint-returning-CSRF-error-on-post/m-p/484595#M82979</link>
      <description>&lt;P&gt;Hello, I am trying to get a custom API endpoint to work, but I am getting CSRF errors when posting any data to it:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;401 (Splunk cannot authenticate the request. CSRF validation failed.)&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;My endpoint looks like this (my_app/bin/test.py):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;#!/usr/bin/python

import os
import csv
import splunk

class Echo(splunk.rest.BaseRestHandler):
   def handle_GET(self):
       self.response.setStatus(200)
       self.response.write('session: ' + self.sessionKey + '\n')

       for key, value in self.request["headers"].iteritems():
           self.response.write(key + ': ' + value + '\n')

   handle_POST = handle_GET
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Splunk restmap.conf (my_app/default/restmap.conf):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;[script:echo]
match=/echo
handler=test.Echo
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Splunk web.conf (my_app/default/web.conf):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;[expose:echo]
pattern=echo
methods=GET,POST
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've tried communicating with the API two different ways:&lt;/P&gt;
&lt;P&gt;Over port 8000 (POST not working)&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/7704i7A395E1A30190D7E/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Over port 8089 (POST and GET working)&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/7705i2DB705A618C74012/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;My log (/opt/splunk/var/log/splunk/splunkd.log) keeps saying this: &lt;CODE&gt;ERROR UiAuth - Request from xxx.xxx.xxx.xxx to "/en-US/splunkd/__raw/services/echo" failed CSRF validation -- expected "5038769918656995927", but instead cookie had "5038769918656995927" and form parameter had ""&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;What I've tried&lt;/STRONG&gt;:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Adding &lt;CODE&gt;skipCSRFProtection=1&lt;/CODE&gt; to the endpoints config in &lt;CODE&gt;web.conf&lt;/CODE&gt; (documented feature) but it seems to have zero effect.&lt;/LI&gt;
&lt;LI&gt;Cleared my cookies for the domain, as detailed in this splunk answers question: &lt;CODE&gt;answers.splunk.com/answers/581168/splunk-cannot-authenticate-the-request-csrf-valida.html&lt;/CODE&gt;&lt;/LI&gt;
&lt;LI&gt;Tried manually adding &lt;CODE&gt;X-Splunk-Form-Key&lt;/CODE&gt; as a header, as suggested by this splunk answers question: &lt;CODE&gt;answers.splunk.com/answers/661095/post-to-splunkd-raw-endpoint-returns-csrf-validati.html&lt;/CODE&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;It feels like the session cookies are not being transmitted properly, but that doesn't seem correct, given the headers received in the GET request example attached.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;What I need:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;I need to be able to communicate with the web API (authenticated) on port 8000 for GET and POST requests. I am trying to make requests in my custom dashboard:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;const service = mvc.createService({
  owner: 'username here'
});

service.post(
  '/services/echo',
  JSON.stringify({ my: data }),
  function(err, response) {
    // what ever
  }
);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Other Info:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;I'm running splunk in docker, using the 7.1.2 tag.&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2022 16:20:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Custom-API-endpoint-returning-CSRF-error-on-post/m-p/484595#M82979</guid>
      <dc:creator>zackurben</dc:creator>
      <dc:date>2022-10-19T16:20:08Z</dc:date>
    </item>
    <item>
      <title>Re: Custom API endpoint returning CSRF error on post</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Custom-API-endpoint-returning-CSRF-error-on-post/m-p/484596#M82980</link>
      <description>&lt;P&gt;You may need to use, curl with options, like '-L' and headers, X-Splunk-Form-Key and X-Requested-With: XMLHttpRequest..&lt;BR /&gt;
Here's the article for it,  &lt;A href="https://docs.splunk.com/Documentation/StreamApp/7.1.3/DeployStreamApp/SplunkAppforStreamRESTAPI" target="_blank"&gt;https://docs.splunk.com/Documentation/StreamApp/7.1.3/DeployStreamApp/SplunkAppforStreamRESTAPI&lt;/A&gt; &lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;$ curl -Lk -u admin &lt;A href="http://localhost:8000/en-GB/splunkd/__raw/services/myRESTCall" target="_blank"&gt;http://localhost:8000/en-GB/splunkd/__raw/services/myRESTCall&lt;/A&gt;  -H 'Cookie: splunkd_8000=QHASm07YxvF4C8642PFHMRAS0PxRXLie7THbp261otk3pTmWJ2QVn4twqheZM^3_KZ6DDPzenJYszELi8VwvJPrKyJrK_hM75x45XjuzknW1Y7CGH6J4Kp6wakjj9MNAWF; splunkweb_csrf_token_8000=5089449155758749034; session_id_8000=b970b2c4c0b38eb917d980c5b6384f5152a8ad72; login=; splunkweb_uid=' -H 'X-Splunk-Form-Key: 5089449155758749034' -H 'X-Requested-With: XMLHttpRequest'  -X POST&lt;/STRONG&gt;&lt;BR /&gt;
Enter host password for user 'admin':&lt;/P&gt;

&lt;P&gt;&lt;EM&gt;session: PIPsP72nJ7Ha4Z_KEypI4TKtkSiIUQqlqkYmLYVp6PweuwSMRUF0hgHxPx8p_L1GDQQ7UpyU2t8Tk9QQm9fjDrAIIIEioNl5nquFPB1NYwD4Pn9MKWBmBpUwJ12kdxVMksZGfOxkqQ0&lt;BR /&gt;
host: 127.0.0.1:8089&lt;BR /&gt;
  cookie: splunkd_8000=QHASm07YxvF4C8642PFHMRAS0PxRXLie7THbp261otk3pTmWJ2QVn4twqheZM^3_KZ6DDPzenJYszELi8VwvJPrKyJrK_hM75x45XjuzknW1Y7CGH6J4Kp6wakjj9MNAWF;     splunkweb_csrf_token_8000=5089449155758749034; session_id_8000=b970b2c4c0b38eb917d980c5b6384f5152a8ad72; login=; splunkweb_uid=&lt;BR /&gt;
  x-requested-with: XMLHttpRequest&lt;BR /&gt;
  user-agent: curl/7.54.0&lt;BR /&gt;
  authorization: Basic YWRtaW46V2VsY29tZTA=&lt;BR /&gt;
  accept: */&lt;/EM&gt;&lt;BR /&gt;
  x-splunk-form-key: 5089449155758749034* &lt;/P&gt;

&lt;P&gt;Please make sure to include those headers for your testing.&lt;BR /&gt;
&lt;IMG src="https://community.splunk.com/storage/temp/274882-csrf-post.png" alt="alt text" /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 02:26:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Custom-API-endpoint-returning-CSRF-error-on-post/m-p/484596#M82980</guid>
      <dc:creator>sylim_splunk</dc:creator>
      <dc:date>2020-09-30T02:26:22Z</dc:date>
    </item>
    <item>
      <title>Re: Custom API endpoint returning CSRF error on post</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/Custom-API-endpoint-returning-CSRF-error-on-post/m-p/617720#M106636</link>
      <description>&lt;P&gt;Could you please farther explain? I looked at the browser devtools and it seems that the csrf token is already added. Why is this happening only on post requests and not on get requests?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Oct 2022 16:00:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/Custom-API-endpoint-returning-CSRF-error-on-post/m-p/617720#M106636</guid>
      <dc:creator>eliav2</dc:creator>
      <dc:date>2022-10-19T16:00:30Z</dc:date>
    </item>
  </channel>
</rss>

