Skip to content

Fix access checking for properies setters in attribute arguments #18581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Compiler/Checking/Expressions/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11399,7 +11399,12 @@ and TcAttributeEx canFail (cenv: cenv) (env: TcEnv) attrTgt attrEx (synAttr: Syn

let mkAttribExpr e =
AttribExpr(e, EvalLiteralExprOrAttribArg g e)


let checkPropSetterAttribAccess m (pinfo: PropInfo) =
let setterMeth = pinfo.SetterMethod
if not <| IsTypeAndMethInfoAccessible cenv.amap m ad ad setterMeth then
errorR(Error (FSComp.SR.tcPropertyCannotBeSetPrivateSetter(pinfo.PropertyName), m))

let namedAttribArgMap =
attributeAssignedNamedItems |> List.map (fun (CallerNamedArg(id, CallerArg(callerArgTy, m, isOpt, callerArgExpr))) ->
if isOpt then error(Error(FSComp.SR.tcOptionalArgumentsCannotBeUsedInCustomAttribute(), m))
Expand All @@ -11411,6 +11416,7 @@ and TcAttributeEx canFail (cenv: cenv) (env: TcEnv) attrTgt attrEx (synAttr: Syn
| Item.Property (info = [pinfo]) ->
if not pinfo.HasSetter then
errorR(Error(FSComp.SR.tcPropertyCannotBeSet0(), m))
checkPropSetterAttribAccess m pinfo
id.idText, true, pinfo.GetPropertyType(cenv.amap, m)
| Item.ILField finfo ->
CheckILFieldInfoAccessible g cenv.amap m ad finfo
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,7 @@ notAFunctionButMaybeDeclaration,"This value is not a function and cannot be appl
3245,tcCopyAndUpdateNeedsRecordType,"The input to a copy-and-update expression that creates an anonymous record must be either an anonymous record or a record"
3246,tcAugmentationsCannotHaveAttributes,"Attributes cannot be applied to type extensions."
3247,couldNotLoadDependencyManagerExtension,"The dependency manager extension %s could not be loaded. Message: %s"
3248,tcPropertyCannotBeSetPrivateSetter,"Property '%s' cannot be set because the setter is private"
3250,expressionHasNoName,"Expression does not have a name."
3251,chkNoFirstClassNameOf,"Using the 'nameof' operator as a first-class function value is not permitted."
3252,tcIllegalByrefsInOpenTypeDeclaration,"Byref types are not allowed in an open type declaration."
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Compiler/xlf/FSComp.txt.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace FSharp.Compiler.ComponentTests.Attributes

open Xunit
open FSharp.Test.Compiler

module AttributeCtorSetPropAccess =
let private csLib = """
using System;

namespace TestAttribute
{
[AttributeUsage(AttributeTargets.All, Inherited = false)]
public class FooAttribute : Attribute
{
public int X { get; %s set; }
}
}
"""

let private fsCode = """
namespace Other
open System
open TestAttribute

module TestAttributeModule =

[<FooAttribute(X = 100)>]
let myFunction() = ()
"""

[<Theory>]
[<InlineData("private")>]
[<InlineData("internal")>]
[<InlineData("protected")>]
[<InlineData("private protected")>]
[<InlineData("protected internal")>]
let ``Cannot set property outside its accessibility scope``(modifier: string): unit =
FSharp fsCode
|> withReferences [(CSharp <| csLib.Replace("%s", modifier))]
|> compile
|> shouldFail
|> withDiagnostics [ (ErrorType.Error 3248, Line 8, Col 28, Line 8, Col 31, "Property 'X' cannot be set because the setter is private") ]
|> ignore

[<Fact>]
let ``Can set property inside its accessibility scope``(): unit =
FSharp fsCode
|> withReferences [(CSharp <| csLib.Replace("%s", ""))]
|> compile
|> shouldSucceed
|> ignore
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@
<Compile Include="FSharpChecker\TransparentCompiler.fs" />
<Compile Include="FSharpChecker\SymbolUse.fs" />
<Compile Include="FSharpChecker\FindReferences.fs" />
<Compile Include="Attributes\AttributeCtorSetPropAccess.fs"/>
</ItemGroup>

<ItemGroup>
Expand Down
Loading