ring 2024-01-17

having trouble using wrap-resource properly and safely! What is the default location in the project tree for file I want served? How do I prevent accidentally serving source files? What I have is a straightforward cljs frontent/ring backend web app with my handler wrapped in (wrap-resource "") Hmm.. seems I’m not supposed to do that. https://clojurians.slack.com/archives/C0A5GSC6T/p1502385145943556

@weavejester do you want me to do so?

Update the docstring you mean?

Imma be real, at this point I prefer just making a route for each individual resource

(defn sitemap-xml-handler
  [request]
  {:status  200
   :headers {"Content-Type" "application/xml"}
   :body    (slurp (io/resource "/public/sitemap.xml"))})

["/public/sitemap.xml"
  {:get {:handler sitemap-xml-handler}}]

more code? yes

"I accidentally gave access to my entire classpath including .class files"? No

Potentially I could update the docstring with a warning about making every resource on your classpath public. Or add a warning/precondition in there, as I don't think there's ever a legitimate reason for making your entire classpath public.

A downside to a precondition is that it will break apps. That sounds "good", but not every app is exposed to the public internet. Exposing your entire classpath over http is usually bad, but there are always contexts where "who cares"

I forget the exact args for wrap-resource, but generally you give it a prefix like "public/" and then it will only serve resources from under <classpath root>/public/

a lot of tooling will try and put different things in different directories so you'll have a resources/ directory in the project, so resources/public/ would be the place to put things if you use "public" as the prefix. it all just ends up thrown on the classpath which is why not including a prefix will make it able to serve up source files

Okay!

Thanks, I’m up and running. This seemed less intuitive/less well-documented than it might be, perhaps because I’m not accustomed to java builds and terminology like classpath/resource path. Also seems to be a little bit of friction with shadow-cljs having public as the default/typical build location.

not sure about the shadow-cljs, but assuming you want to serve up the js that is the result of the build then it make sort of make sense

it would just be better if it were resources/public or something

one dirlevel deep so you can use (wrap-resource "public")

but also, I find it strange that it is so easy to end up serving your whole src dir. I am just now realizing I’ve been doing that on anothe project

Hi all - apologies if I'm overlooking something obvious, but what is the idiomatic way to turn off the stacktraces produced by jetty for 500 errors and replace them with something more generic in production? I found some Java incantations but I wanted to make sure I wasn't missing something easy in ring/`run-jetty`.

Replace the handler you pass to run-jetty with (fn [req] (try (handler req) (catch Throwable e your-error-response-here)))

Ah, perfectly simple. Thank you!

Hey folks, I’m digging into https://developer.mozilla.org/en-US/docs/Web/Privacy/Partitioned_cookies It’s one of the https://developers.google.com/privacy-sandbox/3pcd to deal with Google phasing out 3rd party cookies. However, I found that it isn’t a supported key https://ring-clojure.github.io/ring/ring.middleware.cookies.html#var-cookies-response. Has anyone found a way to work with partitioned cookies? Google has started the 3rd party cookie phase out as of the https://developers.google.com/privacy-sandbox/blog/cookie-countdown-2024jan Thanks all!

I'll add support for it in the next Ring version, along with a more flexible way of adding cookie attributes. In the meantime, you could always set the Set-Cookie header directly. Or write middleware to automatically add Partitioned to existing cookies.

🙌 1
✅ 1