-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
16e7c4f
commit a6aa6b7
Showing
3 changed files
with
145 additions
and
0 deletions.
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,55 @@ | ||
import { SyntaxKind } from 'ts-morph' | ||
import { PatcherFactory } from '../../types/index.js' | ||
import { ConfigUpdaterPatcher } from '../config_updater_patcher.js' | ||
|
||
export function shieldConfig(): PatcherFactory { | ||
return (runner) => new ShieldConfig(runner) | ||
} | ||
|
||
/** | ||
* Rewrite the config/shield.ts file to use the new API | ||
*/ | ||
export class ShieldConfig extends ConfigUpdaterPatcher { | ||
static patcherName = 'shield-config' | ||
|
||
async invoke() { | ||
super.invoke() | ||
|
||
const file = this.getConfigFile('config/shield.ts') | ||
if (!file) return | ||
|
||
/** | ||
* Take each configuration section exported from the old file | ||
* and create a new object literal that will include all | ||
*/ | ||
const exportedDeclarations = file.getExportedDeclarations() | ||
const sections = ['csp', 'csrf', 'xframe', 'hsts', 'contentTypeSniffing', 'dnsPrefetchControl'] | ||
|
||
let configObjectLiteral = '{\n' | ||
|
||
sections.forEach((sectionName) => { | ||
const symbols = exportedDeclarations.get(sectionName) | ||
if (!symbols) return | ||
|
||
const symbol = symbols[0].getChildrenOfKind(SyntaxKind.ObjectLiteralExpression) | ||
configObjectLiteral += sectionName + ': ' + symbol[0].getText() + ',\n' | ||
}) | ||
|
||
configObjectLiteral += '}' | ||
|
||
/** | ||
* Write the new file | ||
*/ | ||
const newContent = ` | ||
import { defineConfig } from '@adonisjs/shield' | ||
export default defineConfig(${configObjectLiteral}) | ||
` | ||
file.replaceWithText(newContent) | ||
await this.formatFile(file).save() | ||
|
||
this.logger.info('Updated config/shield.ts file') | ||
|
||
this.exit() | ||
} | ||
} |
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,88 @@ | ||
import dedent from 'dedent' | ||
import { test } from '@japa/runner' | ||
|
||
import { createRunner } from '../test_helpers/index.js' | ||
import { shieldConfig } from '../src/patchers/config_files/shield_config.js' | ||
|
||
test.group('Shield config', () => { | ||
test('Update shield config', async ({ assert, fs }) => { | ||
await fs.setupProject({}) | ||
|
||
await fs.create( | ||
'config/shield.ts', | ||
dedent` | ||
import Env from '@ioc:Adonis/Core/Env' | ||
import { ShieldConfig } from '@ioc:Adonis/Addons/Shield' | ||
export const csp: ShieldConfig['csp'] = { | ||
enabled: false, | ||
directives: {}, | ||
reportOnly: false, | ||
} | ||
export const csrf: ShieldConfig['csrf'] = { | ||
enabled: Env.get('NODE_ENV') !== 'test', | ||
exceptRoutes: [], | ||
enableXsrfCookie: true, | ||
methods: ['POST', 'PUT', 'PATCH', 'DELETE'], | ||
} | ||
export const dnsPrefetch: ShieldConfig['dnsPrefetch'] = { | ||
enabled: true, | ||
allow: true, | ||
} | ||
export const xFrame: ShieldConfig['xFrame'] = { | ||
enabled: true, | ||
action: 'DENY', | ||
} | ||
export const hsts: ShieldConfig['hsts'] = { | ||
enabled: true, | ||
maxAge: '180 days', | ||
includeSubDomains: true, | ||
preload: false, | ||
} | ||
export const contentTypeSniffing: ShieldConfig['contentTypeSniffing'] = { | ||
enabled: true, | ||
} | ||
` | ||
) | ||
|
||
await createRunner({ | ||
projectPath: fs.basePath, | ||
patchers: [shieldConfig()], | ||
}).run() | ||
|
||
const content = await fs.contents('config/shield.ts') | ||
assert.snapshot(content).matchInline(` | ||
" | ||
import { defineConfig } from '@adonisjs/shield' | ||
export default defineConfig({ | ||
csp: { | ||
enabled: false, | ||
directives: {}, | ||
reportOnly: false, | ||
}, | ||
csrf: { | ||
enabled: Env.get('NODE_ENV') !== 'test', | ||
exceptRoutes: [], | ||
enableXsrfCookie: true, | ||
methods: ['POST', 'PUT', 'PATCH', 'DELETE'], | ||
}, | ||
hsts: { | ||
enabled: true, | ||
maxAge: '180 days', | ||
includeSubDomains: true, | ||
preload: false, | ||
}, | ||
contentTypeSniffing: { | ||
enabled: true, | ||
}, | ||
}) | ||
" | ||
`) | ||
}) | ||
}) |