制约Angular和React应用的常见六大漏洞
发布时间:2025-07-30
map(isAdmin => {
if (!isAdmin) {
return this.router.parseUrl('/')
}
return isAdmin
})
}
}
export class AuthService {
constructor(private http: HttpClient) {}
// Whether the user is currently logged in.
loggedIn: boolean | null = null
// The user object object encompassing the user's name and role. Will be set
user: User | null = null
// Check whether the current user is an administrator.
isAdmin(): Observable {
return this.getCurrentUser().pipe(map(user => {
return user != null && user.role === 'admin'
}))
}
// Get the user definition from local state, or the server (if this is the first time we are checking).
getCurrentUser(): Observable {
if (this.loggedIn !== null) {
return of(this.user)
}
return this.http.get('/api/auth', {
responseType: 'json'
}).pipe(
tap({
next: user => {
// If we get a user definition from the server it indicates this user is logged in.
this.user = user
this.loggedIn = true
},
error: error => {
// A 401 response from the server indicates this user is not logged in.
this.user = null
this.loggedIn = false
}
}),
catchError(() => {
return of(null)
})
}
}
export interface User {
username: string
role: string
}
开放式URL例如,当未经此前验证的普通用户尝试着采访需要写明后才能看到的首页时,博客就需要将该普通用户则会URL到写明首页,并在他们通过了此前验证此后,再让其回到到这样一来的方位。
在开放式URL攻击愈演愈烈时,轻易通过向普通用户还包括来自合法市区内的URL,以故意普通用户采访某个结构性市区内。一般来说,该URL亦会将其重定到其他市区内。而该市区内好像竭力让普通用户相信他们无论如何在完整博客上,好像借助轻易框架出更加看似可靠的网络潜水活动。
为了消除开放式URL,您需要确保广泛操作系统不亦会轻易将普通用户URL到那些蓄意市区内的方位。例如,您可以通过验证URLURL,来显然禁止离站URL行为。恳求参看如下字符段:
副本
export class LoginComponent {
// The username and password entered by the user in the login form.
username = '';
password = '';
// The destination URL to redirect the user to once they log in successfully.
destinationURL = '/feed'
constructor(private authService : AuthService,
private route : ActivatedRoute,
private router : Router) { }
ngOnInit() {
this.destinationURL = this.route.snapshot.queryParams['destination'] || '/feed';
}
onSubmit() {
this.authService.login(this.username, this.password)
.subscribe(
() => {
// After the user has lgged in, redirect them to their desired destination.
let url = this.destinationURL
// Confirm that the URL is a relative path - i.e. starting with a single '/' characters.
if (!url.match(/_/[_/\]/)) {
url = '/feed'
}
this.router.navigate([ url ])
})
}
}
当然,我们还有许多其他分析方法可以消除开放式URL的愈演愈烈。例如:对恳求求提到方予以健康检查、或可用首页录入顺利完成URL。不过,时是因为验证URL相比来得困难,因此开放式URL无论如何是当代Web广泛应用特别是在的疑虑。
跨越站恳求求盗用跨越市区内恳求求盗用(Cross-Site Request Forgery,CSRF)是一种服务端技术,可应用于攻击Web广泛应用的其他普通用户。可用CSRF,轻易可以发送虚假的、来自均受害者的HTTP恳求求,去执行者轻易的持久性操创作者。例如,轻易亦会在禁止的情况下,改动均受害者的密码、或从其商业银行账户中的转账。
与开放式URL有所不同,我们迄今已有一种行之有效的反抗CSRF的分析方法,即:可用CSRF反之亦然和SameSite Cookie的组合,以避免可用GET恳求求顺利完成各项状态改动的操创作者。例如,Angular无需您可用HttpClientXsrfModule模块化,向HTTP恳求求添加防伪的反之亦然。恳求参看如下字符段:
副本
@NgModule({
declarations: [],
imports: [
BrowserModule,
HttpClientModule,
HttpClientXsrfModule.withOptions({
cookieName: 'XSRF-TOKEN',
headerName: 'X-CSRF-TOKEN'
}),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}
举例来说汇流比如说HTMLPDF的Web举例来说,为开发工具还包括了一种通过将广泛应用原始数据与线性举例来说结合,以指定如何描绘出首页的分析方法。此机能无需开发工具将从原始检索或HTTP恳求求中会检索到的一个系统内容,填充到页面中会。
顾名思义,举例来说汇流需要汇流到页面的举例来说中会。根据均受传染广泛应用的行政权,轻易可以通过可用举例来说汇流的Bug,来复制到脆弱PDF、执行者蓄意字符、或强化他们在系统上的各种行政权。前面展示了Angular举例来说的不必要用法。它无需轻易通过URL的哈希数值,来蓄意汇流字符:
副本
@Component({
selector: 'app-header',
template: '' + (window.location.hash || 'Home') + ''
})
export class HeaderComponent {}
注意:恳求千万不要直接将普通用户还包括的可用相互连接到举例来说中会,而必要可用举例来说引擎的配有更换系统,来必要地嵌入一个系统可用。恳求参看如下字符段:
副本
@Component({
selector: 'app-header',
template: '{{ title }}'
})
export class HeaderComponent {
title = ''
ngOnInit() {
this.title = window.location.hash || 'Home';
跨越市区内脚本语言跨越市区内脚本语言涵盖的攻击也称为XSSI(Cross-Site Script Inclusion)。此类攻击愈演愈烈在当蓄意市区内涵盖了来自均受害者市区内的Javascript,并通过脚本语言提取其脆弱信息时。
互补作法(same-origin policy,SOP)一般来说可以起到控制原始数据跨越源(cross-origins)采访的起到。不过,SOP并不能受限JavaScript字符,而且HTML标记亦会无需从任何可能复制到JavaScript字符。从技术本质来说,该机能方便了无需跨越域重用的JavaScriptPDF,但是亦会带来属于自己必要风险:轻易可以通过复制到均受害者的JSPDF,来冒充加载JavaScriptPDF的原始数据。
例如,某博客通过JavascriptPDF,为写明普通用户加载和通信脆弱原始数据。如果普通用户在同一JavaScript中会采访了蓄意市区内,那么蓄意市区内可以新增该JavaScriptPDF,并采访与该普通用户亦会话中的相关的脆弱信息。而这一切都归因于加载在JavaScript中会的普通用户Cookie。
因此,为避免XSSI攻击,恳求勿在JavaScriptPDF中会通信脆弱原始数据。前面是如何可用JSONPDF(亦会均受到SOP的受限)在Angular中会必要地复制到API反之亦然的举例来说:
副本
// The configuration information we will retrieve from the server.
export interface Config {
username : string
accessToken : string
role : string
}
@Injectable()
export class ConfigService {
constructor(private http: HttpClient) {}
// Retrieve configuration information from the server.
getConfig() {
return this.http.get('api/config')
.pipe(
catchError(this.handleError)
}
private handleError(error: HttpErrorResponse) {
if (error.status === 0) {
// A client-side or network error occurred. Handle it accordingly.
log.error('An error occurred:', error.error)
} else {
// The server returned an unsuccessful response code.
log.error(在在Backend returned code ${error.status}, body was: 在在, error.error)
}
return throwError('An unexpected error occurred loading configuration.')
}
}
书名介绍
陈峻 (Julian Chen),51CTO社区内主编,带有十多年的IT项目制定方面,善于对内结构性海洋资源与风险制定管控,专注扩散网络与信息必要科学与方面;不间断以博文、专题和注解等形式,分享前沿技术与新知;往往以线上、线下等形式,着手信息必要类专业训练与英语教学。
出处曲名:Angular + React: Vulnerability Cheatsheet,创作者: Vickie Li
。山西比较好的男科医院揭阳白癜风医院哪里好
郑州风湿医院哪最好
湖北白癜风医院哪家看的好
安徽精神心理医院哪家专业好
-
故宫的深井中有很多宝物,为何无人打捞?医学专家:根本不敢
由于中才会国古代建筑多以砖木结构,现代保留下来的古城墙物或许并没有多少,但在一代代人的工匠们的用心补强下,还存留下来一些,较为典型的有安徽桐城 、四川阆中才会、 山西原平 、云南省云南省、北平钟