Skip to content

Commit 0f39d43

Browse files
author
Greg Bowler
authored
Merge pull request #62 from PhpGt/61-post-only
Only inject into POST forms, closes #61
2 parents f8b7236 + ae6ff0b commit 0f39d43

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

src/HTMLDocumentProtector.php

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public function protectAndInject(
5555
$this->tokenStore->saveToken($token);
5656

5757
foreach($forms as $form) {
58+
$formMethod = $form->getAttribute("method");
59+
if(strtolower($formMethod) !== "post") {
60+
continue;
61+
}
62+
5863
$csrfElement = $this->document->createElement(
5964
"input"
6065
);

test/unit/HTMLDocumentProtectorTest.php

+29-24
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ class HTMLDocumentProtectorTest extends TestCase {
3131
<body>
3232
<h1>This HTML is for the unit test.</h1>
3333
<p>Hello</p>
34-
<form method="POST">
35-
<input type="text">
36-
<button type="submit"></button>
37-
</form>
34+
35+
<form method="POST">
36+
<input type="text">
37+
<button type="submit"></button>
38+
</form>
3839
</body>
3940
</html>
4041
HTML;
@@ -50,17 +51,16 @@ class HTMLDocumentProtectorTest extends TestCase {
5051
<body>
5152
<h1>This HTML is for the unit test.</h1>
5253
<p>Hello</p>
53-
<form method="POST">
54-
<input type="text">
55-
<button type="submit"></button>
56-
</form>
57-
<form method="GET">
58-
<input type="text" value="A text field">
59-
<button type="submit"></button>
60-
</form>
61-
<!-- an empty form too...-->
62-
<form method="POST">
63-
</form>
54+
<form method="POST">
55+
<input type="text">
56+
<button type="submit"></button>
57+
</form>
58+
<form method="GET">
59+
<input type="text" value="A text field">
60+
<button type="submit"></button>
61+
</form>
62+
<!-- an empty form too...-->
63+
<form method="POST"></form>
6464
</body>
6565
</html>
6666
HTML;
@@ -77,9 +77,9 @@ class HTMLDocumentProtectorTest extends TestCase {
7777
<body>
7878
<h1>This HTML is for the unit test.</h1>
7979
<p>Hello</p>
80-
<!-- an empty form too...-->
81-
<form method="POST">
82-
</form>
80+
<!-- an empty form too...-->
81+
<form method="POST">
82+
</form>
8383
</body>
8484
</html>
8585
HTML;
@@ -153,12 +153,17 @@ public function testMultipleForms() {
153153

154154
// check that the token has been injected in all forms
155155
$doc = $sut->getHTMLDocument();
156-
$this->assertEquals(
157-
3, $doc->querySelectorAll(
158-
"input[name='" . HTMLDocumentProtector::TOKEN_NAME . "']")->length);
159-
$this->assertEquals(
160-
1, $doc->querySelectorAll(
161-
"head meta[name='" . HTMLDocumentProtector::TOKEN_NAME . "']")->length);
156+
$this->assertCount(
157+
2,
158+
$doc->querySelectorAll(
159+
"input[name='" . HTMLDocumentProtector::TOKEN_NAME . "']"
160+
)
161+
);
162+
$this->assertCount(
163+
1,
164+
$doc->querySelectorAll(
165+
"head meta[name='" . HTMLDocumentProtector::TOKEN_NAME . "']")
166+
);
162167
}
163168

164169
public function testSingleCodeSharedAcrossForms() {

0 commit comments

Comments
 (0)