Security Design for Safe Operations

Defense-in-depth security architecture that small businesses can trust

SecurityToken AuthenticationRate LimitingCredential ManagementAccess Control
4 min read

About This Topic

In business systems, security is paramount. Unauthorized access or data leaks can damage a company's reputation. This project adopted a defense-in-depth security design to ensure small businesses can use it with confidence.

Defense in Depth

Relying on a single defense measure is risky. By defending with multiple layers, even if one is breached, the next layer provides protection.

Four-Layer Security Structure
Layer 1: Token Authentication

Verify legitimate access with secret token

Pass
Layer 2: Rate Limiting

Block mass requests in short timeframes

Pass
Layer 3: Input Validation

Reject invalid data before processing

Pass
Layer 4: Secure Credential Management

Encrypted storage + auto-renewal

Layer 1: Token Authentication

Communication from GAS uses a secret token of 32+ characters. Access from third parties who don't know this token is automatically rejected.

Token length
Details32+ characters (hard to guess)
Transmission method
DetailsIncluded in HTTP header
Verification location
DetailsCompared with server-side environment variable
On mismatch
DetailsImmediately return 401 Unauthorized

Token Management Tips

Layer 2: Rate Limiting

A mechanism that automatically blocks a flood of requests in a short time.

Rate Limiting Operation
Request Received

Identify by client IP + last 8 characters of token

Check Count

Check request count in the past minute

Under 60
Allow and increment count
60 or more
Return 429 Too Many Requests

What Rate Limiting Prevents

  • Malicious mass access: Denial of service attacks
  • Operational errors: Duplicate processing from button mashing
  • Infinite loops: Endless requests from program bugs

Response Header Notifications

API responses include the remaining request count.

X-RateLimit-Remaining
MeaningRemaining requests
Example45
X-RateLimit-Reset
MeaningReset time (Unix timestamp)
Example1705363200

Layer 3: Input Validation

Strictly check if submitted data is in the correct format before processing.

Date
Validationyyyy-mm-dd format
Invalid Example2024/01/15 (slash separator)
Amount
ValidationNumber (up to 3 decimal places)
Invalid Example1,000 (with comma)
Tags
ValidationMaximum 10
Invalid Example11 or more tags
Idempotency Key
Validation8+ characters
Invalid ExampleKey too short

When Validation Finds Issues

Returns an error before processing begins, preventing invalid data from being sent to the invoicing service.

Layer 4: Secure Credential Management

Credentials (access tokens) needed to connect to the invoicing service require special handling.

Three Credential Management Techniques

Encrypted storage
DescriptionStore in encrypted cloud storage like Redis
BenefitReduce leak risk
Auto-renewal
DescriptionAutomatically get new token 60 seconds before expiry
BenefitNo manual management
Distributed lock
DescriptionPrevent duplicate updates even with concurrent access
BenefitPrevent token inconsistency
Token Auto-Renewal Flow
Token Retrieval Request

Get token before API call

Check Expiry

More than 60 seconds remaining?

Valid
Use cached token
Near expiry
Acquire distributed lock and refresh
API Call

Make request with valid token

How Distributed Locking Works

If multiple requests try to refresh the token simultaneously:

  • Same token refreshed multiple times (wasted API calls)
  • Old and new tokens mixed (inconsistency)

Distributed locking ensures only one instance can refresh the token at a time.

Security Design Summary

Unauthorized access
CountermeasureToken authentication
LayerLayer 1
DDoS attacks
CountermeasureRate limiting
LayerLayer 2
Invalid input
CountermeasureInput validation
LayerLayer 3
Credential leak
CountermeasureEncrypted storage + auto-renewal
LayerLayer 4

What This Design Achieves

For Operations

  • Reduced security workload: Automated protection mechanisms
  • Easy incident response: Just change the token
  • Audit compliance: Track history with structured logs

For Users

  • Safe to use: Protected by defense in depth
  • Transparency: Know remaining rate limit
  • Quick error notification: Immediate awareness of issues

Related Topics