Skip to content

[C#] CWE-348: Using a client-supplied IP address in a security check #9339

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

Closed

Conversation

manunio
Copy link

@manunio manunio commented May 26, 2022

@manunio
Copy link
Author

manunio commented Jun 26, 2022

Hi, any updates?

Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial review of QL code only.

}
}

string clientIpParameterName() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private?

]
}

string getIpAddressRegex() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private?

*
* For example: `[FromHeader(Name = "X-Forwarded-For")]`.
*/
private class AspNetCoreActionMethodParameterSource extends ClientSuppliedIpUsedInSecurityCheckSource,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a test for this.

Comment on lines +52 to +53
p.getAnAttribute().getType().hasQualifiedName("Microsoft.AspNetCore.Mvc.FromHeaderAttribute") and
p.getAnAttribute().getNamedArgument("Name").(StringLiteral).getValue().toLowerCase() =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably this should be the same attribute? So introduce a new Attribute a variable and change to

a = p.getAnAttribute() and
a.getType().hasQualifiedName("Microsoft.AspNetCore.Mvc.FromHeaderAttribute") and
a.getNamedArgument("Name").(StringLiteral).getValue().toLowerCase() = clientIpParameterName()

exists(RemoteFlowSource rfs, MethodCall mc |
rfs.asExpr() = mc.getQualifier() and
mc.getArgument(0).(StringLiteral).getValue().toLowerCase() = clientIpParameterName() and
this.getExpr() = mc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So shouldn't it be xHeader that is the source in this case, and not the method call itself?

// `Request.Headers.TryGetValue("X-Forwarded-For", out var xHeader)`
exists(RemoteFlowSource rfs, MethodCall mc |
rfs.asExpr() = mc.getQualifier() and
mc.getArgument(0).(StringLiteral).getValue().toLowerCase() = clientIpParameterName() and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mc.getTarget().getName() = "TryGetValue"?

Comment on lines +79 to +80
mc.getQualifier().(StringLiteral).getValue() instanceof PrivateHostName and
not mc.getQualifier().(StringLiteral).getValue() = "0:0:0:0:0:0:0:1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The StringLiteral infix casts should not be needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same in other places.

}

/** A data flow sink for sql operation. */
private class SqlOperationSink extends ClientSuppliedIpUsedInSecurityCheckSink {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is that relevant for this query?

mc.getArgument(0).(MethodCall).getTarget() = s.getSplitMethod() and
not mc.getTarget()
.hasQualifiedName("System.Linq.Enumerable",
["FirstOrDefault<System.String>", "First<System.String>"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it should be just

["FirstOrDefault<>", "First<>"]

@manunio manunio closed this Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants