@@ -65,14 +65,20 @@ class PhpMessageSource extends MessageSource
6565 * load the messages for [[sourceLanguage]]. For example: [[sourceLanguage]] is `en-GB`,
6666 * $language is `en`. The method will load the messages for `en` and merge them over `en-GB`.
6767 *
68+ * The category is validated before [[getMessageFilePath()]] is called, so the check described there also applies
69+ * when a subclass overrides that method.
70+ *
6871 * @param string $category the message category
6972 * @param string $language the target language
7073 * @return array the loaded messages. The keys are original messages, and the values are the translated messages.
74+ * @throws InvalidArgumentException if the category resolves to an unsafe path, or the language code is invalid.
7175 * @see loadFallbackMessages
7276 * @see sourceLanguage
7377 */
7478 protected function loadMessages ($ category , $ language )
7579 {
80+ $ this ->validateCategory ($ category );
81+
7682 $ messageFile = $ this ->getMessageFilePath ($ category , $ language );
7783 $ messages = $ this ->loadMessagesFromFile ($ messageFile );
7884
@@ -133,7 +139,10 @@ protected function loadFallbackMessages($category, $fallbackLanguage, $messages,
133139 *
134140 * The category may use `/` or `\` as namespace separators (for example, `app/error`). Categories that contain `..`
135141 * segments, an absolute path, or a stream-wrapper scheme (such as `php://`) are rejected so the resolved path
136- * cannot escape [[basePath]].
142+ * cannot escape [[basePath]]. Categories listed in [[fileMap]] are not checked.
143+ *
144+ * [[loadMessages()]] applies the same category check before calling this method, so an overriding implementation
145+ * receives only categories that passed it.
137146 *
138147 * @param string $category the message category
139148 * @param string $language the target language
@@ -143,25 +152,54 @@ protected function loadFallbackMessages($category, $fallbackLanguage, $messages,
143152 protected function getMessageFilePath ($ category , $ language )
144153 {
145154 $ language = (string ) $ language ;
155+
146156 if ($ language !== '' && !preg_match ('/^[a-z0-9_-]+$/i ' , $ language )) {
147157 throw new InvalidArgumentException (sprintf ('Invalid language code: "%s". ' , $ language ));
148158 }
159+
160+ $ this ->validateCategory ($ category );
161+
149162 $ messageFile = Yii ::getAlias ($ this ->basePath ) . "/ $ language/ " ;
163+
150164 if (isset ($ this ->fileMap [$ category ])) {
151165 $ messageFile .= $ this ->fileMap [$ category ];
152166 } else {
153- $ normalizedCategory = str_replace ('\\' , '/ ' , (string ) $ category );
167+ $ messageFile .= str_replace ('\\' , '/ ' , (string ) $ category ) . '.php ' ;
168+ }
154169
155- if (preg_match ('~(?:^|/)\.\.(?:/|$)|^/|^[A-Za-z]:/|^[A-Za-z][A-Za-z0-9+.\-]*://~ ' , $ normalizedCategory )) {
156- throw new InvalidArgumentException (
157- sprintf ('Invalid message category: "%s". ' , (string ) $ category ),
158- );
159- }
170+ return $ messageFile ;
171+ }
160172
161- $ messageFile .= "{$ normalizedCategory }.php " ;
173+ /**
174+ * Rejects a category that would resolve to a path outside [[basePath]].
175+ *
176+ * Backslashes are treated as `/` separators. A category that contains a `..` segment, starts with `/` or a drive
177+ * letter (such as `C:/`), or starts with a stream-wrapper scheme (such as `php://`) is rejected. A segment that
178+ * consists only of dots and spaces and has two or more dots (such as `.. ` or `...`) is treated as a `..` segment.
179+ * Categories listed in [[fileMap]] are trusted configuration and are not checked.
180+ *
181+ * @param string $category the message category
182+ * @throws InvalidArgumentException if the category resolves to an unsafe path.
183+ */
184+ private function validateCategory ($ category )
185+ {
186+ if (isset ($ this ->fileMap [$ category ])) {
187+ return ;
162188 }
163189
164- return $ messageFile ;
190+ $ normalizedCategory = str_replace ('\\' , '/ ' , (string ) $ category );
191+
192+ // A segment made only of dots and spaces with at least two dots is rejected, not just a literal `..`:
193+ // Windows may trim trailing dots and spaces from path components. The quantifiers are possessive so a long
194+ // run of dots cannot exhaust the backtrack limit.
195+ $ pattern = '~(?:^|/) *+\. *+\.[. ]*+(?:/|$)|^/|^[A-Za-z]:/|^[A-Za-z][A-Za-z0-9+.\-]*://~ ' ;
196+
197+ // `preg_match()` returns `false` when matching fails to complete; such a category is rejected as well.
198+ if (preg_match ($ pattern , $ normalizedCategory ) !== 0 ) {
199+ throw new InvalidArgumentException (
200+ sprintf ('Invalid message category: "%s". ' , (string ) $ category ),
201+ );
202+ }
165203 }
166204
167205 /**
0 commit comments