nuxt-security icon indicating copy to clipboard operation
nuxt-security copied to clipboard

CSRF

Open moshetanzer opened this issue 1 year ago • 16 comments

Hi,

Thanks again for your great library.

Having this issue wonder if you could point me in the right direction.

I have CSRF protection enabled for my login page. My app is hosted on Vercel.

Seems to be that the first time out customer comes back to app, the CSRF always fails and requires a page reload for it to work.

It is an internal app only used I would say weekly. Could it be something with vercel's Serverless architecture. Seems strange.

Any suggestions?

Latest version nuxt-security: nuxt:

moshetanzer avatar Jun 15 '24 22:06 moshetanzer

That is an interesting question @Morgbn would you have some ideas about it?

Baroshem avatar Jun 16 '24 14:06 Baroshem

Have disabled for now... would love a solution

moshetanzer avatar Jun 17 '24 17:06 moshetanzer

Also been a pain since when you disable csurf in nuxt config. Whole app breaks since car is not found...

moshetanzer avatar Jun 17 '24 17:06 moshetanzer

I also just noticed this issue with a form. I am hosting our Nuxt site on Azure Static Web Apps.

Not sure what the cause could be, but if I have more info, I will share here.

christie-hill-za avatar Jun 19 '24 07:06 christie-hill-za

Don't have answer now. I would love to get feedback from @Morgbn about it as he knows the stuff :)

Baroshem avatar Jun 19 '24 11:06 Baroshem

Hi,

I think that maybe a warning should be put on the website (even thought it is by default disabled) that CSRF doesnt work properly with serverless functions.

moshetanzer avatar Jun 19 '24 11:06 moshetanzer

Hello, I recently noticed the same thing on a project hosted on Cloudfare I need to investigate, I'll get back to you as soon as I know more!

Morgbn avatar Jun 19 '24 11:06 Morgbn

Issue is simple. Functions should be considered starless due to their cold state nature. Which means any solution using the same method cannot work. We have to change this CSRF to the Double submit cookie pattern.

UPDATE: Think it will be better and easier to just go with the Origin method. @Baroshem should we get the host from a ENV? Or Host or X-forwards host. Issue is that I think x forwarded is disabled by default?

If I have a bit of time 😇 will open a PR.

moshetanzer avatar Jun 19 '24 20:06 moshetanzer

Hey @Morgbn any findings in your testing? :)

Baroshem avatar Jul 04 '24 05:07 Baroshem

Hey @Baroshem,

Sorry i havent managed to do a PR, might be quicker if you want just to add this as csrfOrigin so that the token can still be used for those not hosting on serverless.

Found this the other day it is from the oslo/request package. It use a safe header. Only issue with it is one of the version of internet explorer which i am not convinced is such a big issue.

This method is suggested also in the owasp in defence in deep" - https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#using-standard-headers-to-verify-origin

export function verifyRequestOrigin(origin: string, allowedDomains: string[]): boolean {
	if (!origin || allowedDomains.length === 0) return false;
	const originHost = safeURL(origin)?.host ?? null;
	if (!originHost) return false;
	for (const domain of allowedDomains) {
		let host: string | null;
		if (domain.startsWith("http://") || domain.startsWith("https://")) {
			host = safeURL(domain)?.host ?? null;
		} else {
			host = safeURL("https://" + domain)?.host ?? null;
		}
		if (originHost === host) return true;
	}
	return false;
}

function safeURL(url: URL | string): URL | null {
	try {
		return new URL(url);
	} catch {
		return null;
	}
}

moshetanzer avatar Jul 04 '24 05:07 moshetanzer

Any updates on this?

moshetanzer avatar Jul 11 '24 10:07 moshetanzer

Still waiting for the feedback from @Morgbn :)

Baroshem avatar Jul 11 '24 11:07 Baroshem

Wasn't suggesting to remove his package. Rather just add a this as 'corsOrigin'

moshetanzer avatar Jul 11 '24 22:07 moshetanzer

@moshetanzer yeah, I understand. I am just asking for the opinion and guidance from the author of the nuxt-csurf package as this technically could be implemented there :)

@Morgbn let me know if you found something

Baroshem avatar Jul 16 '24 06:07 Baroshem

Congrats on major!

Hey @Baroshem seems like a pity to ship major security package without a working CSRF protection. Why don’t we just add in the meantime my above suggestion as csrf-origin or as a type on csrf.

moshetanzer avatar Sep 19 '24 15:09 moshetanzer

Tagging @Morgbn for opinion :)

Baroshem avatar Sep 19 '24 15:09 Baroshem

I believe I am having the same issue Does seem to be more frequent on local development HMR seems to cause a CSRF token mismatch every time and requires a reload

BritLuey avatar Nov 03 '24 22:11 BritLuey

I recently updated nuxt-csurf, it might solve this issue.

Morgbn avatar Nov 04 '24 07:11 Morgbn

I will patch it in the next version and see if it makes a difference then :)

Baroshem avatar Nov 04 '24 08:11 Baroshem

Does some try the fix, I'm not sure to understand how to set up the encryptSecret?

Barbapapazes avatar Nov 20 '24 15:11 Barbapapazes