Pricing Blog

API call using custom action and Http only cookie

  • fe_rojas-1432399560604389416

    FeRojas

    1 month ago

    Hi!
    I've built a custom action to handle an API call that streams responses in a format the built-in API system doesn't parse correctly. The action fetches data, processes the streaming response, and passes it to a component—everything works perfectly.
    However, I've run into an issue: I can't use HTTP-only cookies for the authorization token with this custom action. Is there a known workaround for this, or an alternative approach I should consider?
    Thanks in advance for any guidance!
  • Tod-1432399562722771025

    Tod

    1 month ago

    Great job @FeRojas! Your contribution to the Nordcraft Community just made you advance to Community Level 7! 🌲
  • lucasg-1432404429675434128

    Lucas G

    1 month ago

    What format are you dealing with that isn't being parsed correctly?
  • None of the settings under the Advanced tab help? (parse as)
  • Custom actions (client-side JS) cannot access httpOnly cookies
  • fe_rojas-1432410646888317089

    FeRojas

    1 month ago

    Hi, is JSON lines from a n8n agent. Json stream work somehow, but generated error some times ("line x couldnt chunk/parse or something like that). It worked 95% of the time... but when it generate the error it produce other errors in other workflows. With custom action work perfectly and I have a lot of more control of how the parsing and streaming occurs.
  • lucasg-1432412698334859364

    Lucas G

    1 month ago

    I would create a bug report. Team toddle would likely want to take a look at this.
  • fe_rojas-1432415793794257080

    FeRojas

    1 month ago

    Oka, I'll report it then 👍
  • fe_rojas-1432418309399117854

    FeRojas

    1 month ago

    By the way, my question was intentend in the line of using equivalant to fetch(url, { credentials: 'include' }) , but it seems is not possible to customize how the cookie is send (or choose which cookie to send)
  • lucasg-1432419335661686946

    Lucas G

    1 month ago

    Like I mentioned earlier, you cannot send httpOnly cookies via client-side JS. That's the main point of storing them as httpOnly
  • To address it right now, I'd look at parsing/returning the full JSON for now instead of streaming it until you iron out what's causing certain chunks to error out
  • fe_rojas-1432444972656890018

    FeRojas

    1 month ago

    From my understanding, even though you cannot access the cookie content itself using javascript, you can instruct the browser to send the http-only-cookies with the fetch call. The only problem is that you cannot choose which cookie to send. But it seem that with cookie attributes like path/domain this could be potentially solve if the target cookie is for a specific domain. Im experimenting with that now...
  • fe_rojas-1432447965062238310

    FeRojas

    1 month ago

    My goal is to find a secure way to make client-side API calls to a webhook using a custom action. My current strategy is to, after the user's first message, generate a unique session JWT token on the server with Supabase that includes an expiration date and signed payload with specific attributes for the target endpoint. This token is saved in an HTTP-only cookie, sent with fetch calls using credentials: 'include', and validated on the server before processing the request. This protects against XSS attacks while enabling secure client-side API calls using custom action (and with all its flexibility and customization)
  • lucasg-1432448107290955858

    Lucas G

    1 month ago

    Yeah I guess that's more accurate to say. It's not that you cannot send httpOnly cookies, it's that you cannot read them via client-side JS. Good disctinction to make
  • The browser will send the cookies that fit the request URL, you'd have to verify things on the server though as you wouldn't be building an Auth Bearer header in that case
    👍1
  • fe_rojas-1432455501458964561

    FeRojas

    1 month ago

    Yes, not being able to build an Auth Bearer header is a limitation (and the one that inspire this thread). It seems that In practice you can only call endpoints where you can configure how the cookie is read and validated. But it does open some interesting possibilities be able to do it through a custom action.