Simon Willison’s Weblog: GitHub OAuth for a static site using Cloudflare Workers

Source URL: https://simonwillison.net/2024/Nov/29/github-oauth-cloudflare/
Source: Simon Willison’s Weblog
Title: GitHub OAuth for a static site using Cloudflare Workers

Feedly Summary: GitHub OAuth for a static site using Cloudflare Workers
Here’s a TIL covering a Thanksgiving AI-assisted programming project. I wanted to add OAuth against GitHub to some of the projects on my tools.simonwillison.net site in order to implement “Save to Gist".
That site is entirely statically hosted by GitHub Pages, but OAuth has a required server-side component: there’s a client_secret involved that should never be included in client-side code.
Since I serve the site from behind Cloudflare I realized that a minimal Cloudflare Workers script may be enough to plug the gap. I got Claude on my phone to build me a prototype and then pasted that (still on my phone) into a new Cloudflare Worker and it worked!
… almost. On later closer inspection of the code it was missing error handling… and then someone pointed out it was vulnerable to a login CSRF attack thanks to failure to check the state= parameter. I worked with Claude to fix those too.
Useful reminder here that pasting code AI-generated code around on a mobile phone isn’t necessarily the best environment to encourage a thorough code review!
Tags: ai, oauth, llms, security, tools, generative-ai, ai-assisted-programming, projects, cloudflare, github

AI Summary and Description: Yes

Summary: The text discusses a project involving the implementation of OAuth authentication for a static site hosted on GitHub Pages, utilizing Cloudflare Workers to manage the necessary server-side components. It highlights the importance of security practices, particularly in the context of AI-generated code.

Detailed Description: The provided text outlines a practical programming project that incorporates OAuth authentication to enhance the functionality of a website. The key points include:

– **Project Context**: The author is working on a site that allows users to save content to Gist on GitHub, which necessitates implementing OAuth for user authentication.

– **Technical Challenge**: As the site is statically hosted on GitHub Pages, the author encountered a challenge: OAuth involves server-side components (like client_secret) that should not be exposed in client-side code.

– **Solution via Cloudflare Workers**: To address this issue, the author devised a solution using Cloudflare Workers, enabling the execution of server-side logic while maintaining the static nature of the site.

– **AI Assistance**: The author utilized the AI tool Claude to quickly prototype the required solution, demonstrating the potential of AI in accelerating programming tasks.

– **Security Oversight**: Upon further review, security vulnerabilities were identified:
– Lack of error handling in the initial prototype.
– A vulnerability to a login Cross-Site Request Forgery (CSRF) attack due to failure to verify the `state=` parameter.

– **Lessons Learned**: The text emphasizes the importance of careful code review, especially when dealing with AI-generated code in less-than-ideal environments (like a mobile phone).

Key Insights for Security and Compliance Professionals:
– **OAuth Implementation Importance**: Understanding OAuth principles is crucial when dealing with authentication for web applications to protect against common vulnerabilities.
– **Cloudflare Workers as a Security Measure**: Utilizing edge computing solutions like Cloudflare Workers can help mitigate challenges when building statically hosted applications, particularly regarding server-side logic and security.
– **AI’s Role in Development**: While AI can enhance productivity and speed, reliance on it for critical security aspects underscores the necessity for thorough human oversight in the code review process.
– **Awareness of Security Vulnerabilities**: The case illustrates the need for developers to be vigilant about potential vulnerabilities, especially when using automated tools for coding.

Overall, the narrative serves as a reminder to security professionals of the evolving landscape where AI and cloud solutions play an integral role in modern application development, while also highlighting the persistent need for vigilant security practices.