-
Notifications
You must be signed in to change notification settings - Fork 1
feat(arc): feat(security): implement CSP in Angular frontend #125
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
Open
piyushsinghgaur1
wants to merge
2
commits into
master
Choose a base branch
from
GH-124
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or 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
d762ea2
to
97bfeef
Compare
devyaniofficial
approved these changes
May 23, 2025
Deepika516
approved these changes
May 23, 2025
feat(security): implement CSP in Angular frontend GH-124
… and maintainability refactor(security): refine CSP handling and environment configuration - Replaces all [true] values with [\"'self'\"] in environment.ts and environment.prod.ts for CSP directives, improving clarity and aligning with CSP standards.
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.
@piyushsinghgaur1 I have made the changes mentioned below in this commit. 00bc312 :
Summary of Changes
-
CSP Configuration in Environment Files
- Replaced all
[true]
values with["'self'"]
in bothenvironment.ts
andenvironment.prod.ts
for all CSP directives. - This makes the configuration more readable and directly aligned with CSP standards (e.g.,
defaultSrc: ["'self'"]
instead ofdefaultSrc: [true]
).
- Replaced all
-
CSP Service Logic Simplified
- Removed the logic in
CspService
that mappedtrue
to'self'
. The service now expects the CSP config to use the standard string'self'
. - The service still supports dynamic values via the
env:
prefix (e.g.,connectSrc: ["'self'", "env:baseApiUrl"]
).
- Removed the logic in
-
Meta Tag Handling
- The service updates the existing
<meta http-equiv="Content-Security-Policy">
tag if present, or creates it if not, ensuring only one CSP meta tag is present in the document head.
- The service updates the existing
-
Development Logging
- In development mode, the applied CSP string is logged to the console for easier debugging.
-
Documentation & Comments
- Added comments in the service to clarify the limitations of using meta tags for CSP and to recommend using HTTP headers for production deployments.
-
Provider Cleanup
- Removed a duplicate
APP_INITIALIZER
provider for the CSP service inapp.module.ts
.
- Removed a duplicate
Why These Changes?
- Clarity: Using
["'self'"]
is much more readable and standard for anyone familiar with CSP. - Maintainability: The service logic is now simpler and easier to maintain.
- Best Practices: The code now better reflects CSP best practices and is easier for new developers to understand and extend.
- Developer Experience: Logging and comments help with debugging and onboarding.
Next Steps / Recommendations
- For maximum security, set CSP via HTTP headers on your web server in production, as meta tags are less secure and not all directives are supported.
- If you need to allow additional sources (e.g., fonts, APIs), add them explicitly in the environment config as strings.
Example CSP config after changes:
csp: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'"],
connectSrc: ["'self'", 'env:baseApiUrl'],
objectSrc: ['none'],
baseUri: ["'self'"],
formAction: ["'self'"],
blockAllMixedContent: true,
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR enhances the security of the Angular frontend by dynamically injecting a Content Security Policy (CSP) using a custom
CspService
. Instead of hardcoding the CSP inindex.html
, this service programmatically creates and inserts a<meta http-equiv="Content-Security-Policy">
tag at runtime.Key Highlights:
CspService
to dynamically insert CSP meta tag into the document headdefault-src 'self'
) to block untrusted contenteval
Why This Matters:
By dynamically managing CSP, we maintain greater flexibility (e.g., environment-specific policies), and ensure stronger defense against XSS, code injection, and content spoofing attacks.
Fixes: #124
Type of change
Checklist: