Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
NpgsqlRest/NpgsqlRestClient/DefaultResponseParser.cs at master · NpgsqlRest/NpgsqlRest · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
NpgsqlRest
/
NpgsqlRest
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
11
Star
131
Code
Issues
0
Pull requests
4
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
NpgsqlRest
/
NpgsqlRestClient
/
DefaultResponseParser.cs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
157 lines (150 loc) · 6.84 KB
master
Breadcrumbs
NpgsqlRest
/
NpgsqlRestClient
/
DefaultResponseParser.cs
Copy path
Top
File metadata and controls
Code
Blame
157 lines (150 loc) · 6.84 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using
Microsoft
.
AspNetCore
.
Antiforgery
;
using
NpgsqlRest
;
using
NpgsqlRest
.
Auth
;
namespace
NpgsqlRestClient
;
public
class
DefaultResponseParser
(
NpgsqlRestAuthenticationOptions
options
,
string
?
antiforgeryFieldNameTag
,
string
?
antiforgeryTokenTag
,
Dictionary
<
string
,
string
?
>
?
availableClaims
,
Dictionary
<
string
,
string
?
>
?
availableEnvVars
=
null
)
{
// Env vars are resolved ONCE at construction. They don't change within the pod's lifetime, so
// re-reading per request would just add System.Environment overhead with no payoff. A K8s pod
// restart re-instantiates the middleware (and thus this parser), which re-reads the values.
private
readonly
Dictionary
<
string
,
string
>
_envVarReplacements
=
ResolveEnvVars
(
availableEnvVars
)
;
private
static
Dictionary
<
string
,
string
>
ResolveEnvVars
(
Dictionary
<
string
,
string
?
>
?
envVars
)
{
if
(
envVars
is
null
||
envVars
.
Count
==
0
)
{
return
[
]
;
}
var
result
=
new
Dictionary
<
string
,
string
>
(
envVars
.
Count
*
2
)
;
foreach
(
var
(
name
,
def
)
in
envVars
)
{
// present env var wins → configured default → empty string.
var
raw
=
Environment
.
GetEnvironmentVariable
(
name
)
??
def
;
// JSON-escape exactly like the claim path below (PgConverters.SerializeString). The
// substituted value is a complete JSON literal (a quoted, escaped string) so templates
// use a bare {NAME} token with no surrounding quotes and an accidental quote/backslash in
// the value cannot break the JS string. NOTE: the relaxed encoder does NOT escape '<'/'>',
// so this is not a defence against a hostile value - env values are operator-controlled,
// matching the claim path's trust model.
// SECURITY: this is an explicit allowlist - only names listed in AvailableEnvVars are ever
// read. NEVER widen this to the whole environment (Environment.GetEnvironmentVariables) -
// that would leak secrets (DB passwords, keys) to every client.
var
serialized
=
PgConverters
.
SerializeString
(
raw
??
string
.
Empty
)
;
result
[
name
]
=
serialized
;
// "!NAME" key ⇔ the name resolved to a real value (env set, or a configured default given).
// The {!NAME}/{!NAME:fallback} template forms key off this in Formatter.TryResolveToken.
if
(
raw
is
not
null
)
{
result
[
string
.
Concat
(
"!"
,
name
)
]
=
serialized
;
}
}
return
result
;
}
public
ReadOnlySpan
<
char
>
Parse
(
ReadOnlySpan
<
char
>
input
,
HttpContext
context
,
AntiforgeryTokenSet
?
tokenSet
)
{
Dictionary
<
string
,
string
>
replacements
=
[
]
;
HashSet
<
string
>
arrayTypes
=
new
(
10
)
{
options
.
DefaultRoleClaimType
}
;
foreach
(
var
claim
in
context
.
User
.
Claims
)
{
if
(
replacements
.
TryGetValue
(
claim
.
Type
,
out
var
existingValue
)
)
{
replacements
[
claim
.
Type
]
=
string
.
Concat
(
existingValue
,
","
,
PgConverters
.
SerializeString
(
claim
.
Value
)
)
;
if
(
arrayTypes
.
Contains
(
claim
.
Type
)
is
false
)
{
arrayTypes
.
Add
(
claim
.
Type
)
;
}
}
else
{
replacements
.
Add
(
claim
.
Type
,
PgConverters
.
SerializeString
(
claim
.
Value
)
)
;
}
}
foreach
(
var
item
in
arrayTypes
)
{
if
(
replacements
.
TryGetValue
(
item
,
out
var
existingValue
)
is
true
)
{
replacements
[
item
]
=
string
.
Concat
(
Consts
.
OpenBracket
,
existingValue
,
Consts
.
CloseBracket
)
;
}
else
{
replacements
[
item
]
=
string
.
Concat
(
Consts
.
OpenBracket
,
Consts
.
CloseBracket
)
;
}
}
// Every entry so far is a real claim value - register the "!name" alias each so the
// {!name}/{!name:fallback} forms resolve to the value (a claim value always beats an inline
// fallback). Names containing ':' (URI-style claim types) are skipped: the fallback grammar
// splits on ':' so the bang form can never address them anyway.
if
(
replacements
.
Count
>
0
)
{
foreach
(
var
(
name
,
value
)
in
replacements
.
ToArray
(
)
)
{
if
(
name
.
IndexOf
(
':'
)
<
0
)
{
replacements
[
string
.
Concat
(
"!"
,
name
)
]
=
value
;
}
}
}
if
(
replacements
.
ContainsKey
(
options
.
DefaultUserIdClaimType
)
is
false
)
{
replacements
.
Add
(
options
.
DefaultUserIdClaimType
,
Consts
.
Null
)
;
}
if
(
replacements
.
ContainsKey
(
options
.
DefaultNameClaimType
)
is
false
)
{
replacements
.
Add
(
options
.
DefaultNameClaimType
,
Consts
.
Null
)
;
}
if
(
tokenSet
is
not
null
&&
(
antiforgeryFieldNameTag
is
not
null
||
antiforgeryTokenTag
is
not
null
)
)
{
if
(
antiforgeryFieldNameTag
is
not
null
)
{
replacements
.
Add
(
antiforgeryFieldNameTag
,
tokenSet
.
FormFieldName
)
;
if
(
antiforgeryFieldNameTag
.
IndexOf
(
':'
)
<
0
)
{
replacements
[
string
.
Concat
(
"!"
,
antiforgeryFieldNameTag
)
]
=
tokenSet
.
FormFieldName
;
}
}
if
(
antiforgeryTokenTag
is
not
null
&&
tokenSet
.
RequestToken
is
not
null
)
{
replacements
.
Add
(
antiforgeryTokenTag
,
tokenSet
.
RequestToken
)
;
if
(
antiforgeryTokenTag
.
IndexOf
(
':'
)
<
0
)
{
replacements
[
string
.
Concat
(
"!"
,
antiforgeryTokenTag
)
]
=
tokenSet
.
RequestToken
;
}
}
}
// Listed-but-absent claims fall back to their configured default, or Consts.Null (the
// historical behaviour) when the array form gives no explicit default. Claim values added
// above from context.User.Claims are already present and win via TryAdd - same for their
// "!name" aliases, registered only when a real default exists.
if
(
availableClaims
is
not
null
&&
availableClaims
.
Count
>
0
)
{
foreach
(
var
(
name
,
def
)
in
availableClaims
)
{
replacements
.
TryAdd
(
name
,
def
??
Consts
.
Null
)
;
if
(
def
is
not
null
&&
name
.
IndexOf
(
':'
)
<
0
)
{
replacements
.
TryAdd
(
string
.
Concat
(
"!"
,
name
)
,
def
)
;
}
}
}
// Feed app-wide env-var values into the same dictionary. Per-request claim values added above
// WIN over env vars if the names ever collide (TryAdd does not overwrite).
if
(
_envVarReplacements
.
Count
>
0
)
{
foreach
(
var
(
name
,
value
)
in
_envVarReplacements
)
{
replacements
.
TryAdd
(
name
,
value
)
;
}
}
return
Formatter
.
FormatString
(
input
,
replacements
)
;
}
}
You can’t perform that action at this time.