-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
feat(server): client version check #9205
Open
darkskygit
wants to merge
7
commits into
canary
Choose a base branch
from
darksky/version-check
base: canary
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.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5d3b83e
feat: init version check
darkskygit f415446
feat: add test
darkskygit e424de7
feat: more detailed test case
darkskygit 9e2228f
chore: adjust comment
darkskygit 83b544c
feat: check with min version
darkskygit 025a45e
feat: add real world version test
darkskygit fae6343
chore: cleanup codes
darkskygit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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,29 @@ | ||
import { defineRuntimeConfig, ModuleConfig } from '../../base/config'; | ||
|
||
export interface VersionConfig { | ||
enable: boolean; | ||
allowedVersion: string; | ||
} | ||
|
||
declare module '../../base/config' { | ||
interface AppConfig { | ||
version: ModuleConfig<never, VersionConfig>; | ||
} | ||
} | ||
|
||
declare module '../../base/guard' { | ||
interface RegisterGuardName { | ||
version: 'version'; | ||
} | ||
} | ||
|
||
defineRuntimeConfig('version', { | ||
enable: { | ||
desc: 'Check version of the app', | ||
default: false, | ||
}, | ||
allowedVersion: { | ||
desc: 'Allowed version range of the app that can access the server', | ||
default: '>=0.0.1', | ||
}, | ||
}); |
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,40 @@ | ||
import type { | ||
CanActivate, | ||
ExecutionContext, | ||
OnModuleInit, | ||
} from '@nestjs/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { | ||
getRequestResponseFromContext, | ||
GuardProvider, | ||
Runtime, | ||
} from '../../base'; | ||
import { VersionService } from './service'; | ||
|
||
@Injectable() | ||
export class VersionGuardProvider | ||
extends GuardProvider | ||
implements CanActivate, OnModuleInit | ||
{ | ||
name = 'version' as const; | ||
|
||
constructor( | ||
private readonly runtime: Runtime, | ||
private readonly version: VersionService | ||
) { | ||
super(); | ||
} | ||
|
||
async canActivate(context: ExecutionContext) { | ||
if (!(await this.runtime.fetch('version/enable'))) { | ||
return true; | ||
} | ||
|
||
const { req } = getRequestResponseFromContext(context); | ||
|
||
const version = req.headers['x-affine-version']; | ||
|
||
return this.version.checkVersion(version); | ||
} | ||
} |
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,13 @@ | ||
import './config'; | ||
|
||
import { Module } from '@nestjs/common'; | ||
|
||
import { VersionGuardProvider } from './guard'; | ||
import { VersionService } from './service'; | ||
|
||
@Module({ | ||
providers: [VersionService, VersionGuardProvider], | ||
}) | ||
export class VersionModule {} | ||
|
||
export type { VersionConfig } from './config'; |
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,68 @@ | ||
import assert from 'node:assert'; | ||
|
||
import { Injectable, Logger } from '@nestjs/common'; | ||
import semver from 'semver'; | ||
|
||
import { Runtime, UnsupportedClientVersion } from '../../base'; | ||
|
||
@Injectable() | ||
export class VersionService { | ||
private readonly logger = new Logger(VersionService.name); | ||
|
||
constructor(private readonly runtime: Runtime) {} | ||
|
||
private async getRecommendedVersion(versionRange: string) { | ||
try { | ||
const range = new semver.Range(versionRange); | ||
const versions = range.set | ||
.flat() | ||
.map(c => c.semver) | ||
.toSorted((a, b) => semver.rcompare(a, b)); | ||
return versions[0]?.toString(); | ||
} catch { | ||
return semver.valid(semver.coerce(versionRange)); | ||
} | ||
} | ||
|
||
async checkVersion(clientVersion?: any) { | ||
const allowedVersion = await this.runtime.fetch('version/allowedVersion'); | ||
const recommendedVersion = await this.getRecommendedVersion(allowedVersion); | ||
if (!allowedVersion || !recommendedVersion) { | ||
// ignore invalid allowed version config | ||
return true; | ||
} | ||
|
||
const parsedClientVersion = semver.valid(clientVersion); | ||
const action = semver.lt(parsedClientVersion || '0.0.0', recommendedVersion) | ||
? 'upgrade' | ||
: 'downgrade'; | ||
assert( | ||
typeof clientVersion === 'string' && clientVersion.length > 0, | ||
new UnsupportedClientVersion({ | ||
clientVersion: '[Not Provided]', | ||
recommendedVersion, | ||
action, | ||
}) | ||
); | ||
|
||
if (parsedClientVersion) { | ||
if (!semver.satisfies(parsedClientVersion, allowedVersion)) { | ||
throw new UnsupportedClientVersion({ | ||
clientVersion, | ||
recommendedVersion, | ||
action, | ||
}); | ||
} | ||
return true; | ||
} else { | ||
if (clientVersion) { | ||
this.logger.warn(`Invalid client version: ${clientVersion}`); | ||
} | ||
throw new UnsupportedClientVersion({ | ||
clientVersion, | ||
recommendedVersion, | ||
action, | ||
}); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
we might need the channel control, cross channel connections should be forbidden