r75604 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75603‎ | r75604 | r75605 >
Date:02:54, 28 October 2010
Author:neilk
Status:ok
Tags:
Comment:
followup to r73555. documenting & validating expected format of session keys (since they must also act as file paths)
Modified paths:
  • /branches/uploadwizard/phase3/includes/specials/SpecialSessionStash.php (modified) (history)
  • /branches/uploadwizard/phase3/includes/upload/SessionStash.php (modified) (history)

Diff [purge]

Index: branches/uploadwizard/phase3/includes/upload/SessionStash.php
@@ -12,6 +12,12 @@
1313 *
1414 */
1515 class SessionStash {
 16+ // Format of the key for files -- has to be suitable as a filename itself in some cases.
 17+ // This should encompass a sha1 content hash in hex (new style), or an integer (old style),
 18+ // and also thumbnails with prepended strings like "120px-".
 19+ // The file extension should not be part of the key.
 20+ const KEY_FORMAT_REGEX = '/^[\w-]+$/';
 21+
1622 // repository that this uses to store temp files
1723 protected $repo;
1824
@@ -65,7 +71,11 @@
6672 * @throws SessionStashBadVersionException
6773 * @return {SessionStashItem} null if no such item or item out of date, or the item
6874 */
69 - public function getFile( $key ) {
 75+ public function getFile( $key ) {
 76+ if ( ! preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
 77+ throw new SessionStashBadPathException( "key '$key' is not in a proper format" );
 78+ }
 79+
7080 if ( !isset( $this->files[$key] ) ) {
7181 if ( !isset( $_SESSION[UploadBase::SESSION_KEYNAME][$key] ) ) {
7282 throw new SessionStashFileNotFoundException( "key '$key' not found in session" );
@@ -113,6 +123,10 @@
114124 $key = $fileProps['sha1'];
115125 }
116126
 127+ if ( ! preg_match( self::KEY_FORMAT_REGEX, $key ) ) {
 128+ throw new SessionStashBadPathException( "key '$key' is not in a proper format" );
 129+ }
 130+
117131 // if not already in a temporary area, put it there
118132 $status = $this->repo->storeTemp( basename( $path ), $path );
119133 if( ! $status->isOK() ) {
Index: branches/uploadwizard/phase3/includes/specials/SpecialSessionStash.php
@@ -92,25 +92,22 @@
9393 if ( $n !== false ) {
9494 $key = $n ? substr( $subPage, 0, $n ) : $subPage;
9595 }
96 -
 96+
9797 try {
9898 $file = $this->stash->getFile( $key );
9999 } catch ( SessionStashFileNotFoundException $e ) {
100100 // if we couldn't find it, and it looks like a thumbnail,
101101 // and it looks like we have the original, go ahead and generate it
102102 $matches = array();
103 - // FIXME: This code assumes all kinds of constraints apply to file keys:
104 - // they can't contain whitespace, and keys for original files can't contain dashes.
105 - // These assumptions should be documented and/or enforced --RK
106 - if ( ! preg_match( '/^(\d+)px-(\S+)$/', $key, $matches ) ) {
 103+ if ( ! preg_match( '/^(\d+)px-(.*)$/', $key, $matches ) ) {
107104 // that doesn't look like a thumbnail. re-raise exception
108105 throw $e;
109106 }
110107
111 - $width = $matches[1];
112 - $origKey = $matches[2];
 108+ list( $dummy, $width, $origKey ) = $matches;
113109
114 - // do not trap exceptions, if not found let exceptions propagate to caller.
 110+ // do not trap exceptions, if key is in bad format, or file not found,
 111+ // let exceptions propagate to caller.
115112 $origFile = $this->stash->getFile( $origKey );
116113
117114 // ok we're here so the original must exist. Generate the thumbnail.

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r73555committing some long-overdue but still unfinished changes. Particularly: test...neilk18:23, 22 September 2010

Status & tagging log