Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

fetch NamespaceSelector namespaces from GraphQL (viewer { affiliatedNamespaces })#63592

Merged
sqs merged 1 commit into
mainfrom
sqs/namespace-gql
Jul 4, 2024
Merged

fetch NamespaceSelector namespaces from GraphQL (viewer { affiliatedNamespaces })#63592
sqs merged 1 commit into
mainfrom
sqs/namespace-gql

Conversation

@sqs

@sqs sqs commented Jul 2, 2024

Copy link
Copy Markdown
Member

Previously, the Batch Changes namespace selector looked at the user's settings that were loaded on the initial page load. This bypassed the GraphQL API, which should be the source of truth for "what namespaces can a user write to" (i.e., their user account and all orgs they are a member of), and introduced a reliance on non-type-safe jscontext data. Also, adding any other features to this NamespaceSelector would require changing the data fetched in jscontext, so it was brittle.

I intend to use the NamespaceSelector for more things that need these missing features and a more solid foundation (such as the ability to transfer saved searches to a different owner), so I started out by refactoring this to access data from the GraphQL API. I added a new viewer { affiliatedNamespaces } GraphQL API field to support this.

Also adds tests and a storybook for this NamespaceSelector component.

Test plan

Test plan:

  1. Go to the Batch Changes global create page. Create a batch change. Ensure it works if you use the initial/default selected namespace (your user account), and if you change it. Ensure the created batch change exists in the intended namespace.
  2. Do the same, but from a user or org batch changes create page. Ensure the NamespaceSelector's initial option is the namespace you are visiting, and that creation succeeds.

@cla-bot cla-bot Bot added the cla-signed label Jul 2, 2024
@sqs sqs requested review from a team July 2, 2024 03:07
@sqs sqs force-pushed the sqs/namespace-gql branch from 7b2990e to 0de5244 Compare July 2, 2024 03:09
Comment thread cmd/frontend/graphqlbackend/viewer.go Outdated
Comment on lines 7 to 25

Check notice

Code scanning / Semgrep OSS

Semgrep Finding: security-semgrep-rules.semgrep-rules.golang.unauthorized-graphql-resolver-rule

The graphql resolver function may not contain authorization check, please review for authorization bugs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this alert might be correct - outside of dotcom we probably want to return an auth error here 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, good call. I agree, but I want to confirm we are thinking for the same reason:

Even though the GraphQL API would be completely inaccessible to any unauthenticated client outside of dotcom, for defense in depth and limiting the negative consequences of a misconfiguration, it is good to restrict this to dotcom only.

(Let me know if there is something else.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh! I actually misremembered that a subset of the GQL API was publicly accessible - that doesn't seem to be the case though:

curl -X POST https://sourcegraph.sourcegraph.com/.api/graphql -d '{}'

Private mode requires authentication.

So I think this would not actually be an issue. Maybe we can instead add a comment that the user == nil && err == nil case is implicitly dotcom-only - but not a blocker

Comment thread cmd/frontend/graphqlbackend/affiliated_namespaces.go Fixed
@sqs sqs force-pushed the sqs/namespace-gql branch from 0de5244 to bebd4a5 Compare July 2, 2024 03:14
Comment thread cmd/frontend/graphqlbackend/viewer.go Outdated
Comment on lines 7 to 25

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this alert might be correct - outside of dotcom we probably want to return an auth error here 🤔

Comment thread cmd/frontend/graphqlbackend/viewer.graphql
Comment thread cmd/frontend/graphqlbackend/viewer.graphql
Comment thread cmd/frontend/graphqlbackend/viewer.graphql Outdated
@sqs sqs force-pushed the sqs/namespace-gql branch from bebd4a5 to 2b83ca4 Compare July 4, 2024 04:10
Comment on lines 16 to 30

Check notice

Code scanning / Semgrep OSS

Semgrep Finding: security-semgrep-rules.semgrep-rules.generic.comment-tagging-rule

Code that highlight SECURITY in comment has changed. Please review the code for changes. The changes might be sensitive.
Comment thread cmd/frontend/graphqlbackend/viewer.go Outdated
Comment on lines 19 to 24

Check notice

Code scanning / Semgrep OSS

Semgrep Finding: security-semgrep-rules.semgrep-rules.generic.comment-tagging-rule

Code that highlight SECURITY in comment has changed. Please review the code for changes. The changes might be sensitive.
Comment thread internal/auth/visitor.go Outdated
Comment on lines 17 to 21

Check notice

Code scanning / Semgrep OSS

Semgrep Finding: security-semgrep-rules.semgrep-rules.generic.comment-tagging-rule

Code that highlight SECURITY in comment has changed. Please review the code for changes. The changes might be sensitive.
@sqs sqs force-pushed the sqs/namespace-gql branch 3 times, most recently from 15547fe to f846bee Compare July 4, 2024 06:21
Previously, the Batch Changes namespace selector looked at the user's settings that were loaded on the initial page load. This bypassed the GraphQL API, which should be the source of truth for "what namespaces can a user write to" (i.e., their user account and all orgs they are a member of), and introduced a reliance on non-type-safe `jscontext` data. Also, adding any other features to this NamespaceSelector would require changing the data fetched in `jscontext`, so it was brittle.

I intend to use the NamespaceSelector for more things that need these missing features and a more solid foundation (such as the ability to transfer saved searches to a different owner), so I started out by refactoring this to access data from the GraphQL API. I added a new `viewer { affiliatedNamespaces }` GraphQL API field to support this.

Test plan:

1. Go to the Batch Changes global create page. Create a batch change. Ensure it works if you use the initial/default selected namespace (your user account), and if you change it. Ensure the created batch change exists in the intended namespace.
2. Do the same, but from a user or org batch changes create page. Ensure the NamespaceSelector's initial option is the namespace you are visiting, and that creation succeeds.
@sqs sqs force-pushed the sqs/namespace-gql branch from f846bee to e7c565c Compare July 4, 2024 06:34
@sqs sqs merged commit f2e648b into main Jul 4, 2024
@sqs sqs deleted the sqs/namespace-gql branch July 4, 2024 06:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants