Skip to content

Commit

Permalink
MDL-29917 prevent form autocompletion in most Moodle forms
Browse files Browse the repository at this point in the history
The password autocompletion in case of Moodle makes sense only on the login page, the form autocompletion in general is most probably useful only on the user signup page.

This patch is compatible with html 5, unfortunately we have to ignore strict warnings in legacy xhtml 1.0 standard.
  • Loading branch information
skodak committed Dec 30, 2011
1 parent ea5534f commit 4362595
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
12 changes: 11 additions & 1 deletion lib/form/password.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ class MoodleQuickForm_password extends HTML_QuickForm_password{
*/ */
var $_helpbutton=''; var $_helpbutton='';
function MoodleQuickForm_password($elementName=null, $elementLabel=null, $attributes=null) { function MoodleQuickForm_password($elementName=null, $elementLabel=null, $attributes=null) {
global $CFG;
if (empty($CFG->xmlstrictheaders)) {
// no standard mform in moodle should allow autocomplete of passwords
// this is valid attribute in html5, sorry, we have to ignore validation errors in legacy xhtml 1.0
$attributes = (array)$attributes;
if (!isset($attributes['autocomplete'])) {
$attributes['autocomplete'] = 'off';
}
}

parent::HTML_QuickForm_password($elementName, $elementLabel, $attributes); parent::HTML_QuickForm_password($elementName, $elementLabel, $attributes);
} }
/** /**
Expand Down Expand Up @@ -48,4 +58,4 @@ function getHelpButton(){
return $this->_helpbutton; return $this->_helpbutton;
} }
} }
?> ?>
10 changes: 10 additions & 0 deletions lib/form/passwordunmask.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
class MoodleQuickForm_passwordunmask extends MoodleQuickForm_password { class MoodleQuickForm_passwordunmask extends MoodleQuickForm_password {


function MoodleQuickForm_passwordunmask($elementName=null, $elementLabel=null, $attributes=null) { function MoodleQuickForm_passwordunmask($elementName=null, $elementLabel=null, $attributes=null) {
global $CFG;
if (empty($CFG->xmlstrictheaders)) {
// no standard mform in moodle should allow autocomplete of passwords
// this is valid attribute in html5, sorry, we have to ignore validation errors in legacy xhtml 1.0
$attributes = (array)$attributes;
if (!isset($attributes['autocomplete'])) {
$attributes['autocomplete'] = 'off';
}
}

parent::MoodleQuickForm_password($elementName, $elementLabel, $attributes); parent::MoodleQuickForm_password($elementName, $elementLabel, $attributes);
} }


Expand Down
10 changes: 10 additions & 0 deletions lib/formslib.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ class moodleform {
* @return moodleform * @return moodleform
*/ */
function moodleform($action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true) { function moodleform($action=null, $customdata=null, $method='post', $target='', $attributes=null, $editable=true) {
global $CFG;
if (empty($CFG->xmlstrictheaders)) {
// no standard mform in moodle should allow autocomplete with the exception of user signup
// this is valid attribute in html5, sorry, we have to ignore validation errors in legacy xhtml 1.0
$attributes = (array)$attributes;
if (!isset($attributes['autocomplete'])) {
$attributes['autocomplete'] = 'off';
}
}

if (empty($action)){ if (empty($action)){
$action = strip_querystring(qualified_me()); $action = strip_querystring(qualified_me());
} }
Expand Down
5 changes: 3 additions & 2 deletions lib/javascript-static.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -427,13 +427,14 @@ function unmaskPassword(id) {
try { try {
// first try IE way - it can not set name attribute later // first try IE way - it can not set name attribute later
if (chb.checked) { if (chb.checked) {
var newpw = document.createElement('<input type="text" name="'+pw.name+'">'); var newpw = document.createElement('<input type="text" autocomplete="off" name="'+pw.name+'">');
} else { } else {
var newpw = document.createElement('<input type="password" name="'+pw.name+'">'); var newpw = document.createElement('<input type="password" autocomplete="off" name="'+pw.name+'">');
} }
newpw.attributes['class'].nodeValue = pw.attributes['class'].nodeValue; newpw.attributes['class'].nodeValue = pw.attributes['class'].nodeValue;
} catch (e) { } catch (e) {
var newpw = document.createElement('input'); var newpw = document.createElement('input');
newpw.setAttribute('autocomplete', 'off');
newpw.setAttribute('name', pw.name); newpw.setAttribute('name', pw.name);
if (chb.checked) { if (chb.checked) {
newpw.setAttribute('type', 'text'); newpw.setAttribute('type', 'text');
Expand Down
2 changes: 1 addition & 1 deletion login/signup.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function signup_captcha_enabled() {
//HTTPS is potentially required in this page //HTTPS is potentially required in this page
httpsrequired(); httpsrequired();


$mform_signup = new login_signup_form(); $mform_signup = new login_signup_form(null, null, 'post', '', array('autocomplete'=>'on'));


if ($mform_signup->is_cancelled()) { if ($mform_signup->is_cancelled()) {
redirect($CFG->httpswwwroot.'/login/index.php'); redirect($CFG->httpswwwroot.'/login/index.php');
Expand Down

0 comments on commit 4362595

Please sign in to comment.