Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions includes/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,28 +486,39 @@ public static function apply_salt_fix(): array {
'message' => __( 'The generated configuration failed a safety check, so nothing was written.', 'magicauth' ),
];
}
// Preserve the original file mode across the swap: move() uses rename(),
// so the destination would otherwise inherit the temp file's perms and
// silently widen a hardened wp-config.php (e.g. 0600 -> 0644). Applying
// the original mode to the temp before the move also keeps the brief
// temp-file window no more readable than the file it replaces.
$mode = $filesystem->getchmod( $path );

$temp = dirname( $path ) . '/.magicauth-wpconfig-' . bin2hex( random_bytes( 8 ) ) . '.tmp';
if ( ! $filesystem->put_contents( $temp, $updated, FS_CHMOD_FILE ) ) {
return [
'ok' => false,
'message' => __( 'Could not write the new configuration. No changes were made.', 'magicauth' ),
];
// Target mode: preserve the original file's permissions. move() uses
// rename() and put_contents() chmods to its $mode argument, either of
// which would otherwise widen a hardened wp-config.php (e.g. 0600 -> 0644).
// Fall back to the WordPress default when the mode can't be read.
$mode_str = $filesystem->getchmod( $path );
$chmod = '' !== $mode_str ? (int) octdec( $mode_str ) : FS_CHMOD_FILE;

Comment on lines +493 to +495

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🌐 Web query:

In current WordPress core, what is the documented/actual return type of WP_Filesystem_Base::getchmod(), and can it return false or non-octal strings?

💡 Result:

In current WordPress core, WP_Filesystem_Base::getchmod is documented to return a string (octal-mode string). The Developer/Trac docs for WP_Filesystem_Base::getchmod state the return type as “string”, i.e., “Mode of the file (the last 3 digits)”[1]. In the upstream code for the base class, getchmod($file) simply returns the string '777' unconditionally[2]. Whether it can return false or non-octal strings: - The method is not documented to return false; the docblock specifies @return string[1]. The base implementation does not return false; it returns '777'[2]. - However, concrete filesystem implementations can differ in behavior. In the direct filesystem implementation (class-wp-filesystem-direct.php), getchmod($file) calls fileperms and returns the string '0' on failure; otherwise it returns substr(decoct(@fileperms($file)),-3), which is an octal-derived string[3]. That means the failure case is still a string and still octal-like ("0"), not false. So, in current core as implemented across at least the base class and the direct filesystem implementation, getchmod returns a string and does not return false; on failure it returns the string '0'. It should not return arbitrary non-octal strings under these implementations (it derives the last 3 digits from decoct(fileperms) or returns a fixed octal string), though the base-class stub itself returns '777' regardless of input[2][3].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Locate and print the relevant section around includes/Installer.php lines ~470-540
FILE="includes/Installer.php"
if [ -f "$FILE" ]; then
  echo "== $FILE =="
  sed -n '450,540p' "$FILE" | nl -ba | sed -n '1,200p'
else
  echo "File not found: $FILE" >&2
  exit 1
fi

# Show definitions/usage of put_contents call(s) in this region for $chmod
rg -n "put_contents\\(|\\$chmod" "$FILE" | head -n 50

Repository: EtticDevelopment/magicauth

Length of output: 160


🏁 Script executed:

#!/bin/bash
set -euo pipefail

FILE="includes/Installer.php"
echo "== $FILE =="
if [ ! -f "$FILE" ]; then
  echo "Missing $FILE" >&2
  exit 1
fi

# print with line numbers around the chmod section
sed -n '470,530p' "$FILE" | nl -ba

echo "== Context: chmod usage =="
rg -n "\\$chmod" "$FILE" | head -n 50

echo "== Context: getchmod usage =="
rg -n "getchmod\\(" "$FILE" | head -n 50

Repository: EtticDevelopment/magicauth

Length of output: 161


🏁 Script executed:

#!/bin/bash
set -euo pipefail

FILE="includes/Installer.php"
if [ ! -f "$FILE" ]; then
  echo "Missing $FILE" >&2
  exit 1
fi

sed -n '460,540p' "$FILE" | nl -ba

echo "---- getchmod occurrence ----"
rg -n "getchmod\\(" "$FILE"
echo "---- chmod usage occurrence ----"
rg -n "\\$chmod" "$FILE"

Repository: EtticDevelopment/magicauth

Length of output: 114


🏁 Script executed:

#!/bin/bash
set -euo pipefail

FILE="includes/Installer.php"
echo "== Checking file =="
if [ ! -f "$FILE" ]; then
  echo "Missing $FILE" >&2
  exit 1
fi
echo "Size(bytes): $(stat -c %s "$FILE" 2>/dev/null || echo unknown)"

echo "== Snippet around chmod (lines 475-525) =="
# Use cat -n for line numbers (nl not available)
sed -n '475,525p' "$FILE" | cat -n

echo "== Lines containing getchmod() =="
rg -n "getchmod\\(" "$FILE" || true

echo "== Lines containing $chmod =="
rg -n "\\$chmod" "$FILE" || true

echo "== Lines containing put_contents() =="
rg -n "put_contents\\(" "$FILE" || true

Repository: EtticDevelopment/magicauth

Length of output: 3022


🏁 Script executed:

#!/bin/bash
set -euo pipefail

FILE="includes/Installer.php"
echo "== Snippet around chmod (lines 485-510) =="
sed -n '485,510p' "$FILE" | cat -n

echo "== Full chmod block context (475-515) =="
sed -n '475,515p' "$FILE" | cat -n

echo "== getchmod() occurrences =="
rg -n "getchmod\\(" "$FILE" || true

echo "== octdec() occurrences =="
rg -n "octdec\\(" "$FILE" || true

echo "== put_contents() occurrences near chmod block =="
rg -n "put_contents\\(.*\\$chmod|put_contents\\(.*?,\\s*\\$updated,\\s*\\$chmod|put_contents\\(\\s*\\$path,\\s*\\$updated,\\s*\\$chmod|put_contents\\(\\s*\\$path,\\s*\\$updated" "$FILE" || true

# Safe check for raw "$chmod" text (no bash $ expansion)
echo "== Lines containing literal '$chmod' =="
grep -nF '$chmod' "$FILE" || true

Repository: EtticDevelopment/magicauth

Length of output: 4161


Harden chmod parsing to avoid applying 0000 when getchmod() fails

WP_Filesystem_*::getchmod() is expected to return an octal-mode string, and WP_Filesystem_Direct::getchmod() returns the string '0' when it can’t read permissions. Current logic only falls back on '', so '0' !== $mode_str and octdec('0') produces $chmod = 0, which is then passed to put_contents() for both the atomic temp swap and the in-place fallback writes.

$mode_str = $filesystem->getchmod( $path );
$chmod    = '' !== $mode_str ? (int) octdec( $mode_str ) : FS_CHMOD_FILE;
Proposed fix
-		$mode_str = $filesystem->getchmod( $path );
-		$chmod    = '' !== $mode_str ? (int) octdec( $mode_str ) : FS_CHMOD_FILE;
+		$mode_raw = $filesystem->getchmod( $path );
+		$mode_str = is_string( $mode_raw ) ? trim( $mode_raw ) : '';
+		$chmod    = preg_match( '/^[0-7]{3,4}$/', $mode_str ) ? (int) octdec( $mode_str ) : FS_CHMOD_FILE;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@includes/Installer.php` around lines 493 - 495, The chmod parsing uses
$filesystem->getchmod($path) and treats only the empty string as failure, so a
returned '0' becomes octdec('0') → 0 and passes a 0000 mode to writes; update
the logic around $mode_str and $chmod to treat '0' (and any non-octal or invalid
value) as a failure and fall back to FS_CHMOD_FILE. Specifically, in the
Installer.php code that reads $mode_str and computes $chmod, validate $mode_str
using a check like: ensure it's a non-empty octal string (e.g., matches
/^[0-7]+$/) before calling octdec and casting; otherwise set $chmod =
FS_CHMOD_FILE so put_contents() never receives 0000. Make the change where
$mode_str and $chmod are defined (referencing getchmod, $mode_str, $chmod, and
FS_CHMOD_FILE).

// Preferred path: atomic temp-file swap. Requires a writable *directory*
// (to create the sibling temp) and also keeps the brief temp window no
// more readable than the file it replaces.
$temp = dirname( $path ) . '/.magicauth-wpconfig-' . bin2hex( random_bytes( 8 ) ) . '.tmp';
$written = false;
if ( $filesystem->put_contents( $temp, $updated, $chmod ) ) {
if ( $filesystem->move( $temp, $path, true ) ) {
$written = true;
} else {
$filesystem->delete( $temp );
}
}
if ( '' !== $mode ) {
$filesystem->chmod( $temp, (int) octdec( $mode ) );

// Fallback: overwrite wp-config.php in place. Covers the common hardening
// where wp-config.php itself is writable but its directory is not, so a
// sibling temp file can't be created. Overwriting an existing writable
// file needs no directory write access; the content was already validated
// by the safety gate above, so the (non-atomic) in-place write is sound.
if ( ! $written ) {
$written = $filesystem->put_contents( $path, $updated, $chmod );
}
if ( ! $filesystem->move( $temp, $path, true ) ) {
$filesystem->delete( $temp );

if ( ! $written ) {
return [
'ok' => false,
'message' => __( 'Could not replace wp-config.php. No changes were made.', 'magicauth' ),
'message' => __( 'Could not write to wp-config.php. Use the manual copy-and-paste option instead.', 'magicauth' ),
];
}
delete_transient( self::SALT_NOTICE_KEY );
Expand Down
4 changes: 2 additions & 2 deletions magicauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: MagicAuth
* Plugin URI: https://github.com/EtticDevelopment/magicauth
* Description: Passwordless WordPress sign-in via email magic link or 6-character code.
* Version: 1.0.0
* Version: 1.0.1
* Requires at least: 6.4
* Requires PHP: 8.0
* Author: Ettic
Expand All @@ -26,7 +26,7 @@
return;
}

define( 'MAGICAUTH_VERSION', '1.0.0' );
define( 'MAGICAUTH_VERSION', '1.0.1' );
define( 'MAGICAUTH_DB_VERSION', 1 );
define( 'MAGICAUTH_FILE', __FILE__ );
define( 'MAGICAUTH_DIR', plugin_dir_path( __FILE__ ) );
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: login, passwordless, magic link, authentication, security
Requires at least: 6.4
Tested up to: 7.0
Requires PHP: 8.0
Stable tag: 1.0.0
Stable tag: 1.0.1
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down
Loading