-
-
Notifications
You must be signed in to change notification settings - Fork 424
Rule: Add form-method-require
rule
#1710
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Listener } from '../htmlparser' | ||
import { Rule } from '../types' | ||
|
||
export default { | ||
id: 'form-method-require', | ||
description: | ||
'The method attribute of a <form> element must be present with a valid value: "get", "post", or "dialog".', | ||
init(parser, reporter) { | ||
const onTagStart: Listener = (event) => { | ||
const tagName = event.tagName.toLowerCase() | ||
|
||
if (tagName === 'form') { | ||
const mapAttrs = parser.getMapAttrs(event.attrs) | ||
const col = event.col + tagName.length + 1 | ||
|
||
if (mapAttrs.method === undefined) { | ||
reporter.warn( | ||
'The method attribute must be present on <form> elements.', | ||
event.line, | ||
col, | ||
this, | ||
event.raw | ||
) | ||
} else { | ||
const methodValue = mapAttrs.method.toLowerCase() | ||
if ( | ||
methodValue !== 'get' && | ||
methodValue !== 'post' && | ||
methodValue !== 'dialog' | ||
) { | ||
reporter.warn( | ||
'The method attribute of <form> must have a valid value: "get", "post", or "dialog".', | ||
event.line, | ||
col, | ||
this, | ||
event.raw | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
parser.addListener('tagstart', onTagStart) | ||
}, | ||
} as Rule |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
const HTMLHint = require('../../dist/htmlhint.js').HTMLHint | ||
|
||
const ruleId = 'form-method-require' | ||
const ruleOptions = {} | ||
|
||
ruleOptions[ruleId] = true | ||
|
||
describe(`Rules: ${ruleId}`, () => { | ||
it('Form with method="get" should not result in an error', () => { | ||
const code = '<form method="get"></form>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Form with method="post" should not result in an error', () => { | ||
const code = '<form method="post"></form>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Form with method="dialog" should not result in an error', () => { | ||
const code = '<form method="dialog"></form>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Form without method attribute should result in an error', () => { | ||
const code = '<form></form>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(1) | ||
expect(messages[0].rule.id).toBe(ruleId) | ||
expect(messages[0].line).toBe(1) | ||
expect(messages[0].col).toBe(6) | ||
expect(messages[0].type).toBe('warning') | ||
expect(messages[0].message).toBe( | ||
'The method attribute must be present on <form> elements.' | ||
) | ||
}) | ||
|
||
it('Form with invalid method value should result in an error', () => { | ||
const code = '<form method="invalid"></form>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(1) | ||
expect(messages[0].rule.id).toBe(ruleId) | ||
expect(messages[0].line).toBe(1) | ||
expect(messages[0].col).toBe(6) | ||
expect(messages[0].type).toBe('warning') | ||
expect(messages[0].message).toBe( | ||
'The method attribute of <form> must have a valid value: "get", "post", or "dialog".' | ||
) | ||
}) | ||
|
||
it('Form with uppercase method value should not result in an error', () => { | ||
const code = '<form method="POST"></form>' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
|
||
it('Other elements should not be affected by this rule', () => { | ||
const code = '<div>Not a form</div><input type="text">' | ||
const messages = HTMLHint.verify(code, ruleOptions) | ||
expect(messages.length).toBe(0) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
id: form-method-require | ||
title: form-method-require | ||
description: Requires form elements to have a valid method attribute for better security and user experience. | ||
sidebar: | ||
badge: New | ||
pagefind: false | ||
hidden: true | ||
--- | ||
|
||
import { Badge } from '@astrojs/starlight/components'; | ||
|
||
The method attribute of a `<form>` element must be present with a valid value: "get", "post", or "dialog". | ||
|
||
Level: <Badge text="Warning" variant="caution" /> | ||
|
||
## Config value | ||
|
||
- `true`: enable rule | ||
- `false`: disable rule | ||
|
||
### The following patterns are **not** considered rule violations | ||
|
||
```html | ||
<form method="get"></form> | ||
<form method="post"></form> | ||
<form method="dialog"></form> | ||
``` | ||
|
||
### The following patterns are considered rule violations | ||
|
||
```html | ||
<form>No method specified</form> | ||
<form method="invalid">Invalid method</form> | ||
``` | ||
|
||
## Why this rule is important | ||
|
||
The absence of the method attribute means the form will use the default `GET` method. With `GET`, form data is included in the URL (e.g., `?username=john&password=secret`), which can expose sensitive information in browser history, logs, or the network request. | ||
|
||
The HTML specification requires that form elements have one of three valid methods: | ||
|
||
- `get`: Appends form data to the URL (default, but not recommended for sensitive data) | ||
- `post`: Sends form data in the request body (more secure for sensitive data) | ||
- `dialog`: Used for dialog forms (HTML5 feature) | ||
|
||
This rule helps ensure that forms have explicit, valid methods for better security and user experience. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.