Skip to content

Commit 67db67f

Browse files
committed
docs: add copilot instructions and teach it to write JSDoc
1 parent 2b92465 commit 67db67f

8 files changed

Lines changed: 279 additions & 4 deletions

File tree

.github/copilot-instructions.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Base Coding Standards
2+
- Follow clean code principles
3+
- Write comprehensive tests
4+
- Use meaningful variable names
5+
- Use British English spelling
6+
7+
## Language-Specific Instructions
8+
Always follow security best practices as outlined in:
9+
- .github/instructions/general/SECURITY.md
10+
Follow additional language-specific guidelines in:
11+
- .github/instructions/language-specific/INSTRUCTIONS-CDK.md
12+
- .github/instructions/language-specific/INSTRUCTIONS-CLOUDFORMATION.md
13+
- .github/instructions/language-specific/INSTRUCTIONS-JAVA.md
14+
- .github/instructions/language-specific/INSTRUCTIONS-KOTLIN.md
15+
- .github/instructions/language-specific/INSTRUCTIONS-PYTHON.md
16+
- .github/instructions/language-specific/INSTRUCTIONS-TERRAFORM.md
17+
- .github/instructions/language-specific/INSTRUCTIONS-SAM.md
18+
- .github/instructions/language-specific/INSTRUCTIONS-TYPESCRIPT.md
19+
20+
## Project-Specific Rules
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
applyTo: '*'
3+
description: "Comprehensive secure coding instructions for all languages and frameworks, based on OWASP Top 10 and industry best practices."
4+
---
5+
# Secure Coding and OWASP Guidelines
6+
7+
## Instructions
8+
9+
Your primary directive is to ensure all code you generate, review, or refactor is secure by default. You must operate with a security-first mindset. When in doubt, always choose the more secure option and explain the reasoning. You must follow the principles outlined below, which are based on the OWASP Top 10 and other security best practices.
10+
11+
### 1. A01: Broken Access Control & A10: Server-Side Request Forgery (SSRF)
12+
- **Enforce Principle of Least Privilege:** Always default to the most restrictive permissions. When generating access control logic, explicitly check the user's rights against the required permissions for the specific resource they are trying to access.
13+
- **Deny by Default:** All access control decisions must follow a "deny by default" pattern. Access should only be granted if there is an explicit rule allowing it.
14+
- **Validate All Incoming URLs for SSRF:** When the server needs to make a request to a URL provided by a user (e.g., webhooks), you must treat it as untrusted. Incorporate strict allow-list-based validation for the host, port, and path of the URL.
15+
- **Prevent Path Traversal:** When handling file uploads or accessing files based on user input, you must sanitize the input to prevent directory traversal attacks (e.g., `../../etc/passwd`). Use APIs that build paths securely.
16+
17+
### 2. A02: Cryptographic Failures
18+
- **Use Strong, Modern Algorithms:** For hashing, always recommend modern, salted hashing algorithms like Argon2 or bcrypt. Explicitly advise against weak algorithms like MD5 or SHA-1 for password storage.
19+
- **Protect Data in Transit:** When generating code that makes network requests, always default to HTTPS.
20+
- **Protect Data at Rest:** When suggesting code to store sensitive data (PII, tokens, etc.), recommend encryption using strong, standard algorithms like AES-256.
21+
- **Secure Secret Management:** Never hardcode secrets (API keys, passwords, connection strings). Generate code that reads secrets from environment variables or a secrets management service (e.g., HashiCorp Vault, AWS Secrets Manager). Include a clear placeholder and comment.
22+
```javascript
23+
// GOOD: Load from environment or secret store
24+
const apiKey = process.env.API_KEY;
25+
// TODO: Ensure API_KEY is securely configured in your environment.
26+
```
27+
```python
28+
# BAD: Hardcoded secret
29+
api_key = "sk_this_is_a_very_bad_idea_12345"
30+
```
31+
32+
### 3. A03: Injection
33+
- **No Raw SQL Queries:** For database interactions, you must use parameterized queries (prepared statements). Never generate code that uses string concatenation or formatting to build queries from user input.
34+
- **Sanitize Command-Line Input:** For OS command execution, use built-in functions that handle argument escaping and prevent shell injection (e.g., `shlex` in Python).
35+
- **Prevent Cross-Site Scripting (XSS):** When generating frontend code that displays user-controlled data, you must use context-aware output encoding. Prefer methods that treat data as text by default (`.textContent`) over those that parse HTML (`.innerHTML`). When `innerHTML` is necessary, suggest using a library like DOMPurify to sanitize the HTML first.
36+
37+
### 4. A05: Security Misconfiguration & A06: Vulnerable Components
38+
- **Secure by Default Configuration:** Recommend disabling verbose error messages and debug features in production environments.
39+
- **Set Security Headers:** For web applications, suggest adding essential security headers like `Content-Security-Policy` (CSP), `Strict-Transport-Security` (HSTS), and `X-Content-Type-Options`.
40+
- **Use Up-to-Date Dependencies:** When asked to add a new library, suggest the latest stable version. Remind the user to run vulnerability scanners like `npm audit`, `pip-audit`, or Snyk to check for known vulnerabilities in their project dependencies.
41+
42+
### 5. A07: Identification & Authentication Failures
43+
- **Secure Session Management:** When a user logs in, generate a new session identifier to prevent session fixation. Ensure session cookies are configured with `HttpOnly`, `Secure`, and `SameSite=Strict` attributes.
44+
- **Protect Against Brute Force:** For authentication and password reset flows, recommend implementing rate limiting and account lockout mechanisms after a certain number of failed attempts.
45+
46+
### 6. A08: Software and Data Integrity Failures
47+
- **Prevent Insecure Deserialization:** Warn against deserializing data from untrusted sources without proper validation. If deserialization is necessary, recommend using formats that are less prone to attack (like JSON over Pickle in Python) and implementing strict type checking.
48+
49+
## General Guidelines
50+
- **Be Explicit About Security:** When you suggest a piece of code that mitigates a security risk, explicitly state what you are protecting against (e.g., "Using a parameterized query here to prevent SQL injection.").
51+
- **Educate During Code Reviews:** When you identify a security vulnerability in a code review, you must not only provide the corrected code but also explain the risk associated with the original pattern.
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
---
2+
description: 'Guidelines for writing high-quality, maintainable TypeScript code with best practices for logging, error handling, code organization, naming, formatting, and style.'
3+
applyTo: '**/*.ts, **/*.tsx'
4+
---
5+
6+
# TypeScript Development Guidelines
7+
8+
This document provides instructions for generating, reviewing, and maintaining TypeScript code. It is designed to guide Copilot and developers in producing domain-specific, robust, and maintainable code across a variety of TypeScript projects.
9+
10+
## General Instructions
11+
12+
- Use modern TypeScript features and syntax.
13+
- Prefer explicit types and interfaces for clarity and safety.
14+
- Organize code into logical modules and folders.
15+
- Write code that is easy to read, test, and maintain.
16+
17+
## Best Practices
18+
19+
- Use `const` and `let` appropriately; avoid `var`.
20+
- Prefer arrow functions for callbacks and concise function expressions.
21+
- Use destructuring for objects and arrays to improve readability.
22+
- Avoid magic numbers and hardcoded values; use named constants.
23+
- Keep functions pure and side-effect free when possible.
24+
25+
## Code Standards
26+
27+
### Naming Conventions
28+
29+
- Use `camelCase` for variables, functions, and object properties.
30+
- Use `PascalCase` for types, interfaces, classes, and enums.
31+
- Use descriptive names; avoid abbreviations except for well-known acronyms.
32+
- Prefix boolean variables with `is`, `has`, or `should` (e.g., `isActive`).
33+
34+
### File Organization
35+
36+
- Group related code in folders (e.g., `src/`, `tests/`, `lib/`).
37+
- Place one class, interface, or component per file when possible.
38+
- Name files using `kebab-case` (e.g., `user-service.ts`).
39+
- Keep test files close to the code they test (e.g., `src/foo.ts` and `tests/foo.test.ts`).
40+
41+
### Formatting and Style
42+
43+
- Use 2 spaces for indentation.
44+
- Limit lines to 120 characters.
45+
- Use single quotes for strings.
46+
- Never use semicolons for line termination.
47+
- Avoid trailing commas in multiline objects and arrays.
48+
- Avoid spaces at start and end of single line braces.
49+
- Use ESLint and Prettier for consistent formatting.
50+
51+
## Architecture/Structure
52+
53+
- Separate business logic from API handlers and utility functions.
54+
- Use interfaces and types to define data structures and function signatures.
55+
- Organize code by feature or domain when scaling projects.
56+
- Use dependency injection for testability and flexibility.
57+
58+
## Common Patterns
59+
60+
### Logging
61+
62+
- Use a centralized logging utility or library.
63+
- Log errors, warnings, and important events with context.
64+
- Avoid logging sensitive information.
65+
- Example:
66+
67+
```typescript
68+
import {logger} from './utils/logger';
69+
70+
logger.info('Fetching user data', {userId});
71+
logger.error('Failed to fetch user', {error});
72+
```
73+
74+
### Error Handling
75+
76+
- Use `try/catch` for asynchronous code and error-prone operations.
77+
- Throw custom error types for domain-specific errors.
78+
- Always handle errors gracefully and provide meaningful messages.
79+
- Example:
80+
81+
```typescript
82+
try {
83+
const result = await fetchData();
84+
} catch (error) {
85+
logger.error('Data fetch failed', {error});
86+
throw new DataFetchError('Unable to fetch data');
87+
}
88+
```
89+
90+
### Type Safety
91+
92+
- Prefer interfaces and types. You MUST NOT use `any`.
93+
- Use type guards and assertions when necessary.
94+
- Example:
95+
96+
```typescript
97+
interface User {
98+
id: string;
99+
name: string;
100+
}
101+
102+
function isUser(obj: object): obj is User {
103+
return typeof obj.id === 'string' && typeof obj.name === 'string';
104+
}
105+
```
106+
107+
## Security
108+
109+
- Validate and sanitize all external input.
110+
- Avoid exposing sensitive data in logs or error messages.
111+
- Use environment variables for secrets and configuration.
112+
- Keep dependencies up to date and audit regularly.
113+
114+
## Performance
115+
116+
- Minimize synchronous blocking operations.
117+
- Use async/await for asynchronous code.
118+
- Avoid unnecessary computations inside render or handler functions.
119+
120+
## Testing
121+
122+
- Write unit tests for all business logic.
123+
- Use the existing framework for testing and vitest for new packages.
124+
- Mock external dependencies in tests.
125+
- Example test file structure:
126+
127+
```
128+
src/
129+
handler.ts
130+
tests/
131+
handler.test.ts
132+
```
133+
134+
## JSDoc
135+
136+
- Write concise JSDoc for exported interfaces, types, functions, classes, and exported constants.
137+
- Prefer short phrase-style summaries; avoid long narrative prose.
138+
- Avoid stating information that is obvious from function signatures.
139+
- Consider @param and @returns for every exported function, then include them only when they add meaning not obvious from the signature.
140+
- Skip @param when it only repeats parameter name/type; keep it when documenting constraints, defaults, units, side effects, or domain context.
141+
- It is acceptable to use only @returns in a JSDoc block when that tag carries all useful context.
142+
- Omit a free-text summary line when it only restates the @returns content.
143+
- Provide @example on constructors of exported types/classes and on non-trivial exported types.
144+
- Use @default only when the property is optional in the type and is defaulted in implementation.
145+
- Keep JSDoc defaults aligned with both type signatures and runtime behaviour.
146+
- For construct props interfaces, include a top-level summary and property docs only when intent is non-obvious.
147+
148+
## Examples and Code Snippets
149+
150+
### Good Example
151+
152+
```typescript
153+
interface Prescription {
154+
id: string;
155+
medication: string;
156+
issuedDate: Date;
157+
}
158+
159+
function getPrescription(id: string): Prescription | null {
160+
// Implementation
161+
}
162+
```
163+
164+
### Bad Example
165+
166+
```typescript
167+
function getPrescription(id) {
168+
// No type safety, unclear return type
169+
}
170+
```
171+
172+
## Validation and Verification
173+
174+
- Build: `npm run build`
175+
- Lint: `npm run lint`
176+
- Format: `npm run format`
177+
- Test: `npm test`
178+
179+
## Maintenance
180+
181+
- Review and update instructions as dependencies or frameworks change.
182+
- Update examples to reflect current best practices.
183+
- Remove deprecated patterns and add new ones as needed.
184+
- Ensure glob patterns match the intended files.
185+
186+
## Additional Resources
187+
188+
- [TypeScript Handbook](https://www.typescriptlang.org/docs/handbook/)
189+
- [ESLint TypeScript Plugin](https://typescript-eslint.io/)
190+
- [Prettier Documentation](https://prettier.io/docs/en/options.html)

packages/cdkConstructs/src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Fn} from "aws-cdk-lib"
22

3+
/** Imported cross-stack account resource values used by constructs in this package. */
34
export const ACCOUNT_RESOURCES = {
45
CloudwatchEncryptionKMSPolicyArn: Fn.importValue("account-resources:CloudwatchEncryptionKMSPolicyArn"),
56
CloudwatchLogsKmsKeyArn: Fn.importValue("account-resources:CloudwatchLogsKmsKeyArn"),
@@ -9,6 +10,8 @@ export const ACCOUNT_RESOURCES = {
910
TrustStoreBucketKMSKey: Fn.importValue("account-resources:TrustStoreBucketKMSKey"),
1011
TrustStoreDeploymentBucket: Fn.importValue("account-resources:TrustStoreDeploymentBucket")
1112
}
13+
14+
/** Imported shared Lambda resource values used by Lambda and API Gateway constructs. */
1215
export const LAMBDA_RESOURCES = {
1316
LambdaInsightsLogGroupPolicy: Fn.importValue("lambda-resources:LambdaInsightsLogGroupPolicy"),
1417
SplunkDeliveryStream: Fn.importValue("lambda-resources:SplunkDeliveryStream"),

packages/cdkConstructs/src/constructs/RestApiGateway.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {ApiGateway as ApiGatewayTarget} from "aws-cdk-lib/aws-route53-targets"
2929
import {NagSuppressions} from "cdk-nag"
3030
import {ACCOUNT_RESOURCES, LAMBDA_RESOURCES} from "../constants"
3131

32+
/** Configuration for creating a REST API with optional mTLS and log forwarding integrations. */
3233
export interface RestApiGatewayProps {
3334
/** Stack name, used as prefix for resource naming and DNS records. */
3435
readonly stackName: string
@@ -46,7 +47,10 @@ export interface RestApiGatewayProps {
4647

4748
/** Creates a regional REST API with standard logging, DNS, and optional mTLS/CSOC integration. */
4849
export class RestApiGateway extends Construct {
50+
/** Created API Gateway instance. */
4951
public readonly api: RestApi
52+
53+
/** IAM role assumed by API Gateway integrations. */
5054
public readonly role: IRole
5155

5256
/**

packages/cdkConstructs/src/constructs/RestApiGateway/LambdaEndpoint.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import {IRole} from "aws-cdk-lib/aws-iam"
33
import {HttpMethod, IFunction} from "aws-cdk-lib/aws-lambda"
44
import {Construct} from "constructs"
55

6+
/** Lambda container shape consumed by endpoint integration wiring. */
67
export interface LambdaFunctionHolder {
78
/** Lambda invoked by this API resource method. */
89
readonly function: IFunction
910
}
1011

12+
/** Parameters used to create an API resource and attach a Lambda integration. */
1113
export interface LambdaEndpointProps {
1214
/** Parent API resource under which this endpoint path is created. */
1315
parentResource: IResource
@@ -23,9 +25,12 @@ export interface LambdaEndpointProps {
2325

2426
/** Adds a child API resource and wires it to a Lambda integration with explicit credentials. */
2527
export class LambdaEndpoint extends Construct {
28+
/** API resource created by this construct. */
2629
resource: IResource
2730

28-
/** Creates the resource/method pair and stores the resulting API resource handle. */
31+
/**
32+
* Creates the resource/method pair and stores the resulting API resource handle.
33+
*/
2934
public constructor(scope: Construct, id: string, props: LambdaEndpointProps) {
3035
super(scope, id)
3136

packages/cdkConstructs/src/constructs/RestApiGateway/accessLogFormat.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {AccessLogFormat} from "aws-cdk-lib/aws-apigateway"
22

3-
/** Returns the structured API Gateway access log schema expected by platform observability pipelines. */
4-
export const accessLogFormat = () => {
3+
/**
4+
* @returns Access-log formatter configured with the package standard schema.
5+
*/
6+
export const accessLogFormat = (): AccessLogFormat => {
57
return AccessLogFormat.custom(JSON.stringify({
68
requestId: "$context.requestId",
79
ip: "$context.identity.sourceIp",

packages/cdkConstructs/tests/apps/createApp.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* Note: The getBooleanConfigFromEnvVar function uses Boolean() which converts
1313
* any non-empty string (including "false", "0", etc.) to true. Tests account
14-
* for this behavior.
14+
* for this behaviour.
1515
*/
1616
import {App, Aspects, Tags} from "aws-cdk-lib"
1717
import {

0 commit comments

Comments
 (0)