Skip to content
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

Add security page #51

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions docs/pages/infra/platform/security/protect-api-keys.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# How to Protect Your API Keys

Keeping your API keys secure is essential. Here are two main ways to protect them:

- Restrict access to your API keys.
- Use a proxy server to handle requests to Pimlico.

## Restrict Access to Your API Keys

You can limit how your API keys are used by modifying their permissions on the [API Keys page](https://dashboard.pimlico.io/apikeys). Restrictions can include:

- **IP addresses:** Specify which IPs are allowed to make requests.
- **User agents:** Limit access to specific browsers, SDK versions, or other user agents.
- **Origins:** Define which domains are permitted to make requests.

Additionally, you can enable or disable specific API features for each key, such as:

- Bundler methods.
- Paymaster methods.
- Account APIs.

## Use a Proxy Server

You can create a proxy server to handle requests to Pimlico. This way, you can have custom authentication, rate limiting, and other features before forwarding requests to Pimlico.

Here's an example of how you can create a proxy server for `fastify` and `express`:

::::code-group

```typescript [fastify.ts]
import Fastify from 'fastify'
import proxy from '@fastify/http-proxy'

const fastify = Fastify({ logger: true })
const PIMLICO_API_KEY = process.env.PIMLICO_API_KEY

// Middleware to check authentication
fastify.addHook('preHandler', async (request, reply) => {
const authHeader = request.headers.authorization

if (!authHeader || !isValidAuth(authHeader)) {
reply.code(401).send({ error: 'Unauthorized' })
}
})

// Setup proxy to Pimlico API
fastify.register(proxy, {
upstream: `https://api.pimlico.io/v2/137/rpc?apikey=${PIMLICO_API_KEY}`,
prefix: '/api/proxy',
rewriteRequestHeaders: (req, headers) => ({
...headers,
})
})

// Start server
fastify.listen({ port: 3000 }, (err) => {
if (err) {
fastify.log.error(err)
process.exit(1)
}
})

// Helper function to validate auth
function isValidAuth(authHeader: string): boolean {
// Implement your authentication logic here
return true
}

```

```typescript [express.ts]
import express from 'express';
import { createProxyMiddleware } from 'http-proxy-middleware';

const app = express();
const PIMLICO_API_KEY = process.env.PIMLICO_API_KEY;
const targetUrl = `https://api.pimlico.io/v2/137/rpc?apikey=${PIMLICO_API_KEY}`;


// Middleware to check authentication
app.use((req, res, next) => {
const authHeader = req.headers.authorization;

if (!authHeader || !isValidAuth(authHeader)) {
return res.status(401).json({ error: 'Unauthorized' });
}
next();
});

// Setup proxy to Pimlico API
app.use('/api/proxy', createProxyMiddleware({
target: targetUrl,
changeOrigin: true,
pathRewrite: {
'^/api/proxy': '', // Remove '/proxy' from the path
},
onProxyReq: (proxyReq, req) => {
// Ensure JSON content type
proxyReq.setHeader('Content-Type', 'application/json');
},
}));

// Start server
app.listen(3000, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log('Server is running on port 3000');
});

// Helper function to validate auth
function isValidAuth(authHeader: string): boolean {
// Implement your authentication logic here
return true;
}

```

::::
10 changes: 10 additions & 0 deletions vocs.config.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineConfig } from "vocs"
import viteConfig from "./utils"
import { link } from "fs"
import { text } from "stream/consumers"

export const platformSidebar = [
{
Expand Down Expand Up @@ -45,6 +46,15 @@ export const platformSidebar = [
},
],
},
{
text: "Security",
items: [
{
text: "How to protect your API keys",
link: "/infra/platform/security/protect-api-keys",
},
],
},
{
text: "Debugging",
items: [
Expand Down
Loading