-
Notifications
You must be signed in to change notification settings - Fork 2
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
RK-20569 - CORS rework for Dynatrace apps #387
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ac28c5a
Better regex
ron-rookout ae3f3c0
Moved CORS middleware to its own file
ron-rookout 1239803
Added cache + small refactor
ron-rookout ded595e
Bump version
ron-rookout f57614c
Add debug option to skip dt verification
ron-rookout 8f162ed
Formatting
ron-rookout File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import * as cors from "cors"; | ||
import fetch from "node-fetch"; | ||
import { getStoreSafe } from "./explorook-store"; | ||
|
||
|
||
const LOCALHOST_ORIGIN = "https://localhost:8080"; | ||
const ROOKOUT_ORIGIN_REGEX = /^https:\/\/.*\.rookout(?:-dev)?\.com$/; | ||
const DYNATRACE_ORIGIN_REGEX = /^https:\/\/.*\.dynatrace(?:labs)?\.com$/; | ||
|
||
const ALLOW_CORS_OPTION: cors.CorsOptions = {origin: true}; | ||
const DENY_CORS_OPTION: cors.CorsOptions = {origin: false}; | ||
|
||
|
||
const verifiedOriginsCache = new Set([LOCALHOST_ORIGIN]); | ||
|
||
const store = getStoreSafe(); | ||
|
||
const corsOptionsDelegate = async (req: cors.CorsRequest, callback: (err: Error | null, options?: cors.CorsOptions) => void) => { | ||
try { | ||
const shouldSkipDtVerification = store.get("skipDtVerification", false); | ||
const origin = req.headers.origin; | ||
if (verifiedOriginsCache.has(origin) || ROOKOUT_ORIGIN_REGEX.test(origin)) { | ||
callback(null, ALLOW_CORS_OPTION); | ||
return; | ||
} | ||
|
||
if (!DYNATRACE_ORIGIN_REGEX.test(origin)) { | ||
callback(null, DENY_CORS_OPTION); | ||
return; | ||
} | ||
|
||
|
||
if (shouldSkipDtVerification) { | ||
callback(null, ALLOW_CORS_OPTION); | ||
return; | ||
} | ||
|
||
|
||
const response = await fetch(`${origin}/platform-reserved/dob/isapprefallowed?appOrigin=${origin}`); | ||
ron-rookout marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (!response.ok) { | ||
callback(null, DENY_CORS_OPTION); | ||
return; | ||
} | ||
|
||
verifiedOriginsCache.add(origin); | ||
callback(null, ALLOW_CORS_OPTION); | ||
} catch (err) { | ||
callback(err, DENY_CORS_OPTION); | ||
} | ||
}; | ||
|
||
|
||
export const getCorsMiddleware = () => { | ||
return cors(corsOptionsDelegate); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,13 +2,13 @@ import { makeExecutableSchema } from "@graphql-tools/schema"; | |||||
import { ApolloServerPluginLandingPageDisabled } from "apollo-server-core"; | ||||||
import { ApolloServer } from "apollo-server-express"; | ||||||
import * as bodyParser from "body-parser"; | ||||||
import * as cors from "cors"; | ||||||
import * as express from "express"; | ||||||
import { readFileSync } from "fs"; | ||||||
import { applyMiddleware } from "graphql-middleware"; | ||||||
import * as http from "http"; | ||||||
import { join } from "path"; | ||||||
import { resolvers } from "./api"; | ||||||
import {getCorsMiddleware} from "./cors"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
import { notify } from "./exceptionManager"; | ||||||
import { | ||||||
filterDirTraversal, | ||||||
|
@@ -32,17 +32,6 @@ const defaultOptions: StartOptions = { | |||||
port: 44512 | ||||||
}; | ||||||
|
||||||
const corsDomainWhitelist = [ | ||||||
/^https:\/\/.*\.rookout\.com$/, | ||||||
/^https:\/\/.*\.rookout-dev\.com$/, | ||||||
/^https:\/\/.*\.dynatracelabs\.com$/, | ||||||
/^https:\/\/.*\.dynatrace\.com$/, | ||||||
"https://localhost:8080" | ||||||
]; | ||||||
|
||||||
const corsOptions = { | ||||||
origin: corsDomainWhitelist | ||||||
}; | ||||||
|
||||||
export const start = async (options: StartOptions) => { | ||||||
const settings = { ...options, ...defaultOptions }; | ||||||
|
@@ -71,7 +60,7 @@ export const start = async (options: StartOptions) => { | |||||
cache: "bounded" | ||||||
}); | ||||||
|
||||||
app.use(cors(corsOptions)); | ||||||
app.use(getCorsMiddleware()); | ||||||
app.use(bodyParser.json()); | ||||||
|
||||||
await apolloServer.start(); | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add an env variable to skip the HTTP check
@ElDuderinos
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, let's cache the result for a while :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The results are cached (if successful), take a look at line 37.
I didn't want to cache failed results because it might be because of a network issue or timeout or something so I didn't want to prevent it from retrying.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LiranLast @ElDuderinos As a packaged Electron app can't read system env variables during runtime, I added an option in the debug menu (when you right click the X) to toggle skipping verification. Should I change the text to something more clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ron-rookout It may be possible to read env using a package, I remember running into it while patching lang servers
Believe it is called electron-env
Also note this problem mainly occurs in macOs if I recall correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Urook I didn't find this one specifically, I have searched for other ways and couldn't find any either