Skip to content

Commit

Permalink
Validate filename (#1338)
Browse files Browse the repository at this point in the history
* Take the full path into account when sanitizing file name. Fixes #1337.

* Bump to alpha 7.

* Add gitpod yml.

* Get file name cross platform.

* Format code
  • Loading branch information
nojaf committed Dec 26, 2020
1 parent 294495d commit c7b53c4
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
image: mcr.microsoft.com/dotnet/sdk:5.0
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 4.4.0-alpha-007 12/2020

* Fix Program.fs inside full path can lead to invalid AST. [#1337](https://github.com/fsprojects/fantomas/issues/1337)

### 4.4.0-alpha-006 12/2020

* Fix Formatting of long parameter lists. [#657](https://github.com/fsprojects/fantomas/issues/657)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
<Version>4.4.0-alpha-006</Version>
<Version>4.4.0-alpha-007</Version>
<NoWarn>FS0988</NoWarn>
<WarningsAsErrors>FS0025</WarningsAsErrors>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.CoreGlobalTool/Fantomas.CoreGlobalTool.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<ToolCommandName>fantomas</ToolCommandName>
<PackAsTool>True</PackAsTool>
<Version>4.4.0-alpha-006</Version>
<Version>4.4.0-alpha-007</Version>
<AssemblyName>fantomas-tool</AssemblyName>
<WarningsAsErrors>FS0025</WarningsAsErrors>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas.Extras/Fantomas.Extras.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>4.4.0-alpha-006</Version>
<Version>4.4.0-alpha-007</Version>
<Description>Utility package for Fantomas</Description>
<WarningsAsErrors>FS0025</WarningsAsErrors>
</PropertyGroup>
Expand Down
27 changes: 27 additions & 0 deletions src/Fantomas.Tests/CodeFormatterTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,30 @@ let main argv _ =
)
|> Async.RunSynchronously
|> ignore

[<Test>]
let ``sanitize filename if path with Program.fs`` () =
let fileName = @"d:\dev\bootcamp\src\Program.fs"

let source = """
open System
open Poker.Main
[<EntryPoint>]
let main _ =
processInput Console.In Console.Out
0
"""

let parsingOptions =
FakeHelpers.createParsingOptionsFromFile fileName

CodeFormatter.FormatDocumentAsync(
fileName,
SourceOrigin.SourceString source,
config,
parsingOptions,
sharedChecker.Value
)
|> Async.RunSynchronously
|> ignore
2 changes: 1 addition & 1 deletion src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>4.4.0-alpha-006</Version>
<Version>4.4.0-alpha-007</Version>
<NoWarn>FS0988</NoWarn>
<TargetFramework>net5.0</TargetFramework>
<WarningsAsErrors>FS0025</WarningsAsErrors>
Expand Down
7 changes: 5 additions & 2 deletions src/Fantomas/CodeFormatterImpl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ type FormatContext =
SourceText: ISourceText }

// Some file names have a special meaning for the F# compiler and the AST cannot be parsed.
let safeFileName fileName =
let safeFileName (file: string) =
let fileName =
file.Split([| '\\'; '/' |]) |> Array.last

if fileName = "Program.fs" then
"tmp.fsx"
else
fileName
file

let createFormatContext fileName (source: SourceOrigin) =
let (sourceText, sourceCode) = getSourceTextAndCode source
Expand Down
2 changes: 1 addition & 1 deletion src/Fantomas/Fantomas.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>4.4.0-alpha-006</Version>
<Version>4.4.0-alpha-007</Version>
<Description>Source code formatter for F#</Description>
<WarningsAsErrors>FS0025</WarningsAsErrors>
</PropertyGroup>
Expand Down

0 comments on commit c7b53c4

Please sign in to comment.