Fossil SCM

When a server is pointing to a directory, allow *.fossil files to be served out of any subdirectory of that directory. For security, pathnames may not contain any characters except alphanumerics, "/", "-", and "_".

drh 2011-03-18 02:51 UTC trunk
Commit d04fa1e1438ec7428afccc76af1a00ce53564dbc
1 file changed +39 -21
+39 -21
--- src/main.c
+++ src/main.c
@@ -890,36 +890,54 @@
890890
/* If the repository has not been opened already, then find the
891891
** repository based on the first element of PATH_INFO and open it.
892892
*/
893893
zPathInfo = PD("PATH_INFO","");
894894
if( !g.repositoryOpen ){
895
- char *zRepo;
895
+ char *zRepo, *zToFree;
896896
const char *zOldScript = PD("SCRIPT_NAME", "");
897897
char *zNewScript;
898898
int j, k;
899
+ i64 szFile;
899900
900901
i = 1;
901
- while( zPathInfo[i] && zPathInfo[i]!='/' ){ i++; }
902
- zRepo = mprintf("%s%.*s.fossil",g.zRepositoryName,i,zPathInfo);
903
-
904
- /* To avoid mischief, make sure the repository basename contains no
905
- ** characters other than alphanumerics, "-", and "_".
906
- */
907
- for(j=strlen(g.zRepositoryName)+1, k=0; k<i-1; j++, k++){
908
- if( !fossil_isalnum(zRepo[j]) && zRepo[j]!='-' ) zRepo[j] = '_';
909
- }
910
- if( zRepo[0]=='/' && zRepo[1]=='/' ) zRepo++;
911
-
912
- if( file_size(zRepo)<1024 ){
913
- if( zNotFound ){
914
- cgi_redirect(zNotFound);
915
- }else{
916
- @ <h1>Not Found</h1>
917
- cgi_set_status(404, "not found");
918
- cgi_reply();
919
- }
920
- return;
902
+ while( 1 ){
903
+ while( zPathInfo[i] && zPathInfo[i]!='/' ){ i++; }
904
+ zRepo = zToFree = mprintf("%s%.*s.fossil",g.zRepositoryName,i,zPathInfo);
905
+
906
+ /* To avoid mischief, make sure the repository basename contains no
907
+ ** characters other than alphanumerics, "-", "/", and "_".
908
+ */
909
+ for(j=strlen(g.zRepositoryName)+1, k=0; k<i-1; j++, k++){
910
+ if( !fossil_isalnum(zRepo[j]) && zRepo[j]!='-' && zRepo[j]!='/' ){
911
+ zRepo[j] = '_';
912
+ }
913
+ }
914
+ if( zRepo[0]=='/' && zRepo[1]=='/' ){ zRepo++; j--; }
915
+
916
+ szFile = file_size(zRepo);
917
+ if( zPathInfo[i]=='/' && szFile<0 ){
918
+ assert( strcmp(&zRepo[j], ".fossil")==0 );
919
+ zRepo[j] = 0;
920
+ if( file_isdir(zRepo)==1 ){
921
+ fossil_free(zToFree);
922
+ i++;
923
+ continue;
924
+ }
925
+ zRepo[j] = '.';
926
+ }
927
+
928
+ if( szFile<1024 ){
929
+ if( zNotFound ){
930
+ cgi_redirect(zNotFound);
931
+ }else{
932
+ @ <h1>Not Found</h1>
933
+ cgi_set_status(404, "not found");
934
+ cgi_reply();
935
+ }
936
+ return;
937
+ }
938
+ break;
921939
}
922940
zNewScript = mprintf("%s%.*s", zOldScript, i, zPathInfo);
923941
cgi_replace_parameter("PATH_INFO", &zPathInfo[i+1]);
924942
zPathInfo += i;
925943
cgi_replace_parameter("SCRIPT_NAME", zNewScript);
926944
--- src/main.c
+++ src/main.c
@@ -890,36 +890,54 @@
890 /* If the repository has not been opened already, then find the
891 ** repository based on the first element of PATH_INFO and open it.
892 */
893 zPathInfo = PD("PATH_INFO","");
894 if( !g.repositoryOpen ){
895 char *zRepo;
896 const char *zOldScript = PD("SCRIPT_NAME", "");
897 char *zNewScript;
898 int j, k;
 
899
900 i = 1;
901 while( zPathInfo[i] && zPathInfo[i]!='/' ){ i++; }
902 zRepo = mprintf("%s%.*s.fossil",g.zRepositoryName,i,zPathInfo);
903
904 /* To avoid mischief, make sure the repository basename contains no
905 ** characters other than alphanumerics, "-", and "_".
906 */
907 for(j=strlen(g.zRepositoryName)+1, k=0; k<i-1; j++, k++){
908 if( !fossil_isalnum(zRepo[j]) && zRepo[j]!='-' ) zRepo[j] = '_';
909 }
910 if( zRepo[0]=='/' && zRepo[1]=='/' ) zRepo++;
911
912 if( file_size(zRepo)<1024 ){
913 if( zNotFound ){
914 cgi_redirect(zNotFound);
915 }else{
916 @ <h1>Not Found</h1>
917 cgi_set_status(404, "not found");
918 cgi_reply();
919 }
920 return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
921 }
922 zNewScript = mprintf("%s%.*s", zOldScript, i, zPathInfo);
923 cgi_replace_parameter("PATH_INFO", &zPathInfo[i+1]);
924 zPathInfo += i;
925 cgi_replace_parameter("SCRIPT_NAME", zNewScript);
926
--- src/main.c
+++ src/main.c
@@ -890,36 +890,54 @@
890 /* If the repository has not been opened already, then find the
891 ** repository based on the first element of PATH_INFO and open it.
892 */
893 zPathInfo = PD("PATH_INFO","");
894 if( !g.repositoryOpen ){
895 char *zRepo, *zToFree;
896 const char *zOldScript = PD("SCRIPT_NAME", "");
897 char *zNewScript;
898 int j, k;
899 i64 szFile;
900
901 i = 1;
902 while( 1 ){
903 while( zPathInfo[i] && zPathInfo[i]!='/' ){ i++; }
904 zRepo = zToFree = mprintf("%s%.*s.fossil",g.zRepositoryName,i,zPathInfo);
905
906 /* To avoid mischief, make sure the repository basename contains no
907 ** characters other than alphanumerics, "-", "/", and "_".
908 */
909 for(j=strlen(g.zRepositoryName)+1, k=0; k<i-1; j++, k++){
910 if( !fossil_isalnum(zRepo[j]) && zRepo[j]!='-' && zRepo[j]!='/' ){
911 zRepo[j] = '_';
912 }
913 }
914 if( zRepo[0]=='/' && zRepo[1]=='/' ){ zRepo++; j--; }
915
916 szFile = file_size(zRepo);
917 if( zPathInfo[i]=='/' && szFile<0 ){
918 assert( strcmp(&zRepo[j], ".fossil")==0 );
919 zRepo[j] = 0;
920 if( file_isdir(zRepo)==1 ){
921 fossil_free(zToFree);
922 i++;
923 continue;
924 }
925 zRepo[j] = '.';
926 }
927
928 if( szFile<1024 ){
929 if( zNotFound ){
930 cgi_redirect(zNotFound);
931 }else{
932 @ <h1>Not Found</h1>
933 cgi_set_status(404, "not found");
934 cgi_reply();
935 }
936 return;
937 }
938 break;
939 }
940 zNewScript = mprintf("%s%.*s", zOldScript, i, zPathInfo);
941 cgi_replace_parameter("PATH_INFO", &zPathInfo[i+1]);
942 zPathInfo += i;
943 cgi_replace_parameter("SCRIPT_NAME", zNewScript);
944

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button