Skip to content

Commit

Permalink
fix(3144): add correct auth validation traces (#3152)
Browse files Browse the repository at this point in the history
  • Loading branch information
dekkku authored Nov 27, 2024
1 parent c316311 commit 47d6b0c
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/core/blueprint/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,22 +401,49 @@ fn to_fields(
GraphQLOperationType::Query
};
// Process fields that are not marked as `omit`

// collect the parent auth ids
let parent_auth_ids = type_of.protected.as_ref().and_then(|p| p.id.as_ref());
// collect the field names that have different auth ids than the parent type
let fields_with_different_auth_ids = type_of
.fields
.iter()
.filter_map(|(k, v)| {
if let Some(p) = &v.protected {
if p.id.as_ref() != parent_auth_ids {
Some(k)
} else {
None
}
} else {
None
}
})
.collect::<HashSet<_>>();

let fields = Valid::from_iter(
type_of
.fields
.iter()
.filter(|(_, field)| !field.is_omitted()),
|(name, field)| {
validate_field_type_exist(config_module, field)
.and(to_field_definition(
let mut result =
validate_field_type_exist(config_module, field).and(to_field_definition(
field,
&operation_type,
object_name,
config_module,
type_of,
name,
))
.trace(name)
));

if fields_with_different_auth_ids.contains(name) || parent_auth_ids.is_none() {
// if the field has a different auth id than the parent type or parent has no
// auth id, we need to add correct trace.
result = result.trace(name);
}

result
},
);

Expand Down
86 changes: 86 additions & 0 deletions tests/core/snapshots/auth-validations.md_error.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
source: tests/core/spec.rs
expression: errors
---
[
{
"message": "Auth provider z not found",
"trace": [
"Baz",
"x",
"@protected"
],
"description": null
},
{
"message": "Auth provider y not found",
"trace": [
"Baz",
"y",
"@protected"
],
"description": null
},
{
"message": "Auth provider x not found",
"trace": [
"Baz",
"z",
"@protected"
],
"description": null
},
{
"message": "Auth provider x not found",
"trace": [
"Foo",
"@protected"
],
"description": null
},
{
"message": "Auth provider x not found",
"trace": [
"Foo",
"baz",
"@protected"
],
"description": null
},
{
"message": "Auth provider y not found",
"trace": [
"Foo",
"baz",
"@protected"
],
"description": null
},
{
"message": "Auth provider b not found",
"trace": [
"Query",
"default",
"@protected"
],
"description": null
},
{
"message": "Auth provider c not found",
"trace": [
"Query",
"default",
"@protected"
],
"description": null
},
{
"message": "Auth provider z not found",
"trace": [
"Zoo",
"a",
"@protected"
],
"description": null
}
]
36 changes: 36 additions & 0 deletions tests/execution/auth-validations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
error: true
---

# auth multiple

```graphql @config
schema @server @upstream @link(id: "a", src: ".htpasswd_a", type: Htpasswd) {
query: Query
}

type Query {
default: String @expr(body: "data") @protected(id: ["a", "b", "c"])
foo: Foo @expr(body: {bar: "baz"})
}

type Foo @protected(id: ["x"]) {
bar: String
baz: String @protected(id: ["y"])
}

type Zoo {
a: String @protected(id: ["z"])
}

type Baz {
x: String @protected(id: ["z"])
y: String @protected(id: ["y"])
z: String @protected(id: ["x"])
}
```

```text @file:.htpasswd_a
testuser1:$apr1$e3dp9qh2$fFIfHU9bilvVZBl8TxKzL/
testuser2:$2y$10$wJ/mZDURcAOBIrswCAKFsO0Nk7BpHmWl/XuhF7lNm3gBAFH3ofsuu
```

1 comment on commit 47d6b0c

@github-actions
Copy link

Choose a reason for hiding this comment

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

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 4.12ms 2.00ms 28.25ms 79.91%
Req/Sec 6.25k 0.88k 7.24k 94.50%

746319 requests in 30.01s, 3.74GB read

Requests/sec: 24870.03

Transfer/sec: 127.65MB

Please sign in to comment.