ColdFusion Mappings and Relative Paths
Recently one of our applications started failing inconsistently with strange errors about certain files not existing. We’d see error emails about things like the
/myapplication/views/pages/survey.cfm not existing which didn’t make any sense since /myapplication is a mapping created inside the Application.cfc
<cfset this.mappings["/myapplication"] = expandPath("../")>
See the bug there? ExpandPath() is relative to the requested template. This application had previously had all requests routed through
/public/index.cfm so the mapping worked fine.
However, recently we had added some new web services like
/public/services/ScheduleService.cfc and now whenever an Ajax request went through the CF mapping would change from pointing to
/public causing all other concurrent requests to fail with confusing missing file errors.
I fixed this by making sure the path was no longer relative.
<cfset this.mappings["/myapplication"] = getDirectoryFromPath(getCurrentTemplatePath())>
So the moral of the story is to NEVER use expandPath() to create a mapping that’s relative to the webroot.