diff --git a/Classes/SecondFactor/Controller/ModuleController.php b/Classes/SecondFactor/Controller/ModuleController.php index 21aeb05..d78ad25 100644 --- a/Classes/SecondFactor/Controller/ModuleController.php +++ b/Classes/SecondFactor/Controller/ModuleController.php @@ -25,11 +25,18 @@ use Sandstorm\BackendUserManagement\SecondFactor\Service\SecondFactorSessionStorageService; use Sandstorm\BackendUserManagement\SecondFactor\Service\TOTPService; -/** - * @Flow\Scope("singleton") - */ class ModuleController extends AbstractModuleController { + /** + * Read-only actions. All others change state and are POST only: Flow skips the CSRF check for GET requests + * ({@see \Neos\Flow\Security\RequestPattern\CsrfProtection::matchRequest()}), and module actions are reachable by + * GET - via the last URI segment ({@see \Neos\Neos\Routing\BackendModuleRoutePartHandler}) or `moduleArguments[@action]`. + * Without this, a forged GET could e.g. register an attacker-known TOTP secret for the victim (createAction). + * Same class of issue as GHSA-944w-5hjq-x6vh, whose core fix does not cover third-party modules. + * @var string[] + */ + private const array SAFE_ACTIONS = ['index', 'new', 'newTotp', 'newWebAuthn']; + /** * @var SecondFactorRepository * @Flow\Inject @@ -92,6 +99,17 @@ class ModuleController extends AbstractModuleController */ protected $passwordlessLoginEnabled = false; + protected function initializeAction(): void + { + parent::initializeAction(); + if (!in_array($this->request->getControllerActionName(), self::SAFE_ACTIONS, true) + && $this->request->getHttpRequest()->getMethod() !== 'POST' + ) { + $this->response->setHttpHeader('Allow', 'POST'); + $this->throwStatus(405); + } + } + /** * used to list all second factors of the current user */ diff --git a/Tests/E2E/features/default/backend-module.feature b/Tests/E2E/features/default/backend-module.feature index 97d2ed3..7fe4004 100644 --- a/Tests/E2E/features/default/backend-module.feature +++ b/Tests/E2E/features/default/backend-module.feature @@ -65,3 +65,20 @@ Feature: Backend module for two-factor authentication management with default se And I navigate to the 2FA management page And I remove the 2FA device with the name "Editor Test Device" Then There should be no 2FA device with the name "Editor Test Device" + + # CSRF (GHSA-944w-5hjq-x6vh): Flow skips the CSRF check for GET - a forged link must not change state + Scenario: A forged GET request cannot register a 2FA device + When I log in with username "editor" and password "password" + And I send a forged GET request that registers a TOTP 2FA device with name "Attacker Device" + Then the forged request should be rejected as "Method Not Allowed" + When I navigate to the 2FA management page + Then There should be 0 enrolled 2FA devices + + Scenario: A forged GET request cannot remove a 2FA device + When I log in with username "admin" and password "password" + And I navigate to the 2FA management page + And I add a new TOTP 2FA device with name "Admin Test Device" + And I send a forged GET request that removes the 2FA device with the name "Admin Test Device" + Then the forged request should be rejected as "Method Not Allowed" + When I navigate to the 2FA management page + Then There should be a 2FA device with the name "Admin Test Device" diff --git a/Tests/E2E/features/user-management/module.feature b/Tests/E2E/features/user-management/module.feature index b7fde5d..26d34da 100644 --- a/Tests/E2E/features/user-management/module.feature +++ b/Tests/E2E/features/user-management/module.feature @@ -47,3 +47,10 @@ Feature: User management backend module And I open the user "jane" in the user management module And I click "send-reset-mail" in the user details Then "jane@example.com" should receive 1 mail + + # CSRF (GHSA-944w-5hjq-x6vh): Flow skips the CSRF check for GET - a forged link must not change state + Scenario: A forged GET request cannot delete a user + When I log in with username "admin" and password "password" + And I send a forged GET request that deletes the user "jane" in the user management module + Then the forged request should be rejected as "Method Not Allowed" + And the user list should contain "jane" diff --git a/Tests/E2E/package-lock.json b/Tests/E2E/package-lock.json index 2bf36dd..b93e6a9 100644 --- a/Tests/E2E/package-lock.json +++ b/Tests/E2E/package-lock.json @@ -6,14 +6,14 @@ "": { "name": "sandstorm-backendusermanagement-e2e", "dependencies": { - "@playwright/test": "^1.58.2", - "otplib": "^13.4.0" + "@playwright/test": "^1.65.1", + "otplib": "^13.6.0" }, "devDependencies": { - "@types/node": "^25.5.0", + "@types/node": "^25.9.9", "playwright-bdd": "^9.2.1", - "prettier": "^3.8.4", - "typescript": "^6.0.2" + "prettier": "^3.10.1", + "typescript": "^6.0.3" } }, "node_modules/@colors/colors": { @@ -150,9 +150,9 @@ "license": "MIT" }, "node_modules/@noble/hashes": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-2.0.1.tgz", - "integrity": "sha512-XlOlEbQcE9fmuXxrVTXCTlG2nlRXa9Rj3rr5Ue/+tX+nmkgbX720YHh0VR3hBF9xDvwnb8D2shVGOwNx+ulArw==", + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-2.4.0.tgz", + "integrity": "sha512-X5XaVWZIBCT7HHZGm5I7ZQXDwLG+bGXuSrMQAW+7Zvl87h1kmc1ZB1VSRJcpUfoUrGQp4Fkoxm5kZ+Ms+aW+eA==", "license": "MIT", "engines": { "node": ">= 20.19.0" @@ -162,80 +162,80 @@ } }, "node_modules/@otplib/core": { - "version": "13.4.0", - "resolved": "https://registry.npmjs.org/@otplib/core/-/core-13.4.0.tgz", - "integrity": "sha512-JqOGcvZQi2wIkEQo8f3/iAjstavpXy6gouIDMHygjNuH6Q0FjbHOiXMdcE94RwfgDNMABhzwUmvaPsxvgm9NYw==", + "version": "13.6.0", + "resolved": "https://registry.npmjs.org/@otplib/core/-/core-13.6.0.tgz", + "integrity": "sha512-TL6/FKKzR55Vr7zo3YGFtPMCSV3SpYPtuvE3T1XcwjFczD9pHU4dYYQBl0AcNvGcqAt4/fPqhZEFfB2OBcnlPA==", "license": "MIT" }, "node_modules/@otplib/hotp": { - "version": "13.4.0", - "resolved": "https://registry.npmjs.org/@otplib/hotp/-/hotp-13.4.0.tgz", - "integrity": "sha512-MJjE0x06mn2ptymz5qZmQveb+vWFuaIftqE0b5/TZZqUOK7l97cV8lRTmid5BpAQMwJDNLW6RnYxGeCRiNdekw==", + "version": "13.6.0", + "resolved": "https://registry.npmjs.org/@otplib/hotp/-/hotp-13.6.0.tgz", + "integrity": "sha512-oPpaQBkAYjBxAs+FWdGNumsEwWJhIZVcsv0vEi+w4OzzLQ6UDZ2qXZNSeF9NljTj6/8d4e1Z4o6RgJa3aIxAOA==", "license": "MIT", "dependencies": { - "@otplib/core": "13.4.0", - "@otplib/uri": "13.4.0" + "@otplib/core": "13.6.0", + "@otplib/uri": "13.6.0" } }, "node_modules/@otplib/plugin-base32-scure": { - "version": "13.4.0", - "resolved": "https://registry.npmjs.org/@otplib/plugin-base32-scure/-/plugin-base32-scure-13.4.0.tgz", - "integrity": "sha512-/t9YWJmMbB8bF5z8mXrBZc2FXBe8B/3hG5FhWr9K8cFwFhyxScbPysmZe8s1UTzSA6N+s8Uv8aIfCtVXPNjJWw==", + "version": "13.6.0", + "resolved": "https://registry.npmjs.org/@otplib/plugin-base32-scure/-/plugin-base32-scure-13.6.0.tgz", + "integrity": "sha512-s6uMbj4MBebvAPnBns1y2RhQ765/U04qHlZmFLV02dlD1S06pU46g/7tHX0vkCx5Pnty723loXto/PM5gFEEdg==", "license": "MIT", "dependencies": { - "@otplib/core": "13.4.0", - "@scure/base": "^2.0.0" + "@otplib/core": "13.6.0", + "@scure/base": "^2.4.0" } }, "node_modules/@otplib/plugin-crypto-noble": { - "version": "13.4.0", - "resolved": "https://registry.npmjs.org/@otplib/plugin-crypto-noble/-/plugin-crypto-noble-13.4.0.tgz", - "integrity": "sha512-KrvE4m7Zv+TT1944HzgqFJWJpKb6AyoxDbvhPStmBqdMlv5Gekb80d66cuFRL08kkPgJ5gXUSb5SFpYeB+bACg==", + "version": "13.6.0", + "resolved": "https://registry.npmjs.org/@otplib/plugin-crypto-noble/-/plugin-crypto-noble-13.6.0.tgz", + "integrity": "sha512-m/8mOhtIhsr4zLtb9MlLI6+w7ZNiKSFnT8Dv7eHR9TCyf2Sxva5e+MbV0wONmF7ZLuvo9F3DtzfPjc748PhJcw==", "license": "MIT", "dependencies": { - "@noble/hashes": "^2.0.1", - "@otplib/core": "13.4.0" + "@noble/hashes": "^2.4.0", + "@otplib/core": "13.6.0" } }, "node_modules/@otplib/totp": { - "version": "13.4.0", - "resolved": "https://registry.npmjs.org/@otplib/totp/-/totp-13.4.0.tgz", - "integrity": "sha512-dK+vl0f0ekzf6mCENRI9AKS2NJUC7OjI3+X8e7QSnhQ2WM7I+i4PGpb3QxKi5hxjTtwVuoZwXR2CFtXdcRtNdQ==", + "version": "13.6.0", + "resolved": "https://registry.npmjs.org/@otplib/totp/-/totp-13.6.0.tgz", + "integrity": "sha512-bMlq0MaUaQE3lOSH352Mrzcwpau5pMf7pMLROS+llk2evpC/sE7SHl9ajBl+PiPnGGb6LGYEjDvKqX8caXkGdw==", "license": "MIT", "dependencies": { - "@otplib/core": "13.4.0", - "@otplib/hotp": "13.4.0", - "@otplib/uri": "13.4.0" + "@otplib/core": "13.6.0", + "@otplib/hotp": "13.6.0", + "@otplib/uri": "13.6.0" } }, "node_modules/@otplib/uri": { - "version": "13.4.0", - "resolved": "https://registry.npmjs.org/@otplib/uri/-/uri-13.4.0.tgz", - "integrity": "sha512-x1ozBa5bPbdZCrrTL/HK21qchiK7jYElTu+0ft22abeEhiLYgH1+SIULvOcVk3CK8YwF4kdcidvkq4ciejucJA==", + "version": "13.6.0", + "resolved": "https://registry.npmjs.org/@otplib/uri/-/uri-13.6.0.tgz", + "integrity": "sha512-CiLFMONTpCi3ara6yQQX3nlpYhafCALKJweA7+7Z+aDkRrW3QmD5a/9thKmQ0cGp2W70AMG/6BSTy82h74LHRw==", "license": "MIT", "dependencies": { - "@otplib/core": "13.4.0" + "@otplib/core": "13.6.0" } }, "node_modules/@playwright/test": { - "version": "1.58.2", - "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.58.2.tgz", - "integrity": "sha512-akea+6bHYBBfA9uQqSYmlJXn61cTa+jbO87xVLCWbTqbWadRVmhxlXATaOjOgcBaWU4ePo0wB41KMFv3o35IXA==", + "version": "1.65.1", + "resolved": "https://registry.npmjs.org/@playwright/test/-/test-1.65.1.tgz", + "integrity": "sha512-y+NCIRm2LvY4slUVWvGh1nujJ8RF/hEisIsq2mtUPgdQC4L701bq5hZGMhrpeXjACvwvO9tj5dJqhvM6TP9lbw==", "license": "Apache-2.0", "dependencies": { - "playwright": "1.58.2" + "playwright": "1.65.1" }, "bin": { "playwright": "cli.js" }, "engines": { - "node": ">=18" + "node": ">=20" } }, "node_modules/@scure/base": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@scure/base/-/base-2.0.0.tgz", - "integrity": "sha512-3E1kpuZginKkek01ovG8krQ0Z44E3DHPjc5S2rjJw9lZn3KSQOs8S7wqikF/AH7iRanHypj85uGyxk0XAyC37w==", + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/@scure/base/-/base-2.4.0.tgz", + "integrity": "sha512-thZ1TuJwFwBblOhgsjDKvvGirBxNp+wSvY/DR6tJBJOTDhdAAcHJ8Vbr2eFnqaxeca4+t0i9KBf+uHYGWwZORg==", "license": "MIT", "funding": { "url": "https://paulmillr.com/funding/" @@ -252,13 +252,13 @@ } }, "node_modules/@types/node": { - "version": "25.5.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-25.5.0.tgz", - "integrity": "sha512-jp2P3tQMSxWugkCUKLRPVUpGaL5MVFwF8RDuSRztfwgN1wmqJeMSbKlnEtQqU8UrhTmzEmZdu2I6v2dpp7XIxw==", + "version": "25.9.9", + "resolved": "https://registry.npmjs.org/@types/node/-/node-25.9.9.tgz", + "integrity": "sha512-b4e2xxj/yMeT2hNlD6x7zNFiQK2Dlmkb9CQ6v3/1G2RezKN4YS2lcEv8lzGm1mhs4f2lM4yNAMI/zUDBZBBt7w==", "dev": true, "license": "MIT", "dependencies": { - "undici-types": "~7.18.0" + "undici-types": ">=7.24.0 <7.24.7" } }, "node_modules/ansi-regex": { @@ -336,20 +336,6 @@ } } }, - "node_modules/fsevents": { - "version": "2.3.2", - "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", - "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", - "hasInstallScript": true, - "license": "MIT", - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": "^8.16.0 || ^10.6.0 || >=11.0.0" - } - }, "node_modules/is-fullwidth-code-point": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", @@ -405,23 +391,23 @@ } }, "node_modules/otplib": { - "version": "13.4.0", - "resolved": "https://registry.npmjs.org/otplib/-/otplib-13.4.0.tgz", - "integrity": "sha512-RUcYcRMCgRWhUE/XabRppXpUwCwaWBNHe5iPXhdvP8wwDGpGpsIf/kxX/ec3zFsOaM1Oq8lEhUqDwk6W7DHkwg==", + "version": "13.6.0", + "resolved": "https://registry.npmjs.org/otplib/-/otplib-13.6.0.tgz", + "integrity": "sha512-PCx6+14PYndCRk0G3JuJHmlExzENs43gafqMUVmSFmzMbEweVhBiYcBCL8B32ZMEDPZA4OsMxFEeAob1H+YFZg==", "license": "MIT", "dependencies": { - "@otplib/core": "13.4.0", - "@otplib/hotp": "13.4.0", - "@otplib/plugin-base32-scure": "13.4.0", - "@otplib/plugin-crypto-noble": "13.4.0", - "@otplib/totp": "13.4.0", - "@otplib/uri": "13.4.0" + "@otplib/core": "13.6.0", + "@otplib/hotp": "13.6.0", + "@otplib/plugin-base32-scure": "13.6.0", + "@otplib/plugin-crypto-noble": "13.6.0", + "@otplib/totp": "13.6.0", + "@otplib/uri": "13.6.0" } }, "node_modules/picomatch": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.5.tgz", - "integrity": "sha512-RvwwcruNjI1ncT5xRakeyS9Lf8lcItv34KD+aif+VH9kduAyfYBipGh12274xtenIPZ119/R9BdTBa8gAwSh0A==", + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.7.tgz", + "integrity": "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA==", "dev": true, "license": "MIT", "engines": { @@ -432,21 +418,18 @@ } }, "node_modules/playwright": { - "version": "1.58.2", - "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.58.2.tgz", - "integrity": "sha512-vA30H8Nvkq/cPBnNw4Q8TWz1EJyqgpuinBcHET0YVJVFldr8JDNiU9LaWAE1KqSkRYazuaBhTpB5ZzShOezQ6A==", + "version": "1.65.1", + "resolved": "https://registry.npmjs.org/playwright/-/playwright-1.65.1.tgz", + "integrity": "sha512-vJ1FuoOrNwOKHUIVEvSt3jT1L+wjjDpwhwIstKtfvTh8XIG4FG4bIFuvNdMxi81LGfAhUgUZleVFaOLY4RIUUw==", "license": "Apache-2.0", "dependencies": { - "playwright-core": "1.58.2" + "playwright-core": "1.65.1" }, "bin": { "playwright": "cli.js" }, "engines": { - "node": ">=18" - }, - "optionalDependencies": { - "fsevents": "2.3.2" + "node": ">=20" } }, "node_modules/playwright-bdd": { @@ -484,21 +467,21 @@ } }, "node_modules/playwright-core": { - "version": "1.58.2", - "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.58.2.tgz", - "integrity": "sha512-yZkEtftgwS8CsfYo7nm0KE8jsvm6i/PTgVtB8DL726wNf6H2IMsDuxCpJj59KDaxCtSnrWan2AeDqM7JBaultg==", + "version": "1.65.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.65.1.tgz", + "integrity": "sha512-hqPpz7blSMPxWVdoeE1yiBSzXsKiVOd2k+kjCvmUywD2GPO2Dd9HfcWnq9NN0GHbNZDFgZ9JfBFr+X5/BLo0qw==", "license": "Apache-2.0", "bin": { "playwright-core": "cli.js" }, "engines": { - "node": ">=18" + "node": ">=20" } }, "node_modules/prettier": { - "version": "3.8.4", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.8.4.tgz", - "integrity": "sha512-N2MylSdi48+5N/6S5j+maeHbUSIzzZ5uOcX5Hm4QpV8Dkb1HFjfAKTKX6yNPJQD9AhcT3ifHNB66tWTTJDi11Q==", + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.10.1.tgz", + "integrity": "sha512-70vp3ZUYoPlFbF9uiTdXHUUCGEIp7d4+6p+8DYbkm2jP7x/3i/paXVF6dopSI3zd/CEitrZN1rc4i85xWmmXlg==", "dev": true, "license": "MIT", "bin": { @@ -605,9 +588,9 @@ } }, "node_modules/typescript": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.2.tgz", - "integrity": "sha512-bGdAIrZ0wiGDo5l8c++HWtbaNCWTS4UTv7RaTH/ThVIgjkveJt83m74bBHMJkuCbslY8ixgLBVZJIOiQlQTjfQ==", + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.3.tgz", + "integrity": "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==", "dev": true, "license": "Apache-2.0", "bin": { @@ -619,9 +602,9 @@ } }, "node_modules/undici-types": { - "version": "7.18.2", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.18.2.tgz", - "integrity": "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==", + "version": "7.24.6", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.24.6.tgz", + "integrity": "sha512-WRNW+sJgj5OBN4/0JpHFqtqzhpbnV0GuB+OozA9gCL7a993SmU+1JBZCzLNxYsbMfIeDL+lTsphD5jN5N+n0zg==", "dev": true, "license": "MIT" }, diff --git a/Tests/E2E/package.json b/Tests/E2E/package.json index 8064894..dd3ec33 100644 --- a/Tests/E2E/package.json +++ b/Tests/E2E/package.json @@ -22,13 +22,13 @@ "test:neos9:username-is-email": "npm run generate-tests && SUT=neos9 FLOW_CONTEXT=Production/E2E-SUT/UserManagement/UsernameIsEmail npx playwright test --grep @username-is-email" }, "devDependencies": { - "@types/node": "^25.5.0", + "@types/node": "^25.9.9", "playwright-bdd": "^9.2.1", - "prettier": "^3.8.4", - "typescript": "^6.0.2" + "prettier": "^3.10.1", + "typescript": "^6.0.3" }, "dependencies": { - "@playwright/test": "^1.58.2", - "otplib": "^13.4.0" + "@playwright/test": "^1.65.1", + "otplib": "^13.6.0" } } diff --git a/Tests/E2E/steps/csrf.steps.ts b/Tests/E2E/steps/csrf.steps.ts new file mode 100644 index 0000000..ebdc4d9 --- /dev/null +++ b/Tests/E2E/steps/csrf.steps.ts @@ -0,0 +1,74 @@ +import { expect, type Page } from '@playwright/test'; +import { createBdd } from 'playwright-bdd'; +import { BackendModulePage } from '../helpers/2fa-pages.ts'; +import { generateOtp } from '../helpers/totp.ts'; + +const { When, Then } = createBdd(); + +// A forged link (e.g. an on a foreign page) is a plain GET with the victim's session cookie - no CSRF token. +// Flow skips the CSRF check for GET, so state-changing module actions must reject it (GHSA-944w-5hjq-x6vh). +// page.request shares the browser context's cookies, i.e. the logged-in session. + +const TWO_FACTOR_MODULE_URL = '/neos/management/twoFactorAuthentication'; +const USER_MANAGEMENT_MODULE_URL = '/neos/administration/userManagement'; + +// attacker-known TOTP secret (base32) +const ATTACKER_TOTP_SECRET = 'JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP'; + +let lastForgedResponse: { status: number; body: string } | undefined; + +async function sendForgedGet(page: Page, url: string): Promise { + const response = await page.request.get(url, { maxRedirects: 0 }); + lastForgedResponse = { status: response.status(), body: await response.text() }; +} + +When( + 'I send a forged GET request that registers a TOTP 2FA device with name {string}', + async ({ page }, deviceName: string) => { + const query = new URLSearchParams({ + 'moduleArguments[secret]': ATTACKER_TOTP_SECRET, + 'moduleArguments[secondFactorFromApp]': generateOtp(ATTACKER_TOTP_SECRET), + 'moduleArguments[name]': deviceName, + }); + await sendForgedGet(page, `${TWO_FACTOR_MODULE_URL}/create?${query}`); + }, +); + +When( + 'I send a forged GET request that removes the 2FA device with the name {string}', + async ({ page }, deviceName: string) => { + const modulePage = new BackendModulePage(page); + await modulePage.goto(); + // the delete form's action URI carries the factor identity - replay it as GET, without the form's CSRF token + const action = await modulePage + .locatorForDeviceRow(deviceName) + .locator('form:has(button[data-test-id="confirm-delete"])') + .getAttribute('action'); + if (!action) throw new Error(`Delete form for 2FA device "${deviceName}" not found`); + await sendForgedGet(page, action); + }, +); + +When( + 'I send a forged GET request that deletes the user {string} in the user management module', + async ({ page }, username: string) => { + await page.goto(USER_MANAGEMENT_MODULE_URL); + const editUri = await page + .locator(`[data-test-id="user-row"][data-username="${username}"] a`) + .first() + .getAttribute('href'); + const userId = editUri ? new URL(editUri, page.url()).searchParams.get('moduleArguments[userId]') : null; + if (!userId) throw new Error(`User id of "${username}" not found in the edit link "${editUri}"`); + const query = new URLSearchParams({ 'moduleArguments[userIds][0]': userId }); + await sendForgedGet(page, `${USER_MANAGEMENT_MODULE_URL}/delete?${query}`); + }, +); + +// Neos 8 passes the module's 405 through. Neos 9 (Backend\ModuleController) only keeps the module response for +// redirects and non-HTML formats - otherwise it renders the module body ("405 Method Not Allowed") inside the backend +// layout with status 200. A successful (unguarded) action redirects instead, so both cases are told apart. +Then('the forged request should be rejected as "Method Not Allowed"', async () => { + expect(lastForgedResponse).toBeDefined(); + const { status, body } = lastForgedResponse!; + expect(status === 405 || (status === 200 && body.includes('405 Method Not Allowed'))).toBe(true); +});