Skip to content

Commit

Permalink
Merge pull request #15171 from schlawg/hcaptcha-credentialless
Browse files Browse the repository at this point in the history
targeted hcaptcha credentialless
  • Loading branch information
ornicar committed May 1, 2024
2 parents 9bae2a2 + ae1cb32 commit 560b079
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/views/auth/bits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object bits:
views.base.layout(
title = trans.site.passwordReset.txt(),
moreCss = cssTag("auth"),
moreJs = hcaptchaScript(form),
modules = hcaptchaScript(form),
csp = defaultCsp.withHcaptcha.some
):
main(cls := "auth auth-signup box box-pad")(
Expand Down Expand Up @@ -104,7 +104,7 @@ object bits:
views.base.layout(
title = "Log in by email",
moreCss = cssTag("auth"),
moreJs = hcaptchaScript(form),
modules = hcaptchaScript(form),
csp = defaultCsp.withHcaptcha.some
):
main(cls := "auth auth-signup box box-pad")(
Expand Down
4 changes: 2 additions & 2 deletions app/views/auth/signup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ object signup:
def apply(form: lila.core.security.HcaptchaForm[?])(using ctx: PageContext) =
views.base.layout(
title = trans.site.signUp.txt(),
modules = jsModuleInit("bits.login", "signup"),
moreJs = frag(hcaptchaScript(form), fingerprintTag),
modules = jsModuleInit("bits.login", "signup") ++ hcaptchaScript(form),
moreJs = frag(fingerprintTag),
moreCss = cssTag("auth"),
csp = defaultCsp.withHcaptcha.some,
withHrefLangs = lila.ui.LangPath(routes.Auth.signup).some
Expand Down
2 changes: 1 addition & 1 deletion modules/clas/src/main/ui/ClasPages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ final class ClasPages(helpers: Helpers, clasUi: ClasUi, dashUi: DashboardUi):

def create(form: lila.core.security.HcaptchaForm[ClasForm.ClasData])(using Context) =
ClasPage(trans.clas.newClass.txt(), Right("newClass"))(cls := "box-pad")
.iife(hcaptchaScript(form))
.js(hcaptchaScript(form))
.csp(_.withHcaptcha):
frag(
h1(cls := "box__top")(trans.clas.newClass()),
Expand Down
2 changes: 1 addition & 1 deletion modules/pref/src/main/ui/AccountPages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ final class AccountPages(helpers: Helpers, ui: AccountUi, flagApi: lila.core.use
def form(form: lila.core.security.HcaptchaForm[?], error: Option[String] = None)(using ctx: Context) =
Page(trans.site.reopenYourAccount.txt())
.cssTag("auth")
.iife(hcaptchaScript(form))
.js(hcaptchaScript(form))
.csp(_.withHcaptcha):
main(cls := "page-small box box-pad")(
h1(cls := "box__top")(trans.site.reopenYourAccount()),
Expand Down
4 changes: 2 additions & 2 deletions modules/ui/src/main/helper/AssetHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ trait AssetHelper:

def flairSrc(flair: Flair): String = staticAssetUrl(s"$flairVersion/flair/img/$flair.webp")

def hcaptchaScript(re: lila.core.security.HcaptchaForm[?]): Option[RawFrag] =
re.enabled.option(raw("""<script src="https://hcaptcha.com/1/api.js" async defer></script>"""))
def hcaptchaScript(re: lila.core.security.HcaptchaForm[?]): EsmList =
re.enabled.so(jsModuleInit("bits.hcaptcha"))

def analyseNvuiTag(using ctx: Context) = ctx.blind.option(EsmInit("analyse.nvui"))
1 change: 1 addition & 0 deletions ui/bits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"src/bits.flatpickr.ts",
"src/bits.forum.ts",
"src/bits.gameSearch.ts",
"src/bits.hcaptcha.ts",
"src/bits.infiniteScroll.ts",
"src/bits.login.ts",
"src/bits.lpv.ts",
Expand Down
19 changes: 19 additions & 0 deletions ui/bits/src/bits.hcaptcha.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function initModule() {
const script = document.createElement('script');
script.src = 'https://hcaptcha.com/1/api.js';

if ('credentialless' in window && window.crossOriginIsolated) {
const documentCreateElement = document.createElement;
script.src = 'https://hcaptcha.com/1/api.js?onload=initHcaptcha';
script.onload = () => {
document.createElement = function () {
const element = documentCreateElement.apply(this, arguments as any);
if (element instanceof HTMLIFrameElement) element.setAttribute('credentialless', '');
return element;
};
};
(window as any).initHcaptcha = () => (document.createElement = documentCreateElement);
}

document.head.appendChild(script);
}

0 comments on commit 560b079

Please sign in to comment.